Thread: Общие вопросы (General Questions)/Start a process in elevated mode and with another user credentials

Start a process in elevated mode and with another user credentials

$pw= convertto-securestring "PASSWORD" -asplaintext –force
$pp = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\user",$pw
$script = "c:\pathtoscript.ps1"
Start-Process powershell -Credential $pp -ArgumentList '-noprofile -command &{Start-Process $script -verb runas}'

Alternatively you can store the credential password in an encrypted file by doing this first, typing in the password, and pressing enter:

read-host -assecurestring | convertfrom-securestring | out-file C:\myencryptedfile.txt

And then the script would look like this:

$pw= get-content C:\myencryptedfile.txt | convertto-securestring
$pp=new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\user",$pw
$script = "c:\pathtoscript.ps1"
Start-Process powershell -Credential $pp -ArgumentList '-noprofile -command &{Start-Process $script -verb runas}'

Passing the $script as a parameter to this should be easy enough and now you have a master script that can run anything just as if you had run Powershell as Administrator.  (This assumes that credential username has administrative privileges)

original source





Re: Start a process in elevated mode and with another user credentials
Another example.
1. Create credentials:
$credential = New-Object System.Management.Automation.PsCredential(".\administrator", (ConvertTo-SecureString "P@ssw0rd" -AsPlainText -Force))

2. Example of the usage (reopen open powershell window without popup):
start-process powershell.exe -Credential $credential -NoNewWindow -ArgumentList "Start-Process powershell.exe -verb runas"