0

I'm trying to create a simple scheduled task using powershell v1.0, and as I look through the Task Scheduler object on MSDN http://msdn.microsoft.com/en-us/library/windows/desktop/aa383607%28v=vs.85%29.aspx I'm not sure I really understand what I'm doing...

So far in my code I have the following:

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
   }
} 

But the members assigned to the $firefoxTask don't seem to make sense to me. For example, how do I do something as simple as naming my new task?

Here is the output I received...

   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}   

None of the above members seem to make sense for what I'm trying to do if I drill further into them.

1 Answer 1

1

to create a new task I usually create it manualy on one server then export it to .xml, with the following code you can import it on another server ( i've not verified this is working in V1.0 though)

    $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)
        }
4
  • What are you using to get your $cred values?
    – nullByteMe
    Commented Jul 18, 2013 at 13:29
  • 1
    humm.. get-credential :) $cred=Get-Credential "domain\user" Commented Jul 18, 2013 at 13:30
  • Thanks :-) I'm a bash guy.. so I'm still pretty new to the entire powershell world :-P
    – nullByteMe
    Commented Jul 18, 2013 at 13:32
  • 1
    beware, you gonna like it ! ;) Commented Jul 18, 2013 at 13:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.