Question

I have a very simple powershell script that has a different behaviour depending on if I execute the script locally or remotely through a Invoke-Command / Enter-PSSession . Other scripts work fine. I'm wondering what is different about the environment of a remote session vs. a local session that would cause this difference.

Was it helpful?

Solution

Assuming the remote endpoint is set up to use your credentials, the remote session may have a different set of modules available (or different versions), there may be a profile script configured to run for the remote session and the environment is likely to be different. Also note that remote endpoints can be configured to limit access to commands (including applications) as well as restrict the language mode. A script could also walk various variables and functions to limit their visibility as well. If you look at session state you can see some of the properties that can be tweaked when configuring a remoting endpoint e.g.:

PS> $ExecutionContext.SessionState


Drive                         : System.Management.Automation.DriveManagementIntrinsics
Provider                      : System.Management.Automation.CmdletProviderManagementIntrinsics
Path                          : System.Management.Automation.PathIntrinsics
PSVariable                    : System.Management.Automation.PSVariableIntrinsics
LanguageMode                  : FullLanguage
UseFullLanguageModeInDebugger : False
Scripts                       : {*}
Applications                  : {*}
Module                        :
InvokeProvider                : System.Management.Automation.ProviderIntrinsics
InvokeCommand                 : System.Management.Automation.CommandInvocationIntrinsics

See this article for more info on constrained endpoints. All that said, it is likely you're using the default endpoint which is usually not constrained. Another difference could be bitness. For instance, you could be running in an x86 shell but connecting to a 64-bit endpoint or vice-versa. If you need to connect to a 32-bit endpoint try the following:

PS> $s = New-PSSession -cn localhost -ConfigurationName microsoft.powershell32
PS> icm -Session $s { [IntPtr]::Size }
4
PS> Remove-PSSession $s
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top