Charley Erd
Charley Erd

Reputation: 103

Admin Powershell closing instantly

I'm using a simple script to run another script as admin that will need to change the location of files in areas that are otherwise restricted. I've tried literally every method I could find on stackoverflow for running a script as administrator to no avail.

My 'admin' version of powershell closes instantly when I call it from another script. All I want is to be able to run a script that has adminitrative rights without the thing closing on me instantly when I gave it a script to execute.

Start-Process powershell -Verb runAs
start-process powershell -argument "C:\Scripts\Backup.ps1 TestBackup" -Verb runAs

and the next 6 answers on this page: PowerShell: Running a command as Administrator

I expected to be able to run the passed script as admin, but instead admin powershell pops up and instantly closes.

Upvotes: 1

Views: 751

Answers (1)

TheMadTechnician
TheMadTechnician

Reputation: 36287

I've had issues with relaunching a script as admin, due to execution policy, so I ended up encoding the command and running it that way:

$Code = ". 'C:\Scripts\Backup.ps1' TestBackup"
$Encoded = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes($code))

# Indicate that the process should be elevated
Start-Process PowerShell.exe -Verb RunAs -ArgumentList "-EncodedCommand",$Encoded

Upvotes: 1

Related Questions