{"id":15921,"date":"2011-01-12T20:10:00","date_gmt":"2011-01-12T20:10:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/01\/12\/schedule-powershell-scripts-that-require-input-values\/"},"modified":"2011-01-12T20:10:00","modified_gmt":"2011-01-12T20:10:00","slug":"schedule-powershell-scripts-that-require-input-values","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/schedule-powershell-scripts-that-require-input-values\/","title":{"rendered":"Schedule PowerShell Scripts that Require Input Values"},"content":{"rendered":"<p><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n<p><b><span style=\"font-size:10.0pt\">Summary:<\/span><\/b><span style=\"font-size:10.0pt\"> Learn how to use the Windows Task Scheduler to run Windows PowerShell scripts that require input values.<\/span><\/p>\n<p><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size:10.0pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Question\" border=\"0\" title=\"Hey, Scripting Guy! Question\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>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 <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/scheduled+tasks\/\">Scheduled Tasks<\/a> 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. <\/span><\/p>\n<p><span style=\"font-size:10.0pt\">&#8212; PS<\/span><\/p>\n<p><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size:10.0pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><span style=\"font-size: 10pt\"><img decoding=\"async\" height=\"34\" width=\"34\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" align=\"left\" alt=\"Hey, Scripting Guy! Answer\" border=\"0\" title=\"Hey, Scripting Guy! Answer\" \/><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span>Hello PS, Microsoft Scripting Guy Ed Wilson here. Well, I was up early this morning and decided to have a <a href=\"http:\/\/blogs.technet.com\/heyscriptingguy\/archive\/2009\/03\/04\/how-do-i-migrate-my-vbscript-wmi-queries-to-windows-powershell.aspx\">coffee day<\/a> 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 <a href=\"http:\/\/blogs.technet.com\/search\/SearchResults.aspx?q=%22conversion+module%22\">conversion module<\/a> seen in the following figure). <\/span><\/p>\n<p><span style=\"font-size:10.0pt\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/6253.HSG-1-13-11-01.jpg\" border=\"0\" \/><\/span><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size:10.0pt\">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 <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/04\/05\/2010-scripting-games-all-links-on-one-page.aspx\">2010 Scripting Games documentation<\/a>. The <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2010\/03\/21\/hey-scripting-guy-march-21-2010.aspx\">Study Guide<\/a> would be an excellent place to start, as would be the <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/scripting+wife\/default.aspx\">Scripting Wife posts<\/a>. There will be some changes (improvements hopefully) and that is what I was contemplating with the early morning ice and caffeine. <\/span><\/p>\n<p><span style=\"font-size:10.0pt\">Anyway, I decided to check the <a href=\"mailto:Scripter@Microsoft.Com\">Scripter@Microsoft.Com<\/a> 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 <b>BiosScriptWithParameter.ps1<\/b> 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 <i>append<\/i> switch for the <b>Out-File<\/b> Windows PowerShell cmdlet, the script will overwrite the text file if it already exists, or create the file if it does not exist. The <b>Out-File<\/b> 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.<\/span><\/p>\n<p><span style=\"font-size:10.0pt\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/6646.HSG-1-13-11-02.jpg\" border=\"0\" \/><\/span><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size:10.0pt\">The complete <b>BiosScriptWithParameter.ps1<\/b> script appears here.<\/span><\/p>\n<p class=\"CodeBlockScreenedHead\" style=\"padding-left: 30px;background: none transparent scroll repeat 0% 0%\"><span style=\"font-size:10.0pt\"><strong>BiosScriptWithParameter.ps1<\/strong><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"padding-left: 30px;background: none transparent scroll repeat 0% 0%\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Param([parameter(Mandatory=$true,<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"padding-left: 30px;background: none transparent scroll repeat 0% 0%\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>HelpMessage=&#8221;Path to file&#8221;)] <\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"padding-left: 30px;background: none transparent scroll repeat 0% 0%\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$path)<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"padding-left: 30px;background: none transparent scroll repeat 0% 0%\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Get-WmiObject -Class win32_bios |<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"padding-left: 30px;background: none transparent scroll repeat 0% 0%\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Out-File -FilePath $path -Encoding ascii<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size:10.0pt\">To create a Scheduled Task that runs the <b>BiosScriptWithParameter<\/b>.ps1, I begin by selecting the <b>Basic Task Wizard<\/b>. The program to run is PowerShell, and the argument to the program looks like the following code.<\/span><\/p>\n<p style=\"margin-left:0.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">-command &#8220;&amp; &#8216;c:\\fso\\BiosScriptWithParameter.ps1&#8217; &#8216;c:\\fso\\myBiosTextFile.txt'&#8221;<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size:10.0pt\">Instead of what I did <a href=\"http:\/\/blogs.technet.comhttps:\/\/devblogs.microsoft.com\/scripting\/use-scheduled-tasks-to-run-powershell-commands-on-windows\/\"><span>yesterday<\/span><\/a> (when I added everything on a single line and let the Task Scheduler sort it out) today I specifically break the commands down. <\/span><\/p>\n<p><span style=\"font-size:10.0pt\">I am passing a value for the <i>command<\/i> 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.<\/span><\/p>\n<p><span style=\"font-size:10.0pt\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/7167.HSG-1-13-11-03.jpg\" border=\"0\" \/><\/span><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size:10.0pt\">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.<\/span><\/p>\n<p class=\"CodeBlockScreenedHead\" style=\"padding-left: 30px;background: none transparent scroll repeat 0% 0%\"><span style=\"font-size:10.0pt\"><strong>ScriptThatAcceptsTwoInputParameters.ps1<\/strong><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"padding-left: 30px;background: none transparent scroll repeat 0% 0%\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Param([parameter(Mandatory=$true,<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"padding-left: 30px;background: none transparent scroll repeat 0% 0%\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\"><span>&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;<\/span>HelpMessage=&#8221;Path to file&#8221;)] <\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"padding-left: 30px;background: none transparent scroll repeat 0% 0%\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$path,<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"padding-left: 30px;background: none transparent scroll repeat 0% 0%\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>[parameter(Mandatory=$true,<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"padding-left: 30px;background: none transparent scroll repeat 0% 0%\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>HelpMessage=&#8221;WMI Class to Query&#8221;)] <\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"padding-left: 30px;background: none transparent scroll repeat 0% 0%\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\"><span>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <\/span>$class)<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"padding-left: 30px;background: none transparent scroll repeat 0% 0%\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Get-WmiObject -Class $class |<\/span><\/span><\/p>\n<p class=\"CodeBlockScreened\" style=\"padding-left: 30px;background: none transparent scroll repeat 0% 0%\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">Out-File -FilePath $path -Encoding ascii<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size:10.0pt\">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.<\/span><\/p>\n<p style=\"margin-left:0.5in\"><span style=\"font-size: 10pt\"><span style=\"font-family: courier new,courier\">-command &#8220;&amp; &#8216;c:\\fso\\ScriptThatAcceptsTwoInputParameters.ps1&#8217; &#8216;c:\\fso\\myBiosTextFile.txt&#8217; &#8216;Win32_bios'&#8221;<\/span><\/span><\/p>\n<p><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size:10.0pt\">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.<\/span><\/p>\n<p><span style=\"font-size:10.0pt\"><img decoding=\"async\" src=\"https:\/\/msdnshared.blob.core.windows.net\/media\/TNBlogsFS\/prod.evol.blogs.technet.com\/CommunityServer.Blogs.Components.WeblogFiles\/00\/00\/00\/76\/18\/0216.HSG-1-13-11-04.jpg\" border=\"0\" \/><\/span><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size:10.0pt\">If the script required three arguments, the process would continue in a similar manner.<\/span><\/p>\n<p><span style=\"font-size:10.0pt\">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 <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/winpsh\/manual\/pipe.mspx\">pipeline<\/a>. <\/span><\/p>\n<p><span style=\"font-size:10.0pt\">I invite you to follow me on <a target=\"_blank\" href=\"http:\/\/bit.ly\/scriptingguystwitter\">Twitter<\/a> or <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a target=\"_blank\" href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a> or post them on the <a href=\"http:\/\/social.technet.microsoft.com\/Forums\/en\/ITCG\/threads\/\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/span><\/p>\n<p><span style=\"font-size:10.0pt\">&nbsp;<\/span><\/p>\n<p><b><span style=\"font-size:10.0pt\">Ed Wilson, Microsoft Scripting Guy<\/span><\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Summary: Learn how to use the Windows Task Scheduler to run Windows PowerShell scripts that require input values. &nbsp; 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 [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,31,22,32,3,4,45],"class_list":["post-15921","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-operating-system","tag-retrieving-input","tag-scheduled-tasks","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>&nbsp; Summary: Learn how to use the Windows Task Scheduler to run Windows PowerShell scripts that require input values. &nbsp; 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=15921"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/15921\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=15921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=15921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=15921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}