How to deal with Windows Schedule&Task by PowerShell in Win7

 

It only will work on Vista/2008 & up, please don't run it on  XP/2003.

If you have to deal with the Schedule&Task on the XP/2003, please refer to the http://code.google.com/p/subsoniccms/source/browse/trunk/TaskScheduler/?r=21

The following resolution will running on Vista/2008 & up.

1.New a COM object with "Schedule.Service"
$scheduleTask=New-Object-ComObject"Schedule.Service"

2.Connect to local computer. You also can type remote computer name.
$scheduleTask.Connect("localhost")

3.Get the folder by the specified path.If you want to get the root folder in the Scheduler&Task, the $workingDirectory will be "\".
$rootFolder=$scheduleTask.GetFolder($workingDirectory)

4.Get the task by the task name from the folder specified in the Step 3.
$targetTask=$rootFolder.GetTasks(0)|Where-Object {$_.name-eq$name}

5.Get the xml property from task object.
[xml]$xml=$targetTask.xml

6.Get the logon account.
$principal=$xml.Task.Principals

$actualLogonAccount=$principal.Principal.UserId


7.Get the frequency array.

$triggers=$xml.Task.Triggers

$calendarTriggers=$triggers.CalendarTrigger

$actualFrequency= @()

foreach($calendarTriggerin$calendarTriggers)

{

$actualFrequency+=$calendarTrigger.LastChild.Name

}


8.Get the commands array.

$actions=$xml.Task.Actions

$execs=$actions.Exec

$actualCommands= @()

foreach($execin$execs)

{

$actualCommands+=$exec.Command

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章