8

我有两台服务器:

  • 服务器A(Windows 2003 服务器)
  • 服务器B(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 脚本?

4

2 回答 2

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
}
于 2017-06-29T19:04:27.557 回答
1

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

cd D:\Builds\build5
start-process CMD.EXE -verb runas -argumentlist "-C",".\Deploy.bat"
于 2012-05-24T04:49:28.133 回答