System Center Orchestrator launches 32-bit PowerShell 2.0 to run PowerShell scripts. I thought I had a good solution, wrapping code with this to launch it in a 64-bit 4.0 session of PowerShell. (This is the simplified version without parameters.)
If ( [IntPtr]::Size * 8 -eq 32 ) { Set-Location -Path 'C:\Windows\SysNative\WindowsPowerShell\v1.0' } Else { Set-Location -Path 'C:\Windows\System32\WindowsPowerShell\v1.0' } .\PowerShell.exe -command { # Code here }
I can test it like this.
If ( [IntPtr]::Size * 8 -eq 32 ) { Set-Location -Path 'C:\Windows\SysNative\WindowsPowerShell\v1.0' } Else { Set-Location -Path 'C:\Windows\System32\WindowsPowerShell\v1.0' } .\PowerShell.exe -command { Return $PSVersionTable.PSVersion.ToString(), "$([IntPtr]::Size * 8)-bit" }
Testing in many different types of PowerShell on various server OS’s, this verifies that no matter what version and bit-ness of PowerShell I run it it, the core code will be run in 64-bit 4.0.
The problem arises when I try to start a job. Like this.
If ( [IntPtr]::Size * 8 -eq 32 ) { Set-Location -Path 'C:\Windows\SysNative\WindowsPowerShell\v1.0' } Else { Set-Location -Path 'C:\Windows\System32\WindowsPowerShell\v1.0' } .\PowerShell.exe -command { $ExecutableJob = Start-Job -ScriptBlock { Return 'Result'} Return $PSVersionTable.PSVersion.ToString(), "$([IntPtr]::Size * 8)-bit", $ExecutableJob.State }
When I run this from 64-bit PowerShell (any version) the returned job state is “Running.”. When I run this from 32-bit PowerShell (any version) the returned job state is “Failed”. Both the returned data and monitoring in Task Manager show that the code is running on a separate, 64-bit PowerShell instance. So what is the difference between a 64-bit PowerShell instance launched from 32-bit PowerShell and a 64-bit PowerShell instance launched from 64-bit PowerShell that causes jobs in the former to fail?