Mon - Fri: 9:00 - 19:00 / Closed on Weekends

Create a Task Scheduler Job by using SCCM

In this post, I am going to create a task scheduler job that runs a powershell file (register_dns.ps1) on a daily bases on SCCM clients. First, I created 3 files and copy these files into a folder where my sccm packages reside.

copyFiles.bat: This batch file first creates a folder named "register_dns" on client computers. Then it copies "register_dns.ps1" and "createScheduledTask.ps1" into the folder we created. Finally, this batch file calls powershell and runs createScheduledTask.ps1 to create the Task Scheduler Job.

::First create the register_dns folder on clients
md C:\register_dns
::Then we copy files into the folder we created
xcopy "%~dp0register_dns.ps1" "c:\register_dns" /Y /E /C /Q /H /S
xcopy "%~dp0createScheduledTask.ps1" "c:\register_dns" /Y /E /C /Q /H /S
::run createScheduleTask.ps1 to create the Task scheduler Job
Powershell.exe -executionpolicy ByPass -File  "C:\register_dns\createScheduledTask.ps1"

 

createScheduledTask.ps1: As I explained above, this file creates a new Task Scheduler Job. The new task name is "Register_DNS". The task will be run as the SYSTEM user. Task Scheduler will runs this task everyday at 9am and this task basically runs a powershell script named "register_dns.ps1".

$Action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-ExecutionPolicy Bypass -File "C:\register_dns\register_dns.ps1"'
$Trigger =  New-ScheduledTaskTrigger -Daily -At 9am
Register-ScheduledTask -Action $action -Trigger $Trigger -TaskName "Register_DNS" -User "NT Authority\SYSTEM" -Description "Runs to register computers to the DNS"

 

register_dns.ps1: This file is mainly created to re-register to the DNS. After registeration is trigger, it logs the date and time of the last registration to a text file.

ipconfig /registerdns
$currentTime = Get-Date
$checkFileExists = Test-Path C:\register_dns\registerDNSLog.txt
if($checkFileExists)
{
    Set-Content C:\register_dns\registerDNSLog.txt 'PS Command ran successfully at ' , $currentTime
}
else
{
New-Item c:\register_dns\registerDNSLog.txt
Set-Content C:\register_dns\registerDNSLog.txt 'PS Command ran successfully at ' , $currentTime
}

 

Now we can create a SCCM Package. 

Software Library > Application Management > Packages > Create a Package>Browse and select the path for source folder >Standard Program > Specify information about the program like below and complete the package creation wizard.

 

 

 Now we only need to distribute the package to Distribution Point and Deploy to a SCCM Collection. I assume you already know those steps.

After my test computer recieves the packages I can see the task scheduler job is created.

 

The log file is updated each time the task scheduler job runs.

 

Thanks for reading.

 

 

 

  • Hits: 1177