Automating deployments using PowerShell & WinRM is, by turns, both awesome and deeply frustrating.
One of the main stumbling blocks, for me anyway, is which side of the machine boundary the code should be evaluated on.
When using Invoke-Command with a script block, any variables inside the block are evaluated remotely:
$foo = "foo" Invoke-Command -ScriptBlock { Write-Host "Foo: $foo" }
If you want to use a local variable on the remote machine, you need to force the evaluation earlier. I ended up with something inspired by Capistrano’s run action.
function run([string] $command, $session) { $runBlock = [ScriptBlock]::Create($command) try { Invoke-Command -Session $session -ScriptBlock $runBlock -ErrorAction Stop } catch { $_.Exception exit 1 } } $foo = "foo" run "write-host `"Foo: $foo`""
Alternatively, you can pass the args to Invoke-Command explicitly:
Invoke-Command -Args $foo, $bar -Session $session -ErrorAction Stop -ScriptBlock { param($foo, $bar) Write-Host "Foo: $foo" Write-Host "Bar: $bar" }