7

執行升高遠程腳本我有兩臺服務器:如何遠程PowerShell中

  • serverA的(Windows 2003服務器)
  • serverB上(視窗7)

服務器A包含一個帶有批處理文件(deploy.bat)的文件夾,需要從提升的powershell提示符執行。在ServerA,如果我從正常提示或PowerShell提示符運行它失敗。如果我從提升的提示符運行它,它的作品。 (以管理員身份運行)。

我遇到的問題是,當我嘗試使用遠程PowerShell執行從serverB執行批處理文件。我可以用這個命令來執行:

Invoke-Command -computername serverA .\remotedeploy.ps1 

remotedeploy.ps1的內容是:

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

我都對着計算器講了很多問題:

  • 執行一個遠程powershell(這對我有用)
  • 用提升的提示執行本地PowerShell(我可以做到這一點)

這個問題是關於兩個在同一時間。所以確切的問題是:

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

回答

1

你試圖改變remoteDeploy.ps1與提升的權限啓動CMD.EXE:

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

我想我已經試過這個,但問我一個密碼(而不是一個選項,因爲我需要它在生成腳本中運行),似乎沒有選擇輸入密碼。 我現在再試一次就可以了。 –

+0

它不起作用。當它在本地執行時,窗體提示詢問我的權限,當我遠程執行它時,我會猜想是因爲同一個窗體窗體提示而凍結的。 –

+1

它似乎可以讓我工作,但當我創建我的PSSession時,我使用真實的管理員憑據。 – JPBlanc

1

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