Powershell script not running from php

Viewed 155

I am running one PowerShell script from PHP like the below example

$ff = shell_exec(env("POWERSHELL_PATH") . ' '.Storage::path('public\ps\test.ps1').' 2>&1');
Storage::disk('public')->put('log.txt', $ff);

above code runs PowerShell script and stores result in log.txt file. It's running fine when test.ps1 has the below content

$version = $PSversionTable
$version.PSVersion.Major

but when test.ps1 has the below content

connect-exchangeonline
Get-MalwareFilterPolicy -Identity Default | Select-Object EnableFileFilter

it gives me the below error.

Error Acquiring Token:
System.InvalidOperationException: Showing a modal dialog box or form when the application is not running in UserInteractive mode is not a valid operation. Specify the ServiceNotification or DefaultDesktopOnly style to display a notification from a service application.
at Microsoft.Identity.Client.Platforms.Features.WinFormsLegacyWebUi.WebUI.<AcquireAuthorizationAsync>d__20.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Identity.Client.Internal.AuthCodeRequestComponent.<FetchAuthCodeAndPkceInternalAsync>d__7.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Identity.Client.Internal.AuthCodeRequestComponent.<FetchAuthCodeAndPkceVerifierAsync>d__4.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Identity.Client.Internal.Requests.InteractiveRequest.<GetTokenResponseAsync>d__11.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Identity.Client.Internal.Requests.InteractiveRequest.<ExecuteAsync>d__8.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Identity.Client.Internal.Requests.RequestBase.<RunAsync>d__13.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Identity.Client.ApiConfig.Executors.PublicClientExecutor.<ExecuteAsync>d__2.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
at Microsoft.Exchange.Management.AdminApiProvider.Authentication.MSALTokenProvider.<GetAccessTokenAsync>d__22.MoveNext()
New-ExoPSSession : One or more errors occurred.
At C:\Program\Files\WindowsPowerShell\Modules\ExchangeOnlineManagement\2.0.5\netFramework\ExchangeOnlineManagement.psm1:475 char:30
+ ... PSSession = New-ExoPSSession -ExchangeEnvironmentName $ExchangeEnviro ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : NotSpecified: (:) [New-ExoPSSession], AggregateException
+ FullyQualifiedErrorId : System.AggregateException,Microsoft.Exchange.Management.ExoPowershellSnapin.NewExoPSSess 
ion

I think it is related pop-up window for authorization but don't know how to resolve it.

3 Answers

I think it is related pop-up window for authorization...

You are right! That's exactly what the error message is telling you:

Showing a modal dialog box or form when the application is not running in 
UserInteractive mode is not a valid operation.

As Robin already stated in the comments, you have to provide authentication parameters to Connect-ExchangeOnline, so that it does not have to prompt you for them interactively.

It supports multiple different authentication methods. Depending on how you want to authenticate, have a look at:

  • -UserPrincipalName for modern authentication
  • -UserPrincipalName -UseRPSSession for modern authentication with Basic Authentication
  • -CertificateFilePath/-CertificateThumbprint/-Certificate for public key authentication
  • -Credential

I Have done it following https://docs.microsoft.com/en-us/powershell/module/exchange/connect-exchangeonline?view=exchange-ps#example-4

where I have created one app in tenant and given it proper permissions, then I create one self-signed certificate and assign that certificate to that app. now I can easily connect to the exchange using below command.

Connect-ExchangeOnline -CertificateThumbPrint "FFF39295FB842B345FV3012B27272C693C5BB725" -AppID "875d7314-3247-4d3d-b419-0c9060334a5f" -Organization "tenant.onmicrosoft.com
Related