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

时间:2012-05-23 17:00:50

标签: powershell powershell-v2.0 powershell-remoting elevated-privileges

我有两台服务器:

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

ServerA 包含一个包含批处理文件(deploy.bat)的文件夹,需要从提升的PowerShell提示符执行。在 ServerA 中,如果我从正常提示或PowerShell提示符运行它,则会失败。如果我从高架提示运行它可以工作。 (以管理员身份运行)。

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

Invoke-Command -computername serverA .\remotedeploy.ps1

remotedeploy.ps1 的内容为:

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

我在stackoverflow中看了很多关于:

的问题
  • 执行远程PowerShell(这对我有用)
  • 使用提升的提示执行本地PowerShell(我可以这样做)

这个问题同时涉及两个问题。所以确切的问题是:

可以在PowerShell中执行ELEVATED REMOTE脚本吗?

2 个答案:

答案 0 :(得分:2)

如果您正在使用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
}

答案 1 :(得分:1)

您是否尝试更改remoteDeploy.ps1以使用提升的权限启动CMD.EXE:

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