2

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.

JensG
  • 13,148
  • 4
  • 45
  • 55
da64u
  • 21
  • 1
  • 3

2 Answers2

1

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
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • Hey, really appreciate the reply. It looks like you understood the question really well. Running that gives an error: Start-Process : This command cannot be run due to the error: The requested operation requires elevation. At C:\Program Files (x86)\ProjectMatrix\ProjectNotify\runas.ps1:7 char:1 + Start-Process @StartArgs + ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [Start-Process], InvalidOperationException + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand – da64u Apr 24 '18 at 15:30
  • Thanks, it runs and asks for credentials then runs both powershell windows with no errors but doesn't run the program. Confused.. – da64u Apr 24 '18 at 16:20
  • When running the simpler one I get cmdlet Get-Credential at command pipeline position 1 Supply values for the following parameters: Id Name PSJobTypeName State HasMoreData Location Command -- ---- ------------- ----- ----------- -------- ------- 1 Job1 BackgroundJob Running True localhost ... but also no program opens. – da64u Apr 24 '18 at 16:22
  • Only GUI. No console. – da64u Apr 24 '18 at 16:22
  • Thanks, it still didn't run but returns: cmdlet Get-Credential at command pipeline position 1 Supply values for the following parameters: Handles NPM(K) PM(K) WS(K) CPU(s) Id SI ProcessName ------- ------ ----- ----- ------ -- -- ----------- 20 3 396 1812 0.03 5512 2 ProjectNotify – da64u Apr 24 '18 at 16:28
  • @da64u OK, so the process **is** launching as your other account, but apparently isn't visible to the account you're launching powershell with. – Maximilian Burszley Apr 24 '18 at 16:28
  • hmm, even when trying my account I don't see it. If I'm on a domain whats the correct format to type in the credentials? I'm trying DOMAIN\USER – da64u Apr 24 '18 at 16:33
  • @da64u Yes, that's the right way. I added more code for you to try now (hopefully this is the silver bullet) – Maximilian Burszley Apr 24 '18 at 16:52
  • Same thing, tried multiple accounts but it doesn't run. Thanks for trying. I've been looking at this all week. – da64u Apr 24 '18 at 17:10
1

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
i Mr Oli i
  • 605
  • 5
  • 12