0
votes

I am using following code for running exe from PowerShell. However, it is throwing the error mentioned in the subject.

$uid = "ABCDomina\builder"
$pwd = "password"
$Args = "-Verb RunAs -Wait -passthru"
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($uid,(ConvertTo-SecureString -String $pwd -AsPlainText -Force))
Start-Process -FilePath C:\windows\system32\system32\notepad.exe -Credential ($cred) -Argumentlist $Args

Error:

Start-Process : This command cannot be run due to the error: Logon failure:
unknown user name or bad password. 
At C:\CD_Clinical\Nightly\DataLabs\Untitled1.ps1:5 char:1
+ Start-Process -FilePath C:\windows\system32\system32\notepad.exe -Credential ($c ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOperationException
     + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.StartProcessCommand
1
Obvious question is obvious: is the password for "ABCDomina\builder" really "password"? Because when I change the username/password to valid ones for my environment (and change the path the notepad) it works fine. Although Norepad did take the contents of $Args as the filename to edit.simon at rcl
Side note: $args is an automatic variable. Don't assign values to it.Ansgar Wiechers

1 Answers

0
votes

Why not use -Credential Get-Credential? it seems a bit pointless as well trying to convert an item to a secure string if you are displaying it plain text, this will mean it gives you a prompt for username and password.

But if thats the route you want to go down then this should work.

$MyCredential=New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $Uid, ($pwd | ConvertTo-SecureString -AsPlainText -Force)