Running PowerShell Scripts As a Scheduled Task
February 27, 2018
Probably the second most frequently asked question I receive about the various scripts is how to run a script as a scheduled task. I asked my most thorough script tester to document how he accomplished this task so a “regular” person could explain the process. This article is how he accomplishes this task using his words and screenshots.
David McSpadden works for a financial institution and uses almost every documentation script available on this site. When he tests a script, he tests every output option and just about every possible combination of parameters. David emails me a copy of every report he runs for every script he tests. Currently, I have over 1,000 sample reports from David.
OK, on to the process David uses to run the Active Directory Health Check script as a scheduled task. The only things I have changed from the email David sent me is blacking out his employer name and letting Grammarly do its thing.
Note: In David’s text, CW is me and MS is my PowerShell mentor Michael B. Smith.
Ok, First off you must have the PowerShell working first. Get all bugs worked out so you can manually execute the ps1 a million times with no errors.
To do this, I have created a scripting environment. C:\tmp\Scripts
In this example, I will show the AD Health Schedule.
Create a batch or dos command script in the c:\tmp\Scripts\AD_Health_Check folder called for lack of a better name pc-adHealth.bat.
This bat file will be used in the scheduled task later.
The bat file is simple and only has the following reference in it:
echo on powershell.exe -NoLogo -File "c:\tmp\scripts\AD_Health_Check\ad health check v2 parms.ps1"
You can see this is calling Webster’s admin script.
The reference PowerShell is as follows:
(What I have tested 1 million times to make sure it is still working.)
cd c:\tmp\scripts\"AD_Health_Check"\ .\"ADHealthCheck_v2.ps1" -CP "Sideline" -CN "David's Employer" -UN "David McSpadden" -All -Log -CSV -Mgmt -Folder \\localhost\c$\tmp\scripts\"AD_Health_Check"\foldertest -PDF
This PowerShell script puts a copy of the PDF output in a static location. I have a script that runs on my SharePoint the goes around and picks up all these admin script outputs and places them in the correct location on the SharePoint server for all the admins to reference.
Now to schedule this, I am only scheduling the bat file we created previously, and I am running the task under my user profile.
Trying to run it under admin remotely did not place nice with me so after much reflection and big thick bat from MS and CW I went with my user profile. (Also a KB article I cannot find again. But MS and CW reviewed it as well.)
I am running these once a week and they haven’t failed yet.
For that matter, can now use my Enterprise Scheduling software SMA Opcon by executing “c:\tmp\scripts\AD_Health_Check\ps-adhealth.bat” as [David’s AD Account] in the context of the Windows machine where the .bat file is hosted. (SMA has a hook on the machine that allowed running in the context of a signed-on user.)
[end of David’s email]
If you have another method for running any of the scripts as a scheduled task, send me an email of how you handle the process and I will add it to this article.
Webster and David