0

I'm trying to run a script as another user who as admin right. I'm using the Start-Process command but my code didn't work at all.

My code :

$PassKey = [byte]95,[...],19,20,96,82
$Password=  Get-Content .\PassKey.txt | Convertto-SecureString -Key $PassKey
$User="%COMPUTERNAME%\MyAdmAccount"
$Credentials=New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $User, $Password

Start-process powershell.exe -Argumentlist "C:\Users\test\Desktop\test_echo.ps1" -credential $Credentials

I havn't got any error code during the execution..

Thanks for your help :)

1 Answers1

0

Your $user variable is incorrect, you cannot specify environment variables using %..% syntax. You need to use $env:ComputerName. Like this:

$user = "$($env:ComputerName)\MyAdmAccount"

Even you are specifying your own computer then no need of specifying it:

$user = "MyAdmAccount"

Or for elevated privileges you can use this script found at here: https://superuser.com/questions/108207/how-to-run-a-powershell-script-as-administrator

There is a caveat pointed for Elevated privileges by @mklement0 that -Verb and -Credentials parameter don't go well together. So you need to enclose another Start-Process inside it like this:

Start-Process powershell.exe -Credential $cred -ArgumentList '-noprofile -command "Start-Process cmd.exe -Verb RunAs"'

Wasif
  • 14,755
  • 3
  • 14
  • 34