We were trying to put together a script which will delete temporary guest wifi accounts automatically. These accounts get configured to be marked as expired after 48 hours automatically. I put together a script to find and delete these accounts, but it kept failing…reason was that I needed to RUN AS ADMIN. Next step was to create a scheduled task, but this kept failing as it too was not running the Powershell script as admin.
So this is the batch file I created in my scheduled tasks to be able to call the Powershell script and run it as admin.
(If your PowerShell script needs to be run as an admin for whatever reason, the 4th line of the batch file will need to change a bit:)

1
2
3
4
@ECHO OFF
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%MyPowerShellScript.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%PowerShellScriptPath%""' -Verb RunAs}";

Reference:
http://blog.danskingdom.com/allow-others-to-run-your-powershell-scripts-from-a-batch-file-they-will-love-you-for-it/

Advertisements

One thought on “MS: Call your PowerShell script from a batch file with Administrative permissions (i.e. Run As Admin)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s