$Using Variables: Passing Variables in PowerShell Remote Sessions
PowerShell scripts that attempt to manage remote machines using locally declared variables can be tricky the first time around. You might have ensured that your script works properly locally only to discover that functionality breaks down when you try to run it against a remote machine using Invoke-Command or a PSSession. Your issue might very well be due to PowerShell scoping.
Types of Scopes in PowerShell in Local and Remote Machines:
Scoping in PowerShell refers to how your variables, functions, and aliases are visible and accessible in your code. It's about determining the lifespan and visibility of these elements within different parts of a script or a PowerShell session. Understanding scoping is crucial because it affects how you read and write scripts, especially when dealing with variables used across remote sessions or in different parts of a script.
Local Machine Scopes: These are the scopes on the machine where you are running the PowerShell session.
Global Scope: Variables or functions set at this level are accessible anywhere in your local PowerShell session.
Script Scope: Specific to the script you are running on your local machine. Variables and functions defined here are not accessible outside the script on your local machine.
Local (Function) Scope: Within a function on your local machine, variables and functions are confined to that function.
Remote Machine Scopes: When you run scripts or commands on a remote machine (for example, using Invoke-Command or PSSessions), the scopes are as follows:
Remote Global Scope: This is the global scope of the remote PowerShell session. It is entirely separate from the global scope of your local session.
Remote Script Scope: If you run a script on a remote machine, this scope is specific to that script in the remote session.
Remote Local (Function) Scope: Similar to the local machine but confined to a function in a script running on the remote machine.
Techniques to Pass Variables across Remote Sessions
Three ways to achieve this are:
The built-in $args array
param() blocks
The $Using scope modifier
Built-in $args Array
The $args array is a built-in PowerShell variable that automatically captures undeclared arguments passed to a script or function. When you use Invoke-Command to run a script block on a remote machine, any additional arguments after the script block are passed into the $args array on the remote side and referenced by the explicit values set in the -ArgumentList parameter.
Param() Blocks
The param() block is used at the beginning of a script or a function to declare expected parameters explicitly. You can define a param() block within the script block you execute remotely. This allows you to pass variables as named parameters.
The $Using Scope Modifier
The $Using scope modifier, included in PowerShell 3.0+ allows for seamless use of local variables in remote script blocks. When you create a script block to run remotely, you can reference local variables directly within the script block by prefixing them with $Using