0

not sure if I'm overcomplicating this, I just want to call another script as local admin and have it run.

The command below opens powershell, and in the new window I get an error and then the window closes too fast for me to be able to see it.

start powershell -verb runas -ArgumentList {Invoke-Expression -Command "C:\Script.ps1"}
4
  • 2
    Add -NoExit to Powershell's parameters. That will keep the process active and you should see the actual error. Commented Oct 12, 2018 at 18:03
  • 1
    most likely something about restrictions on running unsigned scripts, you can probably get past it by adding -ExecutionPolicy Bypass to your arguments. Commented Oct 12, 2018 at 18:08
  • I manually set the execution policy to bypass. -NoExit is a parameter that cannot be found if I enter it after "start powershell" or inside the script block when i call the script Commented Oct 12, 2018 at 19:32
  • Ah, @TheMadTechnician, you were right it was an execution policy issue. For some reason this device hadnt had that run on it Commented Oct 12, 2018 at 20:36

2 Answers 2

1

If you want to stay native powershell use the start-process cmdlet and you can specify the filepath (process to run) as powershell.exe and the -ArgumentList parameters as conditions for your new session. In my example below i'm setting ExecutionPolicy so you don't have to rely on the system level policy, NoProfile will make your script a bit more resiilent by not loading any customized profile on a system.

$Cred = (Get-Credential)
$ScriptLocation = "C:\Temp\TestScript.ps1"
Start-Process -FilePath "powershell.exe" -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File $ScriptLocation" -Credential $Cred

You can see in the script getting a credential object (which you'll probably want to provide so the script will just run) then specifying the script location and executing.

As vonPryz mentioned, you can always troubleshoot by adding -NoExit to your Argument list so the window stays open after executing the script but keep in mind if that if the script location doesn't exist you'll still see the powershell host appear and close right away.

You can also add -WindowStyle Hidden to your argument list to hide any window from appearing at all.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Paul. Thats exactly why my NoExit argument wasn't working. The initial script that starts the next script didn't have the right permissions to access the 2nd script in its current location and it kept closing.
That and the execution policy for whatever reason resets itself when the script finishes running.
0

Turned out to be an execution policy issue. This was a device that was non-standard in our environment and didn't have that GPO pushed.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.