Closed kort3x closed 7 years ago
You can use parameters and -ArgumentList
for Invoke-Command
something like that
$JobsInvoke = $PSsessions | Start-RSJob -ScriptBlock { $ip = "8.8.8.8"; Invoke-Command -Session $_ -ScriptBlock {param($ip) test-connection -ComputerName $ip } -ArgumentList $ip }
@proxb , may be there is room for #118's -VariableToLoad
?
with rule "if -VariableToLoad used then $using:variable not expanded"
Ok, I tried
$JobsInvoke = $PSsessions | Start-RSJob -ScriptBlock { $ip = "8.8.8.8"; Invoke-Command -Session $_ -ScriptBlock {param($ip) test-connection -ComputerName $ip } -ArgumentList $ip }
but now $_ appears to be empty, as I receive "Argument is NULL or empty" for the -session paramter of Invoke-Command.
I dont get it...
btw, when I try an Invoke-Command without an ArgumentList like a simple Get-Date, there is no problem with -session.
$JobsInvoke = $PSsessions | Start-RSJob -ScriptBlock { $ip = "8.8.8.8"; Invoke-Command -Session $_ -ScriptBlock {get-date } }
seems there is a bug in current version, #144, #145
with $args[0]
instead of $_
my example works ok
@proxb , may be there is room for #118's -VariableToLoad ? with rule "if -VariableToLoad used then $using:variable not expanded"
That sounds like a good idea to look at implementing.
I have a Question: How do I pass a variable to invoke-command inside the ScriptBlock of Start-RSJob. Following is not working:
$JobsInvoke = $PSsessions | Start-RSJob -ScriptBlock { $ip = "8.8.8.8"; Invoke-Command -Session $_ -ScriptBlock {test-connection -ComputerName $using:ip }}
It fails with