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

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.

Related

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.

Running .ps1 file how do I bypass the 'Do you want to allow this app to make changes to your device?' UAC Windows Powershell Message?

I run a .bat file to trigger the .ps1 file and it works fine. If the spooler is not running it will start it up. But in executing the file it brings up the UAC dialog box asking if 'I want to allow changes'. The file must run without interaction as it will be called from a scheduler every few minutes. FYI starting the Spooler service is just a test as it will monitor a different service. It was my understanding that the -NoProfile would bypass the UAC dialog box.
Bat File...
#ECHO OFF
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"
ps1 File...
$Service = Get-Service -Name spooler
if ($Service.Status -ne "Running")
{
(Get-Date).ToString() + " - Service stopped." >> C:\Scripts\log.txt
Start-Service spooler
}

Run powershell commands from cmd / batch

I would like to run the following as administrator:
PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command
.\Get-WindowsAutoPilotInfo.ps1 -ComputerName $env:computername
-OutputFile .\computers.csv -append
I would like to simply double click on a .cmd or .bat file and have it invoke the Powershell script as administrator. Here's what I have:
PowerShell "SL -PSPath '%CD%'; $Path = (GL).Path; SL ~; Start
PowerShell -Verb RunAs -Args \"-ExecutionPolicy Unrestricted -Noexit"
SL -PSPath '"$Path"'; & '".\UninstallBloatware.ps1" "-ComputerName
$env:computername" "-OutputFile .\computers.csv" "-append"' "\""
I copied most of the code above from somewhere I can't remember. I don't know enough about quotes structure to know how to fix this. Any ideas what I'm doing wrong?

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

How to run a script as a different user while staying in current session

In my current environment I have a local admin and domain account. I have created a powershell script which runs well when logged into my domain admin account. However, I wish to adapt the script to prompt the user for credentials and then run the script if they have permission.
The current script imports AD and presents the user with a menu. I would like to prompt the user for credentials then present them with the menu if correct.
I have tried adding:
start-process powershell.exe -argument C:\Users\Auser\Documents\User Leaver script test.ps1 -credential ""
I know this is not the correct method to verify then run the script and other parameters are needed.
An excerpt from the script is contained in the following:
$Programstop="No" #Used to Loop
Do
{
PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""C:\Users\Auser\Documents\User Leaver script test.ps1""' -Verb RunAs}"; #This line runs script as admin automatically, change file path to point at your script.
Import-Module ActiveDirectory
set-location ad:"dc=fakepleace,dc=local"
Write-Host "
$Credentials = Get-Credentials
Start-Process powershell.exe -argument C:\Users\Auser\Documents\User Leaver script test.ps1 -credential $Credentials -Verb RunAs