Admin Powershell closing instantly - powershell

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.

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

Related

Call a powershell script from another powershell script as local admin

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"}
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.
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.

Powershell: Run script as administrator causes infinite execution

I am completely new to Powershell. Found a script online for updating Windows, and want to run it as administrator. Have a script that starts a new session and calls that script from a network share:
PowerShell.exe -noprofile -command "&{Start-Process PowerShell -ArgumentList
'-noprofile -file \\path\to\networkshare\00_WindowsUpdate.ps1' -Verb RunAs}"
The problem is that it keeps running the script and opening new windows infinitely. I have searched the Internet and could not find anything specific to my issue. How do I run my script to call the Windows Update script once and prevent it from executing infinitely?
Why are you using PowerShell, to call PowerShell, to run PowerShell?
In .cmd:
RUNAS /noprofile /user:domain\user "powershell -ExecutionPolicy Bypass -Command '& \\Path\update.ps1'"

Run powershell script from cmd with admin privileges

I am running a powershell script from cmd and I want to run it with admin rights.
powershell -command "&{. D:\Temp\Untitled1.ps1 ; Method1 'argument1'}"
Since I am not using start-process so I cannot use -verb Runas, what are the other work arounds for the same?

Start-Process -wait doesn't work when script is launched from command prompt opened with runas or as a scheduled task

I've got a script that's I want to run as a scheduled task, but it's not doing the thing it's supposed to. I'm trying to call an executable with Start-Process and the -Wait switch before continuing. Line is
Start-Process -FilePath "C:\Pfx Engagement\Admin\Utilities\Backup Restore\BackupRestoreUtil.exe" -ArgumentList "/f "$backup_directory"" -Wait
If I call it from a command prompt, ie:
powershell .\script.ps1
it works. It runs the command and waits for it to finish before moving on. There's more to the script that has to be run after that command is finished. The problem is that when it's a scheduled task, it doesn't wait. Doing some basic troubleshooting, I first tried opening a cmd window with runas using the scheduled task account, named "Scripts." So I run
runas /env /user:Scripts cmd
to open a command prompt window with the task account. From that command prompt, I try again the "powershell .\script.ps1" and this time, it doesn't wait. It runs the command and moves on immediately before the command is finished. So I thought it might be an issue with the "Scripts" account, until I opened a new command prompt with runas Administrator
runas /env /user:Administrator cmd
When I call the script from this Administrator command prompt, the -Wait switch is also ignored, and the script moves along immediately after calling it without waiting for it to finish.
The odd part about this is that when I call it from the command prompt from Administrator account without doing runas, it works. Same account, two different results. Any ideas as to what the hell is going on here, and equally importantly, how to fix it?
OS is Server 2008 R2, running powershell 3.0
Can't tell you why it's doing it, but I think this might work around it:
$proc = Start-Process -FilePath "C:\Pfx Engagement\Admin\Utilities\Backup Restore\BackupRestoreUtil.exe" -ArgumentList "/f "$backup_directory"" -Passthru
do {start-sleep -Milliseconds 500}
until ($proc.HasExited)
The -Passthru switch will make it return a Process object for the process, and you can test that to see when the process has exited.

Run powershell in new window

I would like to run new powershell window with parameters. I was trying to run following:
powershell -Command "get-date"
but everything happens in same console. Is there a simple way to do this?
To open a new PowerShell Window from PowerShell:
Start-Process PowerShell
Or my personal favorite:
Start-Process PowerShell -WindowStyle Maximized
Then you could typeGet-Datewithout having to deal with the -ArgumentList's tendency to close itself. I still haven't found a way to open a new PowerShell process with the -ArgumentList Parameter, without it immediately closing after it runs. For Instance:
Start-Process PowerShell -ArgumentList "Get-Date"
or simply
Start-Process PowerShell -ArgumentList Get-Date
Will Close Immediately after running the process.
In order to get it to wait before closing you could add:
Start-Process PowerShell -ArgumentList 'Get-Date; Read-Host "Press Enter"'
Since the -Wait Parameter seems to do nothing at all in this case.
FYI - PowerShell Suggested Syntax is actually:
Start-Process -FilePath "powershell.exe"
But since PowerShell is a standard Windows Application in the %SystemRoot%\system32 Environment Variables the command line(s) should recognize a simple
Powershell
Command
Use the start command. In a CMD prompt, try:
start powershell -noexit -command "get-date"
For Start/Run (or Win+r) prompt, try:
cmd /c start powershell -noexit -command "get-date"
The -noexit will tell Powershell to, well, not to exit. If you omit this parameter, the command will be executed and you are likely to just see a glimpse of a Powershell window. For interactive use, this is a must. For scripts it is not needed.
Edit:
start is an internal command for CMD. In Powershell it is an alias for Start-Process. These are not the same thing.
As for why the window is black, that's because the shortcut for Powershell.exe is configured to set the background blue.
To call a PowerShell (PS) script in a second terminal window without exiting, you can use a script similar to:
Start-Process PowerShell -ArgumentList "-noexit", "get-date"
or if you need to run another script from a specific location:
Start-Process PowerShell -ArgumentList "-noexit", "-command .\local_path\start_server.ps1"