Skip to content

Instantly share code, notes, and snippets.

@mwallner
Created July 23, 2019 13:01
Show Gist options
  • Save mwallner/56114300d6439838e5226a3ec87993fb to your computer and use it in GitHub Desktop.
Save mwallner/56114300d6439838e5226a3ec87993fb to your computer and use it in GitHub Desktop.
PowerShell RemoteException incosistency when run via Jenkins

NOTE: This seems to only affect Win10 1809 LTSC and/or PowerShell 5.1.17763.1

I've got this demo-script, which I put into a Jenkins job:

Write-Host "##############################################"
$ErrorActionPreference = "Stop"

Write-Host "ErrorActionPreference: $ErrorActionPreference"
Write-Host "whoami: $(whoami)"
$PSVersionTable | Format-Table

try {
 cmd /c "comanddoesnotexist c:/foo c:/bar"
} catch {
 Write-Host "### Exception! ###"
}

Write-Host "################### TRACE ###########################"

Trace-Command -Name errorrecord  -Expression { cmd /c "comanddoesnotexist c:/foo c:/bar" }  -PSHost

Write-Host "##############################################"

When I run it locally, I get the following result:

##############################################
ErrorActionPreference: Stop
whoami: ks\builduser

Name                           Value
----                           -----
PSVersion                      5.1.17763.1
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.17763.1
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1


Der Befehl "comanddoesnotexist" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
################### TRACE ###########################
Der Befehl "comanddoesnotexist" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
##############################################

however, when I run the exact same thing via Jenkins, I do get the following:

[PowerShell_DummyChecks] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Users\BUILDU~1\AppData\Local\Temp\jenkins5194440861464562005.ps1'"
##############################################
ErrorActionPreference: Stop
whoami: ks\builduser

Name                           Value                                                                                   
----                           -----                                                                                   
PSVersion                      5.1.17763.1                                                                             
PSEdition                      Desktop                                                                                 
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                 
BuildVersion                   10.0.17763.1                                                                            
CLRVersion                     4.0.30319.42000                                                                         
WSManStackVersion              3.0                                                                                     
PSRemotingProtocolVersion      2.3                                                                                     
SerializationVersion           1.1.0.1                                                                                 


### Exception! ###
################### TRACE ###########################
cmd : Der Befehl "comanddoesnotexist" ist entweder falsch geschrieben oder
In C:\Users\builduser\AppData\Local\Temp\jenkins5194440861464562005.ps1:16 Zeichen:48
+ ... cord  -Expression { cmd /c "comanddoesnotexist c:/foo c:/bar" }  -PSH ...
+                         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Der Befehl "com...eschrieben oder:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
Build step 'Windows PowerShell' marked build as failure
Finished: FAILURE

where does this 'RemoteException' come from - why is it being caught? note that the Jenkins-service is being executed with the same account as when run locally.

to make it even worse, this issue only seems to exist with the above stated combination of Win10 + PowerShell, when I run the same job on another (older) server, I get the following (expected) result:

[PowerShell_DummyChecks] $ powershell.exe -NonInteractive -ExecutionPolicy ByPass "& 'C:\Users\BUILDU~1\AppData\Local\Temp\jenkins5640455886657276493.ps1'"
##############################################
ErrorActionPreference: Stop
whoami: ks\builduser

Name                           Value                                                                                   
----                           -----                                                                                   
PSVersion                      5.1.14393.3053                                                                          
PSEdition                      Desktop                                                                                 
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}                                                                 
BuildVersion                   10.0.14393.3053                                                                         
CLRVersion                     4.0.30319.42000                                                                         
WSManStackVersion              3.0                                                                                     
PSRemotingProtocolVersion      2.3                                                                                     
SerializationVersion           1.1.0.1                                                                                 


Der Befehl "comanddoesnotexist" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
################### TRACE ###########################
Der Befehl "comanddoesnotexist" ist entweder falsch geschrieben oder
konnte nicht gefunden werden.
##############################################
Build step 'Windows PowerShell' marked build as failure
Finished: FAILURE
@mwallner
Copy link
Author

Short: it looks as if in Win10-1607, PowerShell 5.1.14393.3053, 'RemoteExceptions' are not breaking / catchable errors, in Win10-1809, PowerShell 5.1.17763.1 they are - but only under certain circumstances.

@msm-fc
Copy link

msm-fc commented Jul 26, 2019

FWIW I get this exact same remote exception in PowerShell ISE but not native Windows PowerShell.

PSVersion 5.1.17763.316
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.316
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment