简体   繁体   English

在具有提升权限的远程系统上运行Powershell脚本以启用远程处理

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

I am trying to use the following code to copy a PowerShell script to remote windows 7 machine; 我正在尝试使用以下代码将PowerShell脚本复制到远程Windows 7计算机; 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). 它将脚本文件复制到远程系统,但由于$command变量为空,因此未在远程PowerShell会话中执行$command (以下脚本中的第二行不起作用)。

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, 谢谢,

Using WMI to call CMD to call PowerShell to call Start-Process to call PowerShell again? 使用WMI调用CMD来调用PowerShell来调用Start-Process来再次调用PowerShell? 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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