Passing admin credentials in Start-Process cmdlet

86 Views Asked by At

I am trying to build a script that installs an application with encrypted local admin credentials on the pc when the user logged in is not an admin.

I am running the below cmdlet:

$User = "[email protected]" 
$PasswordFile = "C:\temp\Cred\Password.txt" 
$KeyFile = "C:\temp\Cred\AES.key" 
$key = Get-Content $KeyFile 
$MyCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, (Get-Content $PasswordFile | ConvertTo-SecureString -Key $key) 

Start-Process -FilePath C:\Temp\7z2301-x64.exe -Credential $MyCredential

However, I am getting below error even though I am passing correct credentials:

Start-Process : This command cannot be run due to the error: The user name or 
password is incorrect.
At line:13 char:1
+ Start-Process -FilePath C:\Temp\7z2301-x64.exe -Credential $MyCredent ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Start-Process], InvalidOp 
   erationException
    + FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.C 
   ommands.StartProcessCommand

Can someone please help on this?

Thank you in advance!

Getting error invalid username and password when running above code.

1

There are 1 best solutions below

12
Martin Iszac On

If the account you’re using doesn’t have the necessary permissions to access the current directory, you might encounter this error.

If you’re running your script as a service, the service might be running under a different account that doesn’t have the necessary permissions.

Try Get-Credential to confirm the credentials are working properly.

$MyCredential = Get-Credential
Start-Process -FilePath C:\Temp\<some_exe>.exe -Credential $MyCredential