簡體   English   中英

如何在PowerShell中遠程執行ELEVATED遠程腳本

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

我有兩台服務器:

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

ServerA包含一個文件夾,其中包含需要從提升的PowerShell提示符執行的批處理文件(deploy.bat)。 ServerA中 ,如果我從正常提示或PowerShell提示符運行它,它將失敗。 如果我從高架提示運行它可以工作。 (以管理員身份運行)。

我遇到的問題是當我嘗試使用遠程PowerShell執行從serverB執行批處理文件時。 我能夠使用此命令執行:

Invoke-Command -computername serverA .\remotedeploy.ps1

remotedeploy.ps1的內容是:

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

我在stackoverflow中看了很多關於的問題:

  • 執行遠程PowerShell(這適用於我)
  • 使用提升的提示執行本地PowerShell(我可以這樣做)

這個問題同時涉及兩個問題。 所以確切的問題是:

可以在PowerShell中執行ELEVATED REMOTE腳本嗎?

如果您使用的是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
}

您是否嘗試更改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