powershell远程执行exe

发布于 2025-02-06 08:13:21 字数 3036 浏览 1 评论 0原文

我已经搜索并尝试了不同的命令变体,但是似乎没有任何命令对我有用。我相信我也经历了有关此的所有SO参考。

在这里尝试了所有方法: https:/// social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

我的目标是运行已经驻留在远程VM上的EXE。我创建并发布了一个控制台应用程序(在C#中)。它更像是双击,我只需双击EXE文件,然后完成任务即可。我尝试了使用不同的方法,但我似乎无法得到我想要的东西。

尝试的方法:

  1. Indoke-VMScript
  2. win32_process.create()
  3. Invoke-command
  4. start-command start-
  5. psexec
  6. invoke-wmimethod
  7. [diagnostics.process] ::开始

很少的测试:

Invoke-VMScript -VM VMName -GuestUser $(User_ADMIN) -GuestPassword "$(ADMIN_PASSWORD)" -ScriptText { D:\Path\to\ConsoleAppName.exe }

Invoke-VMScript -VM VMName -GuestUser $(User_ADMIN) -GuestPassword "$(ADMIN_PASSWORD)" -ScriptText { cmd /c D:\Path\to\ConsoleAppName.exe }

#This ps1 file will in turn contain command to run the exe file. Tried same with bat as well
Invoke-VMScript -VM VMName -GuestUser $(User_ADMIN) -GuestPassword "$(ADMIN_PASSWORD)" -ScriptText { PowerShell -NoProfile -ExecutionPolicy Bypass -Command "D:\Path\to\ConsoleAppPS.ps1" } 

Invoke-Command -ComputerName VMName -ScriptBlock { & "D:\Path\to\ConsoleAppName.exe" }
#Tried $session = New-PSSession -computername VMNAME -credential $cred

#Tried different other ways with no luck.

任何人都可以帮助我。如果还有其他选择,我真的很乐意尝试一下。提前致谢

更新1:我可以提出的两个最新内容就像将执行命令放入BAT/PS1文件(在VM上)一样,而不是从Invoke-VMScript CMDLET调用BAT/PS1文件。 /p>

方案1 :我创建一个包含以下脚本的PS1文件:

Set-Location "D:\folder\path\to\ConsoleAppName"
$cmd = "cmd /c `"ConsoleAppName.exe"`"

Write-Output "Script execution started"
Invoke-Expression -Command $cmd >> TestOutput.txt
Write-Output "Script executed"

并且我称下面的cmdlet

Invoke-VMScript -VM VMName -GuestUser $(User_ADMIN) -GuestPassword "$(ADMIN_PASSWORD)" -ScriptText { PowerShell -NoProfile -ExecutionPolicy Bypass -Command "D:\Path\to\ps1file.ps1" }

方案2 :我创建一个像上面的PS1文件,并且包含一个包含的蝙蝠文件:powershell -noprofile -executionPolicy旁路-command“ d:\ path \ to \ ps1file.ps1”。多于以下内容使用Invoke-vmscript:

Invoke-VMScript -VM VMName -GuestUser $(User_ADMIN) -GuestPassword "$(ADMIN_PASSWORD)" -ScriptText { cmd /c D:\Path\to\bat\file.bat }
#I can use Invoke-Item instead of cmd /c but no luck

方案3 :psexec方法

D:\NewFolder\PAExec\psexec.exe \\VMIP -u $(User_ADMIN) -p "$(ADMIN_PASSWORD)" -i -s cmd /c D:\folder\path\to\ConsoleAppName\ConsoleAppName.exe

psexec错误:

PsExec v2.34 - Execute processes remotely
...
The handle is invalid.
Connecting to xx.xxx.xx.xxx...

Couldn't access xx.xxx.xx.xxx:
Connecting to xx.xxx.xx.xxx...
##[error]Cmd.exe exited with code '6'. 

I've searched and tried different variations of commands, but none seems to work for me. I believe I have also gone through all the SO references on this.

Tried all the ways here: https://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx

My goal is to run an exe that already resides on a remote VM. I have created and released a Console application (in C#). Its more like a double click, I simply double click on the exe file and it does it tasks. I've tried different ways of using them, but I can't seem to get what I want.

Methods tried:

  1. Invoke-VMScript
  2. Win32_Process.Create()
  3. Invoke-Command
  4. Start-Process
  5. psexec
  6. Invoke-WmiMethod
  7. [diagnostics.process]::start

Few of the tests:

Invoke-VMScript -VM VMName -GuestUser $(User_ADMIN) -GuestPassword "$(ADMIN_PASSWORD)" -ScriptText { D:\Path\to\ConsoleAppName.exe }

Invoke-VMScript -VM VMName -GuestUser $(User_ADMIN) -GuestPassword "$(ADMIN_PASSWORD)" -ScriptText { cmd /c D:\Path\to\ConsoleAppName.exe }

#This ps1 file will in turn contain command to run the exe file. Tried same with bat as well
Invoke-VMScript -VM VMName -GuestUser $(User_ADMIN) -GuestPassword "$(ADMIN_PASSWORD)" -ScriptText { PowerShell -NoProfile -ExecutionPolicy Bypass -Command "D:\Path\to\ConsoleAppPS.ps1" } 

Invoke-Command -ComputerName VMName -ScriptBlock { & "D:\Path\to\ConsoleAppName.exe" }
#Tried $session = New-PSSession -computername VMNAME -credential $cred

#Tried different other ways with no luck.

Can anyone please help me with this. If there is any other alternative I would really love to try that. Thanks in advance

UPDATE 1: The two latest stuff that I can come up with is something like putting the execution command into a bat/ps1 file (on the VM) and than calling that bat/ps1 file from the Invoke-VMScript cmdlet.

Scenario 1: I create a ps1 file containing the below script:

Set-Location "D:\folder\path\to\ConsoleAppName"
$cmd = "cmd /c `"ConsoleAppName.exe"`"

Write-Output "Script execution started"
Invoke-Expression -Command $cmd >> TestOutput.txt
Write-Output "Script executed"

And than I call the below cmdlet

Invoke-VMScript -VM VMName -GuestUser $(User_ADMIN) -GuestPassword "$(ADMIN_PASSWORD)" -ScriptText { PowerShell -NoProfile -ExecutionPolicy Bypass -Command "D:\Path\to\ps1file.ps1" }

Scenario 2: I create a ps1 file just like above and a bat file containing: PowerShell -NoProfile -ExecutionPolicy Bypass -Command "D:\Path\to\ps1file.ps1". Than use the Invoke-VMScript like below:

Invoke-VMScript -VM VMName -GuestUser $(User_ADMIN) -GuestPassword "$(ADMIN_PASSWORD)" -ScriptText { cmd /c D:\Path\to\bat\file.bat }
#I can use Invoke-Item instead of cmd /c but no luck

Scenario 3: psexec way

D:\NewFolder\PAExec\psexec.exe \\VMIP -u $(User_ADMIN) -p "$(ADMIN_PASSWORD)" -i -s cmd /c D:\folder\path\to\ConsoleAppName\ConsoleAppName.exe

Error from psexec:

PsExec v2.34 - Execute processes remotely
...
The handle is invalid.
Connecting to xx.xxx.xx.xxx...

Couldn't access xx.xxx.xx.xxx:
Connecting to xx.xxx.xx.xxx...
##[error]Cmd.exe exited with code '6'. 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文