da64u
da64u

Reputation: 21

How to run a program as another user and add arguments in powershell?

We have a program that only updates when being run with the switch /t from an administrator account.

I came up with the CMD prompt version, but I'm new to powershell and having a hard time translating it to Powershell.

The CMD version is:

C:\Windows\System32\runas.exe /savecred /user:ourdomain\ouruseracct "C:\Program Files (x86)\ProjectMatrix\ProjectNotify\ProjectNotify.exe /t"

So far I got:

C:\Windows\System32\runas.exe /user:ourdomain\ouruseracct /savecred "powershell -c start-process -FilePath \"'C:\Program Files (x86)\ProjectMatrix\ProjectNotify\ProjectNotify.exe'\" -verb runAs"

Which runs powershell as admin and starts the program as admin but we need to pass the argument -t or /t to projectnotify.exe when running it.

I believe we need to make use of the -argumentlist but not sure how to word it.

I tried

$t = "-t"
Start-Process -FilePath "C:\Program Files (x86)\ProjectMatrix\ProjectNotify\projectnotify.exe" -ArgumentList $t -Verb runas

Which runs the program but not sure if that's how you pass the argument.

Upvotes: 2

Views: 5500

Answers (2)

i Mr Oli i
i Mr Oli i

Reputation: 755

I know the question asks for arguements, but if you don't them, this works:

Start cmd.exe -Verb RunAs

You can also run this using the 'Run' window or search box:

powershell -command start cmd.exe -verb runas

Upvotes: 1

Maximilian Burszley
Maximilian Burszley

Reputation: 19704

Extra work (troubleshooting):

$Cred = Get-Credential
$ProcInfo = New-Object -TypeName 'System.Diagnostics.ProcessStartInfo'

$ProcInfo.Domain = $Cred.GetNetworkCredential().Domain
$ProcInfo.UserName = $Cred.UserName
$ProcInfo.Password = $Cred.Password
$ProcInfo.FileName = "${Env:ProgramFiles(x86)}\ProjectMatrix\ProjectNotify\ProjectNotify.exe"
$ProcInfo.Arguments = '/t'
$ProcInfo.WorkingDirectory = "${Env:ProgramFiles(x86)}\ProjectMatrix\ProjectNotify"
$ProcInfo.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Normal
$ProcInfo.Verb = 'RunAs'
$ProcInfo.UseShellExecute = $true

[System.Diagnostics.Process]::Start($ProcInfo)

After some more thought, here's a simpler way (in a single command even):

Start-Job -Credential (Get-Credential) -ScriptBlock {
    $Dir = "${Env:ProgramFiles(x86)}\ProjectMatrix\ProjectNotify"

    $StartArgs = @{
        'FilePath' = "$Dir\ProjectNotify.exe"
        'ArgumentList' = '/t'
        'Verb' = 'RunAs'
        'WindowStyle' = 'Normal'
        'WorkingDirectory' = $Dir
        'PassThru' = $true
    }
    Start-Process @StartArgs
} | Wait-Job | Receive-Job

My previous answer is at the bottom of this post now.


References:

Extra reading:

Assuming an on-demand script, you should create a pscredential object if you want to natively run this from powershell:

Launch.cmd

SET "PS=%WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe"
SET "SCRIPT=%SYSTEMDRIVE%\Path\to\wrapper.ps1"

%PS% -NoProfile -NoLogo -ExecutionPolicy Bypass -File "%SCRIPT%"

wrapper.ps1

$Cred = Get-Credential

# To avoid prompting every time:
#
# if (-not (Test-Path -Path '.\mycred.xml')) {
#     Get-Credential | Export-CliXml -Path '.\mycred.xml'
# }
# $Cred = Import-CliXml -Path '.\mycred.xml'

$StartArgs = @{
    'FilePath' = "$PSHOME\powershell.exe"
    'ArgumentList' = '-NoProfile', '-NoLogo', '-File', '.\runas.ps1'
    'Credential' = $Cred
}
Start-Process @StartArgs

runas.ps1

$StartArgs = @{
    'FilePath' = "${Env:ProgramFiles(x86)}\ProjectMatrix\ProjectNotify\ProjectNotify.exe"
    'ArgumentList' = '/t'
    'Verb' = 'RunAs'
}
Start-Process @StartArgs

Upvotes: 1

Related Questions