How do I pass a local variable to a remote `Invoke-Command`?
powershell, powershell-4.0, powershell-remoting
Solution
In PowerShell 4 (3+ actually) the easiest way is to use the `Using` scope modifier:
Invoke-Command -ComputerName winserver -ScriptBlock {
Get-FileHash E:\test\$Using:dest.zip -Algorithm SHA1
}
For a solution that works with all versions:
Invoke-Command -ComputerName winserver -ScriptBlock {
param($myDest)
Get-FileHash E:\test\$myDest.zip -Algorithm SHA1
} -ArgumentList $dest
Problem
I'm trying to retrieve the hash of a file located on remote server using `Invoke-Command`. It works fine when I give the full path as below: ``` Invoke-Command -ComputerName winserver -ScriptBlock { Get-FileHash -Path E:\test\testfile.zip -Algorithm SHA1 } ``` But I need to pass the file name via a variable as below: ``` Invoke-Command -ComputerName winserver -ScriptBlock { Get-FileHash -Path "E:\test\$dest.zip" -Algorithm SHA1 } ``` How do I access this variable in the `scriptblock` of a remote session?