proxb / PoshRSJob

Provides an alternative to PSjobs with greater performance and less overhead to run commands in the background, freeing up the console and allowing throttling on the jobs.
MIT License
542 stars 87 forks source link

Invoke-Command #148

Closed kort3x closed 7 years ago

kort3x commented 7 years ago

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

Start-RSJob : The value of the using variable '$using:ip' cannot be retrieved because it has not been set in the local session.

MVKozlov commented 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"

kort3x commented 7 years ago

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...

kort3x commented 7 years ago

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 } }

MVKozlov commented 7 years ago

seems there is a bug in current version, #144, #145 with $args[0] instead of $_ my example works ok

proxb commented 7 years ago

@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.