Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Running PowerShell as another user, and launching a script

I won't get into all the details of why I need this, but users must be able to launch PowerShell as a service account and when PowerShell loads it needs to run a script. I already can launch PowerShell with the stored credentials (stored as a secure string), but for the life of me I cannot get the script (located in $args) to run. I have tried a variety of things, and below is where I am currently. Any help would be greatly appreciated.

$user = "domain\service.account"  $pwd1 = "big long huge string of characters" $pwd = ($pwd1 | ConvertTo-SecureString) $Credential = New-Object System.Management.Automation.PSCredential $user, $pwd $args = "\\domain.local\location\location\location\Script\script.ps1" Start-Process powershell.exe -Credential $Credential -ArgumentList ("-file $args") 
like image 606
Little King Avatar asked Mar 11 '15 14:03

Little King


People also ask

How do I launch a PowerShell script?

In File Explorer (or Windows Explorer), right-click the script file name and then select "Run with PowerShell". The "Run with PowerShell" feature starts a PowerShell session that has an execution policy of Bypass, runs the script, and closes the session.

How do I change user in PowerShell?

When you start a PowerShell script, do it as here: Run PowerShell (or ps.exe) and add the script file with -file <script. ps1> as argument. Then, add the desired credentials by clicking the Change User or Group button.

How do I Run a PowerShell script as administrator?

Open the task manager by using the shortcut keys Ctrl+Alt+Del. Explore the File tab and Run a new task by clicking on the option given. In the PowerShell type the same command start-process PowerShell -verb runas and hit enter to run it as administrator.


1 Answers

You can open a new powershell window under a specified user credential like this:

start powershell -credential "" 

enter image description here

like image 53
George Livingston Avatar answered Sep 22 '22 20:09

George Livingston