Remote Variables
When using PowerShell remoting, you need to remember that you’re dealing with two or more computers that don’t share information between them. For example, the following command will run fine on your local computer:
However, if you try to run just the Copy-Item command on a remote computer, it will fail:
The problem here is that $f1 and $f2 are defined on your computer, but not on the remote computer. The script block passed by Invoke-Command isn’t evaluated on your computer, it’s simply passed as-is.
There are two possible fixes. The first is to simply include the variable definitions in the script block:
Another technique, available in PowerShell v3 and later, is to use the $using variable designator. PowerShell pre-scans the script block for these, and will pass along your local variable values to the remote computer(s);
The special $using: syntax is what makes this version of the command work.
Last updated