1

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.

TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
Charley Erd
  • 103
  • 1
  • 1
  • 7

1 Answers1

1

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
TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56