8

I have two servers:

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

ServerA contains a folder with a batch file (deploy.bat) that needs to be executed from an elevated powershell prompt. In ServerA, if I run it from a normal prompt or powershell prompt it fails. If I run it from an elevated prompt it works. (run as administrator).

The problem I have is when I try to execute batch file from serverB using a remote powershell execution. I am able to execute with this command:

Invoke-Command -computername serverA .\remotedeploy.ps1

The content of remotedeploy.ps1 is:

cd D:\Builds\build5
.\Deploy.bat

I have looked a lot questions in stackoverflow about:

  • Execute a remote powershell (This works for me)
  • Execute a local powershell with elevated prompt (I can do it)

This question is about both at the same time. So the exact question is:

Is possible to execute an ELEVATED REMOTE script in PowerShell?

2 Answers 2

2

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
}
1

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"
7
  • I think I have tried this but asked me for a password (not an option as I need it to run in a build script) and seem there is no option for putting a password. I will try again now just to be sure. Commented May 24, 2012 at 8:00
  • It doesn't work. When executing it locally a windows form prompt asking me permission and when I remote execute it, freezes because of same windows form prompt I guess. Commented May 24, 2012 at 9:24
  • 1
    It seems to work form me, but I use the real admin credentials, when I create my PSSession.
    – JPBlanc
    Commented May 24, 2012 at 10:11
  • 1
    What are you using in deploy.bat that need to be elevated. The program should not use UI.
    – JPBlanc
    Commented May 24, 2012 at 10:20
  • I am using a SQLCompare tool. I have managed to stop needing elevated permissions. The UI that appear is the "runas" UI asking me to confirm if I want to run process with elevated permissions. Commented May 24, 2012 at 11:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.