[英]How to remote execute an ELEVATED remote script in PowerShell
我有兩台服務器:
ServerA包含一個文件夾,其中包含需要從提升的PowerShell提示符執行的批處理文件(deploy.bat)。 在ServerA中 ,如果我從正常提示或PowerShell提示符運行它,它將失敗。 如果我從高架提示運行它可以工作。 (以管理員身份運行)。
我遇到的問題是當我嘗試使用遠程PowerShell執行從serverB執行批處理文件時。 我能夠使用此命令執行:
Invoke-Command -computername serverA .\remotedeploy.ps1
remotedeploy.ps1的內容是:
cd D:\Builds\build5
.\Deploy.bat
我在stackoverflow中看了很多關於的問題:
這個問題同時涉及兩個問題。 所以確切的問題是:
可以在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.