How to remote execute an ELEVATED remote script in PowerShell

后端 未结 2 1894
醉酒成梦
醉酒成梦 2021-02-09 10:44

I have two servers:

  • serverA (windows 2003 server)
  • serverB (windows 7)

ServerA contains a

相关标签:
2条回答
  • 2021-02-09 11:25

    If you're using PowerShell 4, you can execute the command using Desired State Configuration, which run as 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
    }
    
    0 讨论(0)
  • 2021-02-09 11:31

    Do you try to change remoteDeploy.ps1 to start CMD.EXE with elevated rights :

    cd D:\Builds\build5
    start-process CMD.EXE -verb runas -argumentlist "-C",".\Deploy.bat"
    
    0 讨论(0)
提交回复
热议问题