PowerShell Scripting Guidelines for Local and Remote Use

What are some of the best practices for writing scripts to be executed in a remote context?

For example, I just discovered that the built-in var $Profile does not exist during remote execution.

+7
powershell powershell-remoting
source share
2 answers

Profile

You found one main difference, $profile not configured.

Buried at MSDN, here are some frequently asked questions about remote powershell or get-help about_Remote_FAQ .

In the section "WHERE IS MY PROFILES?" (heh) he explains:

For example, the following command launches the current CurrentUserCurrentHost profile from the local computer in a session in $ s.

  invoke-command -session $s -filepath $profile 

The following command launches the CurrentUserCurrentHost profile from a remote computer in a session in $ s. Since the variable $ profile is not populated, the command uses an explicit profile path.

  invoke-command -session $s {. "$home\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1"} 

Serialization

Another difference that may affect you is that instead of .NET objects returned by commands that simply return directly when you start them remotely and return them, they become serialized and deserialized over the wire. Many objects support this tone, but some do not. Powershell automatically removes methods on objects that are no longer "connected", and then they are mainly data structures ... but they intercept methods for some types, such as DirectoryInfo .

Usually you do not need to worry about this, but if you return complex objects through a pipe, you can ...

+2
source share
Blocks

Scripts do not act as closures, as usual:

 $var = 5 $sb={ $var } &$sb # 5 Start-Job $sb | Wait-Job | Receive-Job # nothing 
0
source share

All Articles