2

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.

1
  • 1
    Connect-ExchangeOnline is defaulting to an interactive login as you suspect. You need to look at the documentation for how to connect using either a credential object which you can store encrypted on disk, or a certificate. Commented Aug 1, 2022 at 10:08

3 Answers 3

1
+50

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
Sign up to request clarification or add additional context in comments.

2 Comments

-UserPrincipalName also only works with pop-up window. could you give me any example with -Certificate or -Credential?
1

I Have done it following https://learn.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

Comments

0

Take a look at Get-Credential for possible alternatives.
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.security/get-credential?view=powershell-7.2

$User = "Domain01\User01"
$PWord = ConvertTo-SecureString -String "P@sSwOrd" -AsPlainText -Force
$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, $PWord

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

1 Comment

Get-Credential also opens with pop-up window that stores credentials for powershell

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.