How to Run Powershell script as admin using code? - powershell

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

Related

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.

How to pass arguments to PowerShell script executed as administrator from batch script?

I have a PowerShell script that needs to be executed using Windows' Batch Script as Administrator. The script is running without arguments, however when I pass arguments, the PowerShell window pops up and closes instantly. Here is what I have tried.
Batch script
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell '-NoProfile -
ExecutionPolicy Bypass -File \"D:\batch_scripting\test2.ps1" -FolderPath \"test"' -Verb
RunAs"
Powershell
param
(
[string]$FolderPath ='D:\batch_scripting'
)
echo $FolderPath
pause
I will add further functionalities in these scripts later. But I have to figure out this first.
The script can be executed if -FolderPath argument is not passed.
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell '-NoProfile -
ExecutionPolicy Bypass -File \"D:\batch_scripting\test2.ps1"' -Verb
RunAs"
I have also gone through following questions but this does not work for me.
I found the solution, although it's weird.
When you execute the powershell script as Administrator, trailing "/" must be added to the path of the script.
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "Start-Process PowerShell 'NoProfile - ExecutionPolicy Bypass -File \"D:\batch_scripting\test2.ps1\"' -Verb RunAs"
It works fine now.

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

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?

Syntax error when starting powershell from bat file

I want to start a powershell script with RunAs from a bat file. This works.
#echo
SET "InstallerFolder=\\dc01\e\script"
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%InstallerFolder%\Script.ps1""' -Verb RunAs}";
But if i add:
-RedirectStandardOutput ""%InstallerFolder%\node.txt""
It breaks.
So the line looks like this:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-RedirectStandardOutput ""%InstallerFolder%\node.txt"" -NoProfile -ExecutionPolicy Bypass -File ""%InstallerFolder%\TSM Client Install Script.ps1""' -Verb RunAs}";
And resuslts in an powershell error which is gone so fast i can't see it.
Probably syntax?
Help much appreciated!
Thanks.
You get an error because powershell.exe does not have a -RedirectStandardOuptut parameter. (See Technet).
Also your syntax is way off (but since i dont see any reason to start powershell to start powershell again i wont bother explaining the syntax errors).
If you want to use RunAs from the cmd use it directly. For more info see Technet (again).
Also you can redirect output in Batch Files with > or >> if you want to append.