Run a powershell script on a remote system with elevated permissions to enable remoting

╄→гoц情女王★ 提交于 2021-01-28 03:41:14

问题


I am trying to use the following code to copy a PowerShell script to remote windows 7 machine; run it with elevated privileges on this machine to enable remoting on that system.

It is copying the script file to the remote system but it is not executing the command in the remote PowerShell session because of the empty $command variable (the second line in the script below is not working).

Copy-Item -Path C:\users\user1\Myscript.ps1 -Destination \\some-computer\c$\Myscript.ps1

$command = PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\Myscript.ps1""' -Verb RunAs > C:\PS-result1.txt}"

$cmd = "CMD.EXE /c "+$command

Invoke-WmiMethod -class Win32_process -name Create -ArgumentList $cmd -ComputerName "some-computer"

Start-Sleep -s 8

Get-Content \\some-computer\C$\PS-result1.txt

Is it possible to accomplish this? Thanks,


回答1:


Using WMI to call CMD to call PowerShell to call Start-Process to call PowerShell again? That seems a little complicated.

Try something much simpler:

$command = "PowerShell.exe ""C:\Myscript.ps1"" > ""C:\PS-result1.txt"""
Invoke-WmiMethod -class Win32_process -name Create -ArgumentList $command -ComputerName "some-computer"


来源:https://stackoverflow.com/questions/35261655/run-a-powershell-script-on-a-remote-system-with-elevated-permissions-to-enable-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!