gpt4 book ai didi

powershell - 如何在 PowerShell 中远程执行 ELEVATED 远程脚本

转载 作者:行者123 更新时间:2023-12-04 12:42:34 25 4
gpt4 key购买 nike

我有两台服务器:

  • 服务器A (Windows 2003 服务器)
  • 服务器B (Windows 7)

  • 服务器A 包含一个带有批处理文件 (deploy.bat) 的文件夹,需要从提升的 powershell 提示符下执行。在 服务器A ,如果我从正常提示或 powershell 提示运行它,它会失败。如果我从提升的提示运行它,它就可以工作。 (以管理员身份运行)。

    我遇到的问题是当我尝试从 执行批处理文件时服务器B 使用远程 powershell 执行。我可以用这个命令执行:
    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
    }

    关于powershell - 如何在 PowerShell 中远程执行 ELEVATED 远程脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10724591/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com