Kaveesh
Kaveesh

Reputation: 398

Running a process elevated as a different user

I have a simple powershell script which accepts credentials from the user and executes an exe elevated impersonated as that user.

# Function to run an executable with elevated privileges on a different user
function Run-ElevatedExeOnUser {
    
    $credentials = Get-Credential

    $command = {
        Start-Process -FilePath '"C:\Windows\notepad.exe"' -Verb RunAs
    }

    # Start a new PowerShell process with the credentials and command
    Start-Process powershell.exe -Credential $credentials -ArgumentList "-NoProfile", "-Command", $command

}

Run-ElevatedExeOnUser

This script works as expected on my development environment, however on one of other environments, I get the following error.

Start-Process : This command cannot be run due to the error: The directory name is invalid.
At C:\test\testscript.ps1:17 char:5
+     Start-Process powershell.exe -Credential $credentials -ArgumentLi ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

I have verified that the other user is a part of the administrators group. Any suggestions as to why this could be happening? One funny thing here, if I try execute the exe by Shift+Right Click and use the Run as different user option, the exe launches correctly. What am I missing here?

Upvotes: 1

Views: 48

Answers (0)

Related Questions