Run powershell script as local admin

powershell

i am creating a PS script that need to be run on more than 500 machine.
Script will execute via task scheduler at specific interval.

I am not sure what is the best way to execute this script with full elevation. Like script will get disk space details from WMI query.
What should i do? So, my script will not fail on machines due to access rights.

It is not a good practice to put username and password in the powershell script.
So, i am not planning to put credentials in the PS file.
I am not looking for any script just looking for suggestion.

Any advise.

Best Answer

In Task Scheduler, put the admin credentials in there under Security Options on the General tab (select "Run whether user is logged on or not", you will be prompted for password when you save).

Alternatively, if you know the user is going to be an admin, you could probably just tick "Run with highest privileges" but you'll want to test that.

If you're using schtasks there are options to provide username and password (just note these must be provided in plain text) for this purpose and I believe in later versions of PowerShell there are means of managing scheduled tasks but I am not familiar with them. EDIT: PS scheduled tasks run as PS Sessions which run straight into the double-hop authentication issue when trying to remote so schtasks it is...

Related Question