Powershell: Run script as administrator causes infinite execution - powershell

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'"

Related

Running a powershell script as administrator and minimized

So I have set up a task on task scheduler to run a .bat file that runs a powershell script as admin which sets the DNS settings. I figured out how to make the .bat file run minimised, but the powershell window still pops up. Here is the script for the .bat file called "SetDNS". The powershell script's name is "DNS.ps1".
#ECHO OFF
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%DNS.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%C:\Users\Test\Downloads\DNS.ps1%""' -Verb RunAs}";
I want to change it so that the powershell script does not flash open while it runs. Is there something that I could add to the above code to make it run minimized? I tried to add "start /min" to the above code but it did not work. Help is appreciated.

How to Run Powershell script as admin using code?

I have created a PowerShell Script which is on the DC, however I when I run commands from my script it is not running as Admin.
Below is the code I am using to run the code as admin, but this does not seem to be working as I have to manually open PowerShell as Admin then Browse to the Script. Can someone please check the below and give any suggestions.
PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""C:\Scripts\SuperScript.ps1""' -Verb RunAs}";

Powershell Start-Process works but not from .ps1 script

If I paste this into Powershell blue window it runs fine and launches the program
Start-Process “C:\Program Files (x86)\Engine Pro\engine.exe” -ArgumentList "#21#”;
but if I try to run the same command in a script, run.ps1 script, that launches from a scheduled task in Windows, it does nothing
PowerShell.exe -windowstyle hidden -NoProfile -ExecutionPolicy Bypass C:\run.ps1
Does it have something to do with the -ExecutionPolicy Bypass? Do I have to have an Execution policy in the script as well? I really don't know what that is. I know what -windowstyle hidden is but -NoProfile -ExecutionPolicy Bypass I'm not sure why that is there, just found it on another page, but it's all working except for the program launching from within the script.
Thank you.
& Start-Process "C:\Program Files (x86)\Engine Pro\engine.exe" -ArgumentList "#21#";

Run a Powershell script from Batch file with elevated privileges?

I want to run example.ps1 from code.bat
I realize that the start command would run it but require that it be run as admin, how can I do this?
In your batch file:
powershell -Command "&{ Start-Process powershell -ArgumentList '-File C:\folder\psfile.ps1' -Verb RunAs}"
...basically you're using Powershell to run Powershell. The second instance is elevated to Admin and is running your script with those permissions.
Full explanation:
Start-Process can be used to run a program, and also has the parameter -Verb RunAs which elevates the program to run as Admin.
We can't call Start-Process from a batch file as it's a PowerShell command.
But we can run powershell from a batch file, then using the -command parameter to run Start-Process.
We use Start-Process to run powershell (again) and run your script when it is elevated to Admin: -ArgumentList '-File C:\folder\psfile.ps1' -Verb RunAs

Running PowerShell script from batch file as SYSTEM

There are various posts I have seen showing how to run a PowerShell script from a batch file, however I am still not quite sure how to do this with admin rights or running under the system account. I have a batch file that needs to execute a PowerShell script and run the script as admin or the local system account WITHOUT prompting for credentials. This is running on end user machines that do not have the permissions. This is the best I have found for executing a powershell script from a batch file:
#ECHO OFF
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"
PAUSE
But UAC will prompt with the above. Any ideas?