{"id":74194,"date":"2015-01-18T00:01:00","date_gmt":"2015-01-18T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/01\/18\/weekend-scripter-use-powershell-to-remotely-create-scheduled-task-and-folder\/"},"modified":"2019-02-18T10:35:55","modified_gmt":"2019-02-18T17:35:55","slug":"weekend-scripter-use-powershell-to-remotely-create-scheduled-task-and-folder","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-use-powershell-to-remotely-create-scheduled-task-and-folder\/","title":{"rendered":"Weekend Scripter: Use PowerShell to Remotely Create Scheduled Task and Folder"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to remotely create a scheduled task and folder.<\/span><\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. On Friday in <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-create-scheduled-task-in-new-folder\/\" target=\"_blank\">Use PowerShell to Create Scheduled Task in New Folder<\/a>, I created a pretty long Windows PowerShell script that creates a folder for scheduled tasks, and creates, enables, and configures a scheduled task.<\/p>\n<p>Over the weekend, I received several emails to <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a> that said basically, &ldquo;That is all good, but I need to create the same scheduled task on a hundred servers. I cannot run around to run this script on all of them.&rdquo;<\/p>\n<p>Dude (or dudette), this is actually pretty easy because since Windows PowerShell&nbsp;2.0, Windows PowerShell has had remoting built in. In fact, the server version of the operating system has Windows PowerShell remoting enabled by default.<\/p>\n<p>I do not even need to write a script to run a script. I can do this in one line. Here is that line of code (I could make the command a bit shorter by using aliases and a positional parameter, but it would be even harder to read than it is now):<\/p>\n<p style=\"margin-left:30px\">Invoke-Command -ComputerName (Get-ADComputer -filter * -SearchBase &#039;ou=servers,dc=nwtraders,dc=com&#039;).name -FilePath C:\\fso\\CreateScheduledTaskFolderAndTask.ps1<\/p>\n<p>So what I decided to do was to write a short script that would do this configuration for me&#8230;<\/p>\n<p>The first thing I do is create my search base string. This specifies the organizational unit that I will query. Because I only want these scheduled tasks to be on servers (not on domain controllers or workstations), I query an organizational unit called <b>servers<\/b>. Here is my search base:<\/p>\n<p style=\"margin-left:30px\">$searchBase = &quot;ou=servers,dc=nwtraders,dc=com&quot;<\/p>\n<p>Next I need to specify the path to the script. The cool thing is the script does not need to exist on the remote servers. It does not need to exist on a share. It can exist on my workstation. What happens is that Windows PowerShell reads the script file and converts the script into a script block. Then it executes that script block on the remote servers. So I only need a path that is accessible to my workstation. For this, I use my ubiquitous scratch folder named FSO. Here is the path to my script:<\/p>\n<p style=\"margin-left:30px\">$filepath = &quot;C:\\fso\\CreateScheduledTaskFolderAndTask.ps1&quot;<\/p>\n<p><b>&nbsp; &nbsp;Note&nbsp;<\/b> The script I am executing is the exact script I wrote on Friday. I have not modified it in any way.<\/p>\n<p>I use the <b>Get-ADComputer<\/b> cmdlet to query for all the computers in the <b>servers<\/b><i> <\/i>organizational unit. Here is that line of code:<\/p>\n<p style=\"margin-left:30px\">$cn = Get-ADComputer -Filter * -SearchBase $searchBase<span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n<p><b>&nbsp; &nbsp;Note&nbsp;<\/b> The <b>Get-ADComputer<\/b> cmdlet is available from the Active Directory module. I obtained the Active Directory <br \/>&nbsp; &nbsp;module&nbsp;on my workstation when I installed the Remote Server Administration Tools (RSAT). Versions of RSAT are<br \/>&nbsp; &nbsp;operating system specific.<\/p>\n<p>Now I walk through the collection of computer objects stored in the <b>$cn<\/b> variable, and I test my WinRm connection to the remote machine before I try to run the script. There is a <b>Test-Connection<\/b> cmdlet that sends a <i>ping. <\/i>The problem is that by default, ICMP packets are filtered in Windows Firewall; therefore, <i>ping <\/i>commands will fail. But I wanted to ensure that the remote computer was up and responding, so I use <b>Test-WsMan<\/b> instead. Here is that command:<\/p>\n<p style=\"margin-left:30px\">Foreach ($n in $cn.name)<\/p>\n<p style=\"margin-left:30px\">&nbsp;{If(Test-WSMan -ComputerName $n -EA 0)<\/p>\n<p>Finally, I use the <b>Invoke-Command<\/b> cmdlet to run my script on the remote machines. If the machine is not up (or not responding to <b>Test-WsMan<\/b>), I print the name of the remote computer that was not responding.<\/p>\n<p><b>&nbsp; &nbsp;Note&nbsp;<\/b> This command requires admin rights. I could supply alternate credentials if I needed to.<\/p>\n<p>This could easily be modified to log to a file. Here is that command:<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {Invoke-Command -ComputerName $n -FilePath $filepath}<\/p>\n<p style=\"margin-left:30px\">&nbsp; ELSE {&quot;$n is not available&quot;} }<\/p>\n<p>As shown here, I use Remote Desktop to connect to one of the remote servers to look at the Task Scheduler:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-18-15-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-1-18-15-01.png\" alt=\"Image of menu\" title=\"Image of menu\" \/><\/a><\/p>\n<p>Here is the complete script:<\/p>\n<p style=\"margin-left:30px\">$searchBase = &quot;ou=servers,dc=nwtraders,dc=com&quot;<\/p>\n<p style=\"margin-left:30px\">$filepath = &quot;C:\\fso\\CreateScheduledTaskFolderAndTask.ps1&quot;<\/p>\n<p style=\"margin-left:30px\">$cn = Get-ADComputer -Filter * -SearchBase $searchBase<\/p>\n<p style=\"margin-left:30px\">Foreach ($n in $cn.name)<\/p>\n<p style=\"margin-left:30px\">&nbsp;{If(Test-WSMan -ComputerName $n -EA 0)<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp; {Invoke-Command -ComputerName $n -FilePath $filepath}<\/p>\n<p style=\"margin-left:30px\">&nbsp; ELSE {&quot;$n is not available&quot;} }<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to remotely create a scheduled task and folder. Microsoft Scripting Guy, Ed Wilson, is here. On Friday in Use PowerShell to Create Scheduled Task in New Folder, I created a pretty long Windows PowerShell script that creates a folder for scheduled tasks, and creates, [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[31,57,32,3,4,61,45],"class_list":["post-74194","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-operating-system","tag-remoting","tag-scheduled-tasks","tag-scripting-guy","tag-scripting-techniques","tag-weekend-scripter","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell to remotely create a scheduled task and folder. Microsoft Scripting Guy, Ed Wilson, is here. On Friday in Use PowerShell to Create Scheduled Task in New Folder, I created a pretty long Windows PowerShell script that creates a folder for scheduled tasks, and creates, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74194","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\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=74194"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/74194\/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=74194"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=74194"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=74194"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}