Learn about Windows PowerShell
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.
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
it's a great idea that we can use the task scheduler to integrate our powershell jobs!
An easy way to get things done without pressing the button :-)
It might be even more comfortable if we can build schedule directly via PS cmdlets ...
@K_Schulte in Windows PowerShell 3 there are job scheduler cmdlets and it will allow you to create a scheduled task via cmdlets. They work great.
Hi Ed, thanks for another great article.
I need create schedule task programatically using Powershell (PS remoting).
My task execute a powershell script (and other cases a script cmd) inmediately.
$powershellcmd = (get-command powershell.exe).Definition
$upgradeWSPps1 = (Join-Path $ScriptDirectory Tests.LifeCycle.Deploy.MOSS.UpgradeWSP.ps1)
$cmdToRun = "$powershellcmd -ExecutionPolicy Bypass -WindowStyle Hidden -NoLogo -File ""$upgradeWSPps1"" "
Maybe problems with single-quotes, double-quotes..
Any suggestions about it for create task ?
I have got a powershell script which runs just finde when "Run only when user is loggen on" is selected. But when selecting "Run whether user is loggen on or not" the script does not run properly anymore.
The script makes some AD queries and gets me the clusters in our AD. Then it queries the clusters for nodes and some status infos and also queries the nodes for events from failover clustering. Then it puts all in excel and saves the excel to local disk. Then it sends the excel file via smtp with the health of the clusters.
The email is sent, when "Run whether user is loggen on or not" is selected, but no excel file is generated.
Do you have any hints on that? If helpful, I could post the code of the script.
Thanks! Of all the explainations, this one worked for me and it was the simplest as well.
Maybe it's useful for someone:
I was trying to test it and it didn't work for me. I had to pass the name of the file as an argument and without the switch "-file".
I searched it on the Internet and it seems like more people have had the same problem...
Thanks guy that helped me out a bunch.