If you are working with variables inside a script block, you need to add a using: modifier. Otherwise, Powershell will look for the var definition inside the script block.
You can use it also using splatting technique. For instance:. @using:params
Like this:
# C:\Temp\Nested.ps1 [CmdletBinding()] Param( [Parameter(Mandatory=$true)] [String]$Msg ) Write-Host ("Nested Message: {0}" -f $Msg)
# C:\Temp\Controller.ps1 $ScriptPath = "C:\Temp\Nested.ps1" $params = @{ Msg = "Foobar" } $JobContent= { & $using:ScriptPath @using:params } Invoke-Command -ScriptBlock $JobContent -ComputerName 'localhost'
source share