如何远程执行PowerShell中升高远程脚本(How to remote execute an E

2019-07-29 12:33发布

我有两台服务器:

  • serverA的 (Windows 2003服务器)
  • serverB上 (窗口7)

服务器A包含具有需要被从提升的powershell提示执行的批处理文件(DEPLOY.BAT)的文件夹。 在服务器A,如果我从一个正常的提示符下运行或PowerShell提示符下失败。 如果我从提升的提示符下运行它的工作原理。 (以管理员身份运行)。

我的问题是,当我尝试使用远程PowerShell执行从serverB上执行批处理文件。 我可以用这个命令执行:

Invoke-Command -computername serverA .\remotedeploy.ps1

remotedeploy.ps1的内容是:

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

我都对着计算器了很多问题:

  • 执行远程PowerShell(这对我的作品)
  • 执行与升高提示本地的PowerShell(我能做到这一点)

这个问题是关于双方在同一时间。 所以确切的问题是:

可以在PowerShell中执行升高远程脚本?

Answer 1:

你试图改变remoteDeploy.ps1以提升的权限启动CMD.EXE:

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


Answer 2:

如果您使用PowerShell的4,您可以执行使用期望状态配置的命令,它运行的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
}


文章来源: How to remote execute an ELEVATED remote script in PowerShell