Powershell: Invoke a session as Administrator - powershell

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.

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: 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?

How to get elevated access to launch the app pool recycle?

I just used this code (on a ps1 file) to recycle my ApplicationPool:
$WebserverName="MySite"
# Load IIS module:
Import-Module WebAdministration
# Get pool name by the site name:
$pool = (Get-Item "IIS:\Sites\$WebserverName"| Select-Object applicationPool).applicationPool
# Recycle the application pool:
Restart-WebAppPool $pool
But it show me this error:
Import-Module : Process should have elevated status to access IIS
So, i searched on internet and i was able to create this .bat file:
#ECHO OFF
#cd ..
#SET DebugLevel=3
#SET PowerShellScriptPath=.\Header.ps1
#SET CurrentScriptName=%~n0.ps1
#PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList ' -NoProfile -ExecutionPolicy Bypass -File ""%PowerShellScriptPath%"" ""%CurrentScriptName%"" ""%DebugLevel%""' -Verb RunAs}"
#pause
I runned it, logged with the administrator user but it shows me a blue screen (from powershell) and suddenly it stops.
I did something wrong?
Thanks!
Try this:
#SET PowerShellScriptPath=.\Header.ps1
#PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList ' -NoProfile -ExecutionPolicy Bypass -File ""%PowerShellScriptPath%"" ' -Verb RunAs}"
You batch has some extra stuff you don't need.
Ensure that the user running this process has admin rights within the box you're attempting to run this against. This wound up solving the issue for me.