Learn about Windows PowerShell
Summary: Learn how to use the Windows Task Scheduler to run Windows PowerShell scripts that require input values.
Hey, Scripting Guy! I have a Windows PowerShell script that collects information from a computer and writes the results to a text file. I would like to be able to schedule the running of this Windows PowerShell script. The problem is that the Windows PowerShell script requires an input parameter (the path to the text file) and I do not want to re-write the script. In fact, I have another Windows PowerShell script that I will use to create the Scheduled Tasks on a large number of computers in our domain, and the path to the text file will vary depending on the IP subnet upon which the computer resides.
Hello PS, Microsoft Scripting Guy Ed Wilson here. Well, I was up early this morning and decided to have a coffee day today. The snow we had yesterday received a glazing over of rain mixed with ice during the evening, and this morning the front yard is glistening as it reflects the light from the street. It is still cold (20 degrees Fahrenheit -6 degrees Celsius according to my conversion module seen in the following figure).
Actually, the reason I am up early this morning is not to look at the icy snow, nor to drink a cup of clandestine coffee. The reason I am up is to work on the 2011 Scripting Games. The 2011 Scripting Games will be similar to the 2010 Scripting Games and therefore, for scripters that want to get a jump on the competition, they should review the 2010 Scripting Games documentation. The Study Guide would be an excellent place to start, as would be the Scripting Wife posts. There will be some changes (improvements hopefully) and that is what I was contemplating with the early morning ice and caffeine.
Anyway, I decided to check the Scripter@Microsoft.Com email, and PS, I ran across your message. Of course, it is possible to pass parameters to a Windows PowerShell script that is run with a Scheduled Task. The BiosScriptWithParameter.ps1 script is an example of a Windows PowerShell script that obtains information about the bios of a local computer and writes that information to an ASCII text file. It accepts one mandatory parameter which is the path to the text file to hold the output. Because I did not use the append switch for the Out-File Windows PowerShell cmdlet, the script will overwrite the text file if it already exists, or create the file if it does not exist. The Out-File cmdlet is not smart enough to create a folder in the path if it does not exist. Therefore, the script will generate an error if the folder does not exist. This error is shown in the following figure.
The complete BiosScriptWithParameter.ps1 script appears here.
HelpMessage="Path to file")]
Get-WmiObject -Class win32_bios |
Out-File -FilePath $path -Encoding ascii
To create a Scheduled Task that runs the BiosScriptWithParameter.ps1, I begin by selecting the Basic Task Wizard. The program to run is PowerShell, and the argument to the program looks like the following code.
-command "& 'c:\fso\BiosScriptWithParameter.ps1' 'c:\fso\myBiosTextFile.txt'"
Instead of what I did yesterday (when I added everything on a single line and let the Task Scheduler sort it out) today I specifically break the commands down.
I am passing a value for the command parameter of PowerShell.exe (the executable for the Windows PowerShell console). I then use the ampersand inside a pair of quotation marks. The ampersand as used in this line of code is called the invocation operator and it tells Windows PowerShell to interpret the string that follows it as an executable command. The command therefore accepts the first string as the path to the script, and the second string as value for the input parameter (the path) for the script. The thing that makes the command easier to read is the use of the double quotation marks on the outside of the command, and the use of the single quotation marks inside the command. In this manner, the two strings (script and file path) can easily be distinguished. The Scheduled Task action appears in the following figure.
If I have to schedule a task to run a script that uses two input parameters, I add the second parameter after the first parameter. I must use single quotation marks to enclose the string, and place it prior to the closing double quotation marks. An example script that accepts two input parameters is seen here.
HelpMessage="Path to file")]
HelpMessage="WMI Class to Query")]
Get-WmiObject -Class $class |
This time when I create the scheduled task, I will add the value for my second parameter as it appears in this line of code.
-command "& 'c:\fso\ScriptThatAcceptsTwoInputParameters.ps1' 'c:\fso\myBiosTextFile.txt' 'Win32_bios'"
As I work through the wizard, I use the Start a Program option of the wizard, and supply the above command as the argument as seen in the following figure.
If the script required three arguments, the process would continue in a similar manner.
PS, that is all there is to using the Task Scheduler to run Windows PowerShell scripts that require input parameters. Input week will continue tomorrow when I will talk about how to add values to objects through the Windows PowerShell pipeline.
I invite you to follow me on Twitter or Facebook. If you have any questions, send email to me at email@example.com or post them on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy
After hours of scratching my head trying to get Task Scheduler to execute my Powershell script that contained parameters, I came across this example and it worked the first time! Thanks Ed!