0

Hello I have a template with runspaces that runs command and gives output to outputbox. My big question is how to make it run on remote machine? If you run script now it gives you systeminfo of current machine but I need it on remote machines. Thanks in advance

$rs = [runspacefactory]::CreateRunspace()
https://pastebin.com/B91Dgz0a

1 Answers1

0

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.

Sid
  • 2,586
  • 1
  • 11
  • 22
  • Yes, it made sense its just me not knowing where to put AddArgument method in order to work. But I will figure it out. Thanks – Michal Gyurkovsky Dec 08 '19 at 07:47
  • It should be right after the `AddScript()` method if you are still interested. Be sure to pass the `$remoteComputer` variable to the `Function New-PowerShell` – Sid Dec 08 '19 at 08:34
  • I get you but still it isnt working gives me bunch of errors could you please try it editing here? I am not sure whole runspace is kinda new field for me https://cryptpad.fr/code/#/2/code/edit/HCnD7T-cKc+hjZjUdIMLdJSp/ – Michal Gyurkovsky Dec 08 '19 at 11:51
  • I have edited your code. I didn't have a remote machine to test it with but please go ahead and try it. – Sid Dec 08 '19 at 14:22
  • I dont have remote machine available now either but it seems to be likely working since no error has been thrown at me when doing $env:computername as compname. If it aint working I will write here for now I thank you very much sir. – Michal Gyurkovsky Dec 08 '19 at 15:43