{"id":881,"date":"2018-03-12T21:25:12","date_gmt":"2018-03-12T21:25:12","guid":{"rendered":"http:\/\/ihni.uk\/?p=881"},"modified":"2018-03-12T21:25:12","modified_gmt":"2018-03-12T21:25:12","slug":"remotely-create-a-scheduled-task","status":"publish","type":"post","link":"https:\/\/ihni.uk\/?p=881","title":{"rendered":"Remotely Create a Scheduled Task"},"content":{"rendered":"<p>Due to a bit of weirdness with about 75 of our Read only Domain controller servers which had been configured with DHCP\u00a0we needed to routinely perform a flush of the local DNS cache for them to keep replicating and allowing local authentication from clients.<\/p>\n<p><!--more-->So instead of performing this manually as we had been doing for about 2 weeks on an as needed basis, I created the following script to create a scheduled task to perform this every day at 9am.<\/p>\n<p>This could be used to schedule any type of scheduled task.<\/p>\n<p>[cc lang=&#8221;powershell&#8221;]<\/p>\n<p>##############################################################################<br \/>\n#<br \/>\n# Powershell script to create a Scheduled Task that will register DNS on<br \/>\n# local site servers.<br \/>\n#<br \/>\n##############################################################################<\/p>\n<p>$ScriptPath = Split-Path $MyInvocation.InvocationName<br \/>\n$servers = GC $ScriptPath\\servers.txt<br \/>\n$onlinelist = @()<br \/>\n$offlinelist = @()<br \/>\n$MaxThreads = 10<br \/>\n$SleepTimer = 500<\/p>\n<p>ForEach($server in $servers){<br \/>\nStart-Job -ScriptBlock {<br \/>\nif(Test-Connection -ComputerName $server -Quiet -Count 2)<br \/>\n{<br \/>\nWrite-Verbose -Message &#8220;`t$server is up&#8221;<br \/>\n$onlinelist += $server<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\nWrite-Warning -Message &#8220;`t$server is not contactable&#8221;<br \/>\n$offlinelist += $server<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>#Wait for all jobs<br \/>\nGet-Job | Wait-Job<br \/>\n#Get all job results<br \/>\nGet-Job | Receive-Job | Out-GridView<\/p>\n<p>ForEach ($computer in $onlinelist){<br \/>\n$action = New-ScheduledTaskAction -Execute &#8216;powershell.exe&#8217; `<br \/>\n-Argument &#8216;-NoProfile -WindowStyle Hidden -command &#8220;&amp; {ipconfig \/registerdns}&#8221;&#8216;<br \/>\n$trigger =\u00a0 New-ScheduledTaskTrigger -Daily -At 9am<\/p>\n<p>Register-ScheduledTask -Action $action -Trigger $trigger -TaskName &#8220;AppLog&#8221; -Description &#8220;Daily dump of Applog&#8221;<\/p>\n<p>}<\/p>\n<p>[\/cc]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Due to a bit of weirdness with about 75 of our Read only Domain controller&hellip;<\/p>\n","protected":false},"author":1,"featured_media":988,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[19,11],"tags":[27,30,31,73],"class_list":["post-881","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-powershell","category-scripts","tag-dhcp","tag-dns","tag-domain-controller","tag-scheduled-task"],"jetpack_featured_media_url":"https:\/\/ihni.uk\/wp-content\/uploads\/2020\/02\/Powershell.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/posts\/881","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=881"}],"version-history":[{"count":0,"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/posts\/881\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=\/wp\/v2\/media\/988"}],"wp:attachment":[{"href":"https:\/\/ihni.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=881"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=881"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihni.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=881"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}