Summary: Create a scheduled job to run a Windows PowerShell script that creates an HTML Server uptime report
Microsoft Scripting Guy, Ed Wilson, is here. The amount of emails that are generated when attempting to organize and to arrange an event such as PowerShell Saturday is amazing. Every day, it seems, there are nearly a dozen email messages about one detail or another. Windows PowerShell Microsoft MVP, Jim Christopher, is doing an excellent job of keeping things on track, and the Scripting Wife and Brian Wilhite have been hanging right in there with the work. The event truly will be a major big deal, and it will be fun as well as educational. It will be worth the time to register and to attend this event if you are in the Charlotte, North Carolina area on September 15, 2012.
Creating a Scheduled task to run a PowerShell script
The first thing I need to create a scheduled task to run a Windows PowerShell script is the command line that I will execute. The easy way to find this is to use the Run command. At times, I need to know what the command-line switches are for PowerShell.exe. To see these, I open Windows PowerShell and type powershell /? and then I examine the output that displays. The command and the output from the command are shown here.
When I know which switches to use, I practice my command via Run. The following image illustrates using Run to launch Windows PowerShell and to run a Windows PowerShell script. Keep in mind that this will open, and close Windows PowerShell, which is fine for a script producing a report. In testing, I often use the –noexit switch to see any errors arising from the operation.
When I know the command line, I use the Task Scheduler tool, and create a new basic task. First, I need to assign a name and a description. I find it useful to provide a good description as well as a decent name because it facilitates performing maintenance on the task.
The next pane is the Task Trigger pane. It is pretty basic, and self-explanatory. Because I want to create a daily task, I leave that selected. After it is created, it is easy to edit the scheduled task to make it run the task more often, such as every hour if that is the need. One reason I use the Basic Task Wizard is that it is easy to get through the steps needed to create the basic task. I always edit stuff later. The Task Trigger pane is shown here.
Now it is time to set the schedule for the task. In this example, the task runs every morning at 7:00 AM beginning on August 11, 2012.
In the Action pane that follows, I select that we want the scheduled task to Start a program, and then click Next.
In the Start a Program pane, I cheat by placing the command I tested previously from the Run box into the Program/script box. I then click Next. The Start a Program pane is shown here.
Here is where the cheating part comes in to play. I used, directly, the command I tested in the Run box for my program. Rather than attempting to break things up, I simply copied the entire line. The Scheduled Task Wizard is smart enough to know what I wanted to do. It prompts, but it knows. The prompt appears here.
When I have completed the Create Basic Task Wizard, I want to open the task and make a couple of additional changes. The easy way to do this is to select the Open the Properties dialog for this task when I click Finish, as shown here.
Because the task runs on a server, and because one might not be logged on to the server at the time the task is to run, it makes sense to tell the task to run whether or not the user is logged on. This opens a credential dialog, and allows me to set the password for the task. This option appears on the General tab of the scheduled job as shown in the image here.
When I have completed configuring the scheduled task, I always right-click the job and select Run. Then I examine the job history to ensure that the task completed properly. The History tab of the scheduled job is shown here.
Well, that is about all there is to creating a scheduled job to run a Windows PowerShell script. In Windows 8 and Windows Server 2012, there are Windows PowerShell cmdlets to create the scheduled job and to create the job triggers and actions—but that will be the subject of a later Hey, Scripting Guy! Blog post.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy