[英]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.