【问题标题】:Run a powershell script on a remote system with elevated permissions to enable remoting在具有提升权限的远程系统上运行 powershell 脚本以启用远程处理
【发布时间】:2016-02-08 02:39:18
【问题描述】:

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

它正在将脚本文件复制到远程系统,但由于 $command 变量为空(下面脚本中的第二行不起作用),它没有在远程 PowerShell 会话中执行命令。

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

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

【问题讨论】:

    标签: powershell


    【解决方案1】:

    使用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"
    

    【讨论】:

    • 这似乎不会将脚本作为 提升 进程启动,这似乎是查询者希望的,基于原始代码。请注意$command 包含-Verb RunAs 参数到Start-Process
    猜你喜欢
    • 1970-01-01
    • 2018-08-03
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-18
    • 2011-04-23
    相关资源
    最近更新 更多