Here is my code :
$script={
Write-Host "Num Args:" $args.Length;
Write-Host $args[0]
}
Invoke-Command -ScriptBlock $script
When I run
powershell.exe .\test.ps1 one two three
I got
Num Args: 0
I thought I would get
Num Args: 3
One
Something am I missing?
Thanks
You actually have two scopes there. The script level and the script block level.
$args.Lengthand$args[0]will have what you are expecting at theInvoke-Commandlevel. Inside the script block there is another scope for$args. To get the args from the command line all the way into the script block you’ll need to re-pass them fromInvoke-Command -ArgumentList $args.