In future posts, I will provide further examples that show the different intervals that you can schedule tasks. This example is a basic task as there are many areas where you can fine tune the task.
# Parameters to modify $taskName = "Export-InterestingInformation.ps1" $taskWorkingDirectory = "C:\PowerShell" $taskAuthor = "ad\myaccount" $taskDescription = "The Monthly Interesting Information Report" $taskSecurityPrincipal = "ad\reporting" $taskSheduledTaskFolder = "\MyTasks" $startTime = (Get-Date "02/15/2011 06:00:00" -Format s) # Would like to use -asSecureString but RegisterTaskDefinition does not accept it # Look over your shoulder before typing $password = Read-Host -prompt "$taskSecurityPrincipal Password" # The meaty parts $taskService = New-Object -ComObject Schedule.Service $taskService.Connect() $rootFolder = $taskService.GetFolder($taskSheduledTaskFolder) $taskDefinition = $taskService.NewTask(0) $registrationInformation = $taskDefinition.RegistrationInfo $registrationInformation.Description = $taskDescription $registrationInformation.Author = $taskAuthor $taskPrincipal = $taskDefinition.Principal $taskPrincipal.LogonType = 1 $taskPrincipal.UserID = $taskSecurityPrincipal $taskPrincipal.RunLevel = 0 $taskSettings = $taskDefinition.Settings $taskSettings.StartWhenAvailable = $true $taskSettings.RunOnlyIfNetworkAvailable = $true $taskSettings.Priority = 7 $taskSettings.ExecutionTimeLimit = "PT2H" $taskTriggers = $taskDefinition.Triggers $executionTrigger = $taskTriggers.Create(4) $executionTrigger.DaysOfMonth = 16384 # http://msdn.microsoft.com/en-us/library/aa380735(v=vs.85).aspx $executionTrigger.StartBoundary = $startTime $taskAction = $taskDefinition.Actions.Create(0) $taskAction.Path = "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe" $taskAction.Arguments = "-command `"$taskWorkingDirectory\$taskName`"" $taskAction.WorkingDirectory = $taskWorkingDirectory # 6 == Task Create or Update # 1 == Password must be supplied at registration $rootFolder.RegisterTaskDefinition($taskName, $taskDefinition, 6, $taskSecurityPrincipal, $password, 1) # Since we captured this in plain text I am going to nuke the value # Not 100% security Clear-Variable -name password
No comments:
Post a Comment