Remotely Create a Scheduled Task

Remotely Create a Scheduled Task

Due to a bit of weirdness with about 75 of our Read only Domain controller servers which had been configured with DHCP we needed to routinely perform a flush of the local DNS cache for them to keep replicating and allowing local authentication from clients.

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.

This could be used to schedule any type of scheduled task.

[cc lang=”powershell”]

##############################################################################
#
# Powershell script to create a Scheduled Task that will register DNS on
# local site servers.
#
##############################################################################

$ScriptPath = Split-Path $MyInvocation.InvocationName
$servers = GC $ScriptPath\servers.txt
$onlinelist = @()
$offlinelist = @()
$MaxThreads = 10
$SleepTimer = 500

ForEach($server in $servers){
Start-Job -ScriptBlock {
if(Test-Connection -ComputerName $server -Quiet -Count 2)
{
Write-Verbose -Message “`t$server is up”
$onlinelist += $server
}
else
{
Write-Warning -Message “`t$server is not contactable”
$offlinelist += $server
}
}
}

#Wait for all jobs
Get-Job | Wait-Job
#Get all job results
Get-Job | Receive-Job | Out-GridView

ForEach ($computer in $onlinelist){
$action = New-ScheduledTaskAction -Execute ‘powershell.exe’ `
-Argument ‘-NoProfile -WindowStyle Hidden -command “& {ipconfig /registerdns}”‘
$trigger =  New-ScheduledTaskTrigger -Daily -At 9am

Register-ScheduledTask -Action $action -Trigger $trigger -TaskName “AppLog” -Description “Daily dump of Applog”

}

[/cc]

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *