1

I am attempting to run an executable through PowerShell 1.0 using another user's credentials. My current script is the following:

$username = "adminuser"
$password = "thepassword"
$secstr = ConvertTo-SecureString -String $password -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $secstr
Start-Process powershell.exe -verb runas -Credential $cred -File C:\Users\Public\myexecutable.exe

The error I receive is the following:

Error: Start-Process : This command cannot be run due to the error: The handle is invalid.
At C:\Users\Public\privy2.ps1:8 char:1
 + Start-Process powershell.exe -Credential $cred
     + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
     + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand

When using the command without credentials, the script works and the executable runs:

Start-Process powershell.exe -verb runas -File C:\Users\Public\myexecutable.exe

Any ideas as to why I am getting the The handle is invalid error?

Per .NET Process Start Process Error using credentials (The handle is invalid), I have tried adding redirects but the same error persists.

Community
  • 1
  • 1
  • Why are there two different executable names being passed to Start-Process? I'm kind of surprised that isn't a syntax error, the syntax described in the documentation doesn't seem to allow it. – Harry Johnston Dec 13 '16 at 01:39
  • I want to use PowerShell to run the other executable as a different user. –  Dec 13 '16 at 01:44
  • But you're already *in* PowerShell. Start-Process is a PowerShell command. – Harry Johnston Dec 13 '16 at 01:46
  • That is a good point. Let's take away the second PowerShell invocation. Left with `Start-Process C:\Users\Public\myexecutable.exe -verb runas -Credential $cred`, it still causes the same error. –  Dec 13 '16 at 01:51
  • It's either `-Credential` or `-Verb`. You can't use them both. – Tomer Dec 13 '16 at 10:01
  • @Tomer I have removed -Verb and I still get the same error with `Start-Process C:\Users\Public\myexecutable.exe -Credential $cred`. Is there any reason that PowerShell would block me from running a file as a higher-privileged user? I know the credentials are correct because if I change the password at all, it gives me an invalid username or password error. –  Dec 13 '16 at 12:13
  • http://stackoverflow.com/questions/7319658/start-process-raises-an-error-when-providing-credentials-possible-bug – Tomer Dec 13 '16 at 12:27
  • @Tomer Thanks for responding again. Unfortunately adding the working directory flag does not help: `Start-Process myexecutable.exe -Credential $cred -WorkingDirectory 'C:\Users\Public\'`. The same error persists. It would help if the error given was not so vague. –  Dec 13 '16 at 13:29
  • Works for me... I'd try to use `-Credential (Get-Credential)` and see if something changes... – Tomer Dec 13 '16 at 14:53

1 Answers1

0

You need to open Powershell as an administrtor. Simply right-click Powershell and click Run as an Administrtor. It should work just fine and you won't see The handle is invalid error.

pasignature
  • 577
  • 1
  • 6
  • 13