Running PowerShell script from batch file as SYSTEM - powershell

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?

Related

Powershell script to create some users and renaming some users is working when executing it within the powershell but not from the batch file

I have a PowerShell script which is used to create some users and to rename some users and its working fine when we execute it within the PowerShell (when PowerShell is running as administrator).
I have some requirements and I have to create simple executable file of this PowerShell script so every user could execute it just by double clicking on it.
For this purpose, I have created a .bat file which will execute the PowerShell script with admin rights but it's not working by double clicking the .bat file.
The .bat file is as under:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""C:\Users\aalih\Desktop\Final\first.ps1""' -Verb RunAs}"
pause
On running the .bat file, I could see the following error in the Event Viewer:
Session "PerfDiag Logger" failed to start with the following error: 0xC0000035
Any help to make this .bat file to run the script would be highly appreciated.

Powershell: Invoke a session as Administrator

I am trying to run my script as a ADMIN user and install the certificates, but it only works if I run it from the command line as a Administrator. If I trigger my bat file from the other script it is installing the certificates as a local user.
SET PowerShellScriptPath='<FilePath>\File.ps1'
SET CertPath=%~dp0NewCSC.cer
start PowerShell.exe -ExecutionPolicy Bypass -Command "& {Set-ExecutionPolicy Bypass -Force -Scope LocalMachine}" 
timeout 05
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& %PowerShellScriptPath% %CertPath%"
Is it coz of I am using Powershell.exe to invoke for 2 times for every line its taking it as a local user? please help me out here.

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