2013-07-18 58 views
0

我試圖創建使用PowerShell 1.0的簡單的計劃任務,並作爲我期待通過MSDN上http://msdn.microsoft.com/en-us/library/windows/desktop/aa383607%28v=vs.85%29.aspx任務計劃程序對象我不知道我真正明白創建一個新的計劃任務我在做......使用PowerShell 1.0

所以在我的代碼,到目前爲止,我有以下幾點:

Function createFirefoxTask() { 
    $schedule = new-object -com Schedule.Service 
    $schedule.connect() 
    $tasks = $schedule.getfolder("\").gettasks(0) 

    $firefoxTaskExist=$false 

    # Check if the firefox schedule task exists 
    foreach ($task in ($tasks | select Name)) { 
     if($task.name.equals("FirefoxMaint")) { 
     $firefoxTaskExist=$true 
     break 
     } 
    } 

    # Create the scheduled task if it does not exist 
    if($firefoxTaskExist -eq $false) { 
     write-output "Creating firefox task..." 
     $firefoxTask=$schedule.NewTask(0) 
     $firefoxTask | get-member 
    } 
} 

但分配給$firefoxTask成員似乎沒有道理給我。例如,我該如何做一些簡單的命名我的新任務?

這是我收到的輸出...

TypeName: System.__ComObject#{f5bc8fc5-536d-4f77-b852-fbc1356fdeb6} 

Name    MemberType Definition           
----    ---------- ----------           
Actions   Property IActionCollection Actions() {get} {set}   
Data    Property string Data() {get} {set}       
Principal  Property IPrincipal Principal() {get} {set}    
RegistrationInfo Property IRegistrationInfo RegistrationInfo() {get} {set} 
Settings   Property ITaskSettings Settings() {get} {set}    
Triggers   Property ITriggerCollection Triggers() {get} {set}   
XmlText   Property string XmlText() {get} {set} 

以上成員似乎都不是有意義的東西我想,如果我再鑽到他們做。

回答

1

創建一個新的任務,我通常manualy創建一臺服務器上,然後將其導出爲.xml,用下面的代碼,你可以導入到另一臺服務器上(我沒有證實這是工作在V1.0雖然)

$sch = New-Object -ComObject("Schedule.Service") 
    $computername | foreach{ 
     $sch.connect($_) 
     $root=$sch.GetFolder("\") 
     $root.CreateFolder("SMAC") 
     $folder =$sch.GetFolder("\SMAC") 

     Get-childItem -path $task_path -Filter *.xml | %{ 
      $task_name = $_.Name.Replace('.xml', '') #create a task base on the name of the xml file 
      $task_xml = Get-Content $_.FullName 
      $task = $sch.NewTask($null) 
      $task.XmlText = $task_xml 
      $folder.RegisterTaskDefinition($task_name, $task, 6, $cred.UserName, $cred.GetNetworkCredential().password, 1, $null) 
     } 
+0

你用什麼來獲得'$ cred'值? –

+1

赫姆.. GET-憑證:)'$名氣= GET-憑證「域\用戶」' –

+0

感謝:-)我是一個bash的傢伙..所以我還是很新的,整個世界的PowerShell :-P –