简体   繁体   English

如何在PowerShell中远程执行ELEVATED远程脚本

[英]How to remote execute an ELEVATED remote script in PowerShell

I have two servers: 我有两台服务器:

  • serverA (windows 2003 server) serverA (Windows 2003服务器)
  • serverB (windows 7) serverB (Windows 7)

ServerA contains a folder with a batch file (deploy.bat) that needs to be executed from an elevated powershell prompt. ServerA包含一个文件夹,其中包含需要从提升的PowerShell提示符执行的批处理文件(deploy.bat)。 In ServerA , if I run it from a normal prompt or powershell prompt it fails. ServerA中 ,如果我从正常提示或PowerShell提示符运行它,它将失败。 If I run it from an elevated prompt it works. 如果我从高架提示运行它可以工作。 (run as administrator). (以管理员身份运行)。

The problem I have is when I try to execute batch file from serverB using a remote powershell execution. 我遇到的问题是当我尝试使用远程PowerShell执行从serverB执行批处理文件时。 I am able to execute with this command: 我能够使用此命令执行:

Invoke-Command -computername serverA .\remotedeploy.ps1

The content of remotedeploy.ps1 is: remotedeploy.ps1的内容是:

cd D:\Builds\build5
.\Deploy.bat

I have looked a lot questions in stackoverflow about: 我在stackoverflow中看了很多关于的问题:

  • Execute a remote powershell (This works for me) 执行远程PowerShell(这适用于我)
  • Execute a local powershell with elevated prompt (I can do it) 使用提升的提示执行本地PowerShell(我可以这样做)

This question is about both at the same time. 这个问题同时涉及两个问题。 So the exact question is: 所以确切的问题是:

Is possible to execute an ELEVATED REMOTE script in PowerShell? 可以在PowerShell中执行ELEVATED REMOTE脚本吗?

If you're using PowerShell 4, you can execute the command using Desired State Configuration, which run as SYSTEM : 如果您使用的是PowerShell 4,则可以使用Desired State Configuration执行命令,该命令以SYSTEM身份运行:

Invoke-Command -ComputerName ServerA -ScriptBlock {
    configuration DeployBat
    {
        # DSC throws weird errors when run in strict mode. Make sure it is turned off.
        Set-StrictMode -Off

        # We have to specify what computers/nodes to run on.
        Node localhost 
        {
            Script 'Deploy.bat'
            {
                # Code you want to run goes in this script block
                SetScript = {
                    Set-Location 'D:\Builds\build5'
                    # DSC doesn't show STDOUT, so pipe it to the verbose stream
                    .\Deploy.bat | Write-Verbose
                }

                # Return $false otherwise SetScript block won't run.
                TestScript = { return $false }

                # This must returns a hashtable with a 'Result' key/value.
                GetScript = { return @{ 'Result' = 'RUN' } }
            }
        }
    }

    # Create the configuration .mof files to run, which are output to
    # 'DeployBot\NODE_NAME.mof' directory/files in the current directory. The default 
    # directory when remoting is C:\Users\USERNAME\Documents.
    DeployBat

    # Run the configuration we just created. They are run against each NODE. Using the 
    # -Verbose switch because DSC doesn't show STDOUT so our resources pipes it to the 
    # verbose stream.
    Start-DscConfiguration -Wait -Path .\DeployBat -Verbose
}

Do you try to change remoteDeploy.ps1 to start CMD.EXE with elevated rights : 您是否尝试更改remoteDeploy.ps1以使用提升的权限启动CMD.EXE:

cd D:\Builds\build5
start-process CMD.EXE -verb runas -argumentlist "-C",".\Deploy.bat"

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

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