繁体   English   中英

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

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

我正在尝试使用以下代码将PowerShell脚本复制到远程Windows 7计算机; 在此计算机上以提升的特权运行它以启用该系统上的远程处理。

它将脚本文件复制到远程系统,但由于$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

有可能做到这一点吗? 谢谢,

使用WMI调用CMD来调用PowerShell来调用Start-Process来再次调用PowerShell? 这似乎有点复杂。

尝试一些简单得多的方法:

$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