简体   繁体   中英

How to run/execute remote scripts locally using powershell scipt

Steps:

1. I have created a file known as: "test.vbs" under temp folder in machine B and in that script i have msgbox "hi"

2. From machine A - my goal is execute to test.vbs in machine B

3. In machine A. i did the following steps but i didnt get remote script executed. Can anyone please help me.

ps c:/> $s=new -pssession -computername machineB ps c:/> Invoke-Command -Session $s-ScriptBlock{$filecount=(Get-ChildItem c:\\temp\\test.vbs -Recurse)} ps c:/> Invoke-Command -Session $s-ScriptBlock{$filecount}

i received directory c:\\temp and a file name as test.vbs

Now i want to execute test.vbs

ps c:/> Invoke-Command -ComputerName MachineB{& "C:\\Temp\\test.vbs"}

After executing the above command i dont see the test.vbs not executing remotely. my expectation is to get hi messagebox.

I am not understanding if it got executed or not.

can anyone please help me to execute remote script locally using power shell.

thanks inadvance.

If my understanding to your question is correct, you want to execute test.vbs against machine B from machine A, I don't think it's possible in PowerShell.

I faced the same issue some time ago. Neither Enter-PsSession nor Invoke-Command can do it for you probably just because they are not designed for this purpose. They are working totally fine if you run any PowerShell cmdlets remotely eg Get-Item you can easily get items on machine B. But once if you try to do something beyond the PowerShell remote session, like run a vbs script which will start a new CScript.exe process, it just won't work. It's due to some reason in underlying implementation of this PowerShell remoting.

If you do need to do that, I got two workarounds

  1. PsExec can be the best solution. It was designed to do this kind of work originally and it can easily accomplish what you want with a bunch of advanced features.
  2. Create a scheduled task on machine B by using New-ScheduledTask and let it trigger itself on machine B locally.
  3. If you have SCCM installed, it also provides this kind of functionality, check out this blog .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM