Posh-SSH icon indicating copy to clipboard operation
Posh-SSH copied to clipboard

Running Invoke-SSHCommand in background fails

Open made1990 opened this issue 3 years ago • 2 comments

Hi,

I need to run several ssh commands to different hosts in parallel. Thats why I'd like to run them in background, but that fails.

I run:

#$mySession = New-SSHSession -ComputerName $myHostname -Credential $Credentials
#start-job -scriptblock {Invoke-sshcommand -SSHSession $using:mySession -Command "some nice command"}

It starts a job in the background and when I check it with receive-job I see:

Cannot process argument transformation on parameter 'SSHSession'. Cannot convert value "SSH.SshSession" to type "SSH.SshSession[]". Error: "Cannot convert value 
"SSH.SshSession" to type "SSH.SshSession". Error: "Cannot convert the "SSH.SshSession" value of type "Deserialized.SSH.SshSession" to type "SSH.SshSession"."

Is there a way to run Invoke-SSHCommand in background ? The As-Job parameter does not seem to exist.

made1990 avatar Jan 31 '23 14:01 made1990

Because a Job is in its own PS Runspace, you would need to load the module in to each Job, make the connection and run the command I believe.

On Jan 31, 2023, at 10:53 AM, made1990 @.***> wrote:

Hi,

I need to run several ssh commands to different hosts in parallel. Thats why I'd like to run them in background, but that fails.

I run:

#$mySession = New-SSHSession -ComputerName $myHostname -Credential $Credentials #start-job -scriptblock {Invoke-sshcommand -SSHSession $using:mySession -Command "some nice command"} It starts a job in the background and when I check it with receive-job I see:

Cannot process argument transformation on parameter 'SSHSession'. Cannot convert value "SSH.SshSession" to type "SSH.SshSession[]". Error: "Cannot convert value "SSH.SshSession" to type "SSH.SshSession". Error: "Cannot convert the "SSH.SshSession" value of type "Deserialized.SSH.SshSession" to type "SSH.SshSession"." Is there a way to run Invoke-SSHCommand in background ? The As-Job parameter does not seem to exist.

— Reply to this email directly, view it on GitHub https://github.com/darkoperator/Posh-SSH/issues/507, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAD7IHU7E77LFIZTIP2LYFTWVEROBANCNFSM6AAAAAAUMRBHBQ. You are receiving this because you are subscribed to this thread.

darkoperator avatar Jan 31 '23 20:01 darkoperator

@darkoperator Thanks for your response. The problem is that I need to execute a ssh command on several target hosts at once. Thats why I prepare the SSH-Session for all targets in a first step, because setting up the ssh session varies in time for the number of targets. I only wanted to run the ssh command as a job to ensure (more or less) simultaneous execution. That is not possible if I have to setup the ssh session in every single job.

made1990 avatar Feb 10 '23 14:02 made1990