Showing posts with label Schedule.Service. Show all posts
Showing posts with label Schedule.Service. Show all posts

Friday, 24 September 2010

New-Task.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a scheduled task object. 
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN sample using PowerShell 
  6. .NOTES 
  7.     File Name  : New-Task.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted tot: 
  14.         http://msdn.microsoft.com/en-us/library/aa383665%28VS.85%29.aspx 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\New-Task.ps1
  17.     Time Now       : 9/24/2010 12:43:47 PM 
  18.     Task startTime : 2010-09-24T12:44:17 
  19.     Task endTime   : 2010-09-24T12:48:47 
  20.     Task definition created. About to submit the task... 
  21.   
  22.   
  23.     Name               : Test TimeTrigger 
  24.     Path               : Test TimeTrigger 
  25.     State              : 3 
  26.     Enabled            : True 
  27.     LastRunTime        : 12/30/1899 12:00:00 AM 
  28.     LastTaskResult     : 1 
  29.     NumberOfMissedRuns : 0 
  30.     NextRunTime        : 9/24/2010 12:44:17 PM 
  31.     Definition         : System.__ComObject 
  32.     Xml                : <?xml version="1.0" encoding="UTF-16"?> 
  33.                          <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"> 
  34.                            <RegistrationInfo> 
  35.                              <Author>Thomas Lee</Author> 
  36.                              <Description>Start notepad at a certain time</Description> 
  37.                            </RegistrationInfo> 
  38.                            <Triggers> 
  39.                              <TimeTrigger id="TimeTriggerId"> 
  40.                                <StartBoundary>2010-09-24T12:44:17</StartBoundary> 
  41.                                <EndBoundary>2010-09-24T12:48:47</EndBoundary> 
  42.                                <ExecutionTimeLimit>PT5M</ExecutionTimeLimit> 
  43.                                <Enabled>true</Enabled> 
  44.                              </TimeTrigger> 
  45.                            </Triggers> 
  46.                            <Settings> 
  47.                              <IdleSettings> 
  48.                                <Duration>PT10M</Duration> 
  49.                                <WaitTimeout>PT1H</WaitTimeout> 
  50.                                <StopOnIdleEnd>true</StopOnIdleEnd> 
  51.                                <RestartOnIdle>false</RestartOnIdle> 
  52.                              </IdleSettings> 
  53.                              <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy> 
  54.                              <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries> 
  55.                              <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries> 
  56.                              <AllowHardTerminate>true</AllowHardTerminate> 
  57.                              <StartWhenAvailable>true</StartWhenAvailable> 
  58.                              <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable> 
  59.                              <AllowStartOnDemand>true</AllowStartOnDemand> 
  60.                              <Enabled>true</Enabled> 
  61.                              <Hidden>false</Hidden> 
  62.                              <RunOnlyIfIdle>false</RunOnlyIfIdle> 
  63.                              <WakeToRun>false</WakeToRun> 
  64.                              <ExecutionTimeLimit>PT72H</ExecutionTimeLimit> 
  65.                              <Priority>7</Priority> 
  66.                            </Settings> 
  67.                            <Actions Context="Author"> 
  68.                              <Exec> 
  69.                                <Command>C:\Windows\System32\notepad.exe</Command> 
  70.                              </Exec> 
  71.                            </Actions> 
  72.                            <Principals> 
  73.                              <Principal id="Author"> 
  74.                                <UserId>COOKHAM\tfl</UserId> 
  75.                                <LogonType>InteractiveToken</LogonType> 
  76.                              </Principal> 
  77.                            </Principals> 
  78.                          </Task> 
  79.  
  80.     Task submitted. 
  81. #> 
  82. # Helper Function 
  83. function XMLTIME{ 
  84. Param ( $T) 
  85. $csecond = $t.Second.ToString() 
  86. $cminute = $t.minute.ToString() 
  87. $chour = $t.hour.ToString() 
  88. $cday  = $t.day.ToString() 
  89. $cmonth  = $t.month.ToString() 
  90. $cyear = $t.year.ToString() 
  91.  
  92. $date =  $cyear + "-" 
  93. if ($cmonth.Length -eq 1) { $date += "0" + $cmonth + "-"}  
  94. else                      { $date += $cmonth + "-"} 
  95. if ($cday.length -eq 1)   { $date += "0" + $cday + "T"} 
  96. else                      { $date += $cday + "T"} 
  97. if ($chour.length -eq 1)  { $date += "0" + $chour + ":"} 
  98. else                      { $date += $chour + ":"} 
  99. if ($cminute.length -eq 1){ $date += "0" + $cminute + ":"} 
  100. else                      { $date += $cminute + ":"} 
  101. if ($csecond.length -eq 1){ $date += "0" + $csecond} 
  102. else                      { $date += $csecond} 
  103. # return 
  104. $date 
  105. } 
  106.  
  107. ## Script starts here 
  108.  
  109. # A constant that specifies a time-based trigger. 
  110. $TriggerTypeTime = 1 
  111. # A constant that specifies an executable action. 
  112. $ActionTypeExec = 0    
  113.  
  114. # Create and connect to the service 
  115. $service = New-Object -com schedule.service  
  116. $service.Connect() 
  117.  
  118. # Get a folder to create a task definition in.  
  119. $rootFolder = $service.GetFolder("\") 
  120.  
  121. # The taskDefinition variable is the TaskDefinition object. 
  122. # The flags parameter is 0 because it is not supported. 
  123. $taskDefinition = $service.NewTask(0)  
  124.  
  125. # Define information about the task. 
  126. # Set the registration info for the task by  
  127. # creating the RegistrationInfo object. 
  128. $regInfo = $taskDefinition.RegistrationInfo 
  129. $regInfo.Description = "Start notepad at a certain time" 
  130. $regInfo.Author = "Thomas Lee" 
  131.  
  132. # Set the principal for the task 
  133. $principal = $taskDefinition.Principal 
  134.  
  135. # Set the logon type to interactive logon 
  136. $principal.LogonType = 3 
  137.  
  138. # Set the task setting info for the Task Scheduler by 
  139. # creating a TaskSettings object. 
  140. $settings = $taskDefinition.Settings 
  141. $settings.Enabled = $True 
  142. $settings.StartWhenAvailable = $True 
  143. $settings.Hidden = $False 
  144.  
  145. # Create a time-based trigger. 
  146. $triggers = $taskDefinition.Triggers 
  147. $trigger = $triggers.Create($TriggerTypeTime) 
  148.  
  149. # Trigger variables that define when the trigger is active. 
  150. $time = ([system.datetime]::now).addseconds(30) 
  151. $startTime = XmlTime($time) 
  152.  
  153. $time = ([system.datetime]::now).addminutes(5) 
  154. $endTime = XmlTime($time) 
  155.  
  156. "Time Now       : {0}" -f (Get-Date -display time) 
  157. "Task startTime : {0}" -f $startTime 
  158. "Task endTime   : {0}" -f $endTime 
  159.  
  160. $trigger.StartBoundary = $startTime 
  161. $trigger.EndBoundary = $endTime 
  162. $trigger.ExecutionTimeLimit = "PT5M"    #Five minutes 
  163. $trigger.Id = "TimeTriggerId" 
  164. $trigger.Enabled = $True 
  165.  
  166. # Create the action for the task to execute. 
  167.  
  168. # Add an action to the task to run notepad.exe. 
  169. $Action = $taskDefinition.Actions.Create( $ActionTypeExec ) 
  170. $Action.Path = "C:\Windows\System32\notepad.exe" 
  171.  
  172. "Task definition created. About to submit the task..." 
  173.  
  174. # Register (create) the task. 
  175. $rootFolder.RegisterTaskDefinition("Test TimeTrigger", $taskDefinition, 6,"" ,"" , 3) 
  176.  
  177. # all done! 
  178. "Task submitted."