As far as I know and used, Runspaces
are for multithreading
or parallel execution within the confines of a local machine. I am not aware of a way to start and manage the lifecycle of a thread
on a remote machine. That is the job of the OS of the remote machine.
Scriptblock
However, Runspaces
allow you to add scriptblocks
to it using the AddScript()
method as you have in your code. So all you have to do is change the code inside the scriptblock
to something like
SystemInfo /s $remoteComputer
.
You should also be able to use Invoke-command -ComputerName $remoteComputer -ScriptBlock {SystemInfo}
Passing the Argument
Now the question is of passing the $remoteComputer
argument to the scriptblock
. You can use the AddArgument()
method of the Runspace
like: (Using the variablename in your code)
$ps.Runspace.AddArgument($remoteComputer)
method.
I hope it made sense.