@@ -159,7 +159,7 @@ private async ValueTask<AccessToken> RequestAzurePowerShellAccessTokenAsync(bool
159159 try
160160 {
161161 output = async ? await processRunner . RunAsync ( ) . ConfigureAwait ( false ) : processRunner . Run ( ) ;
162- CheckForErrors ( output ) ;
162+ CheckForErrors ( output , processRunner . ExitCode ) ;
163163 ValidateResult ( output ) ;
164164 }
165165 catch ( OperationCanceledException ) when ( ! cancellationToken . IsCancellationRequested )
@@ -168,7 +168,7 @@ private async ValueTask<AccessToken> RequestAzurePowerShellAccessTokenAsync(bool
168168 }
169169 catch ( InvalidOperationException exception )
170170 {
171- CheckForErrors ( exception . Message ) ;
171+ CheckForErrors ( exception . Message , processRunner . ExitCode ) ;
172172 if ( _isChainedCredential )
173173 {
174174 throw new CredentialUnavailableException ( $ "{ AzurePowerShellFailedError } { exception . Message } ") ;
@@ -181,9 +181,10 @@ private async ValueTask<AccessToken> RequestAzurePowerShellAccessTokenAsync(bool
181181 return DeserializeOutput ( output ) ;
182182 }
183183
184- private static void CheckForErrors ( string output )
184+ private static void CheckForErrors ( string output , int exitCode )
185185 {
186- bool noPowerShell = ( output . IndexOf ( "not found" , StringComparison . OrdinalIgnoreCase ) != - 1 ||
186+ int notFoundExitCode = RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) ? 9009 : 127 ;
187+ bool noPowerShell = ( exitCode == notFoundExitCode || output . IndexOf ( "not found" , StringComparison . OrdinalIgnoreCase ) != - 1 ||
187188 output . IndexOf ( "is not recognized" , StringComparison . OrdinalIgnoreCase ) != - 1 ) &&
188189 // If the error contains AADSTS, this should be treated as a general error to be bubbled to the user
189190 output . IndexOf ( "AADSTS" , StringComparison . OrdinalIgnoreCase ) == - 1 ;
@@ -264,7 +265,7 @@ private void GetFileNameAndArguments(string resource, string tenantId, out strin
264265 if ( RuntimeInformation . IsOSPlatform ( OSPlatform . Windows ) )
265266 {
266267 fileName = Path . Combine ( DefaultWorkingDirWindows , "cmd.exe" ) ;
267- argument = $ "/d /c \" { powershellExe } \" { commandBase64 } \" \" ";
268+ argument = $ "/d /c \" { powershellExe } \" { commandBase64 } \" \" & exit ";
268269 }
269270 else
270271 {
0 commit comments