none
Powershell get scheduled task jobs on remote computer RRS feed

  • Question

  • Hello guys!

    I couldn't find any answer or question about this, I need to query scheduled jobs on server (2008R2) with name which contains variables.

    I can get list of jobs, but I don't know how to display only that I need.  For example, there is job $something.$RandomText.$RandomNumbers, So I'm trying to filter jobs by name ($something).
    Script which I found is:

    $sched = New-Object -Com "Schedule.Service"
    $sched.Connect('remote server')
    $out = @()
    $sched.GetFolder("\").GetTasks(0) | % {
        $xml = [xml]$_.xml
        $out += New-Object psobject -Property @{
            "Name" = $_.Name
            "Status" = switch($_.State) {0 {"Unknown"} 1 {"Disabled"} 2 {"Queued"} 3 {"Ready"} 4 {"Running"}}
            "NextRunTime" = $_.NextRunTime
            "LastRunTime" = $_.LastRunTime
            "LastRunResult" = $_.LastTaskResult
            "Author" = $xml.Task.Principals.Principal.UserId
            "Created" = $xml.Task.RegistrationInfo.Date
        }
    }

    And one more thing, I'm using windows 7, so I can't use get-SceduledTasks function. 

    Maybe there is some simpler way to do this?

    Best regards,
    Ronald

    Wednesday, April 8, 2015 10:02 AM

Answers

  • Use -like operator

    $sched = New-Object -Com "Schedule.Service"
    $sched.Connect($env:ComputerName)
    $something = 'Server1*'
    $out = @()
    $sched.GetFolder("\").GetTasks(0) | % {
        $xml = [xml]$_.xml
        $out += New-Object psobject -Property @{
            "Name" = $_.Name
            "Status" = switch($_.State) {0 {"Unknown"} 1 {"Disabled"} 2 {"Queued"} 3 {"Ready"} 4 {"Running"}}
            "NextRunTime" = $_.NextRunTime
            "LastRunTime" = $_.LastRunTime
            "LastRunResult" = $_.LastTaskResult
            "Author" = $xml.Task.Principals.Principal.UserId
            "Created" = $xml.Task.RegistrationInfo.Date
        }
    $out | ? {$_.Name -like "$something"}
    }
    In your $something variable use wild card Server1*


    Regards Chen V [MCTS SharePoint 2010]


    • Edited by Chen VMVP Wednesday, April 8, 2015 11:47 AM
    • Marked as answer by RonaldsS Wednesday, April 8, 2015 12:07 PM
    Wednesday, April 8, 2015 11:46 AM
  • THat does not search all tasks.  It only finds tasks in the root folder.

    Use this:

    schtasks /s <remoteserver> /query /FO CSV |convertFrom-Csv |?{$_.TaskName -match 'server'}

    It recurses all tasks and folders


    \_(ツ)_/

    • Marked as answer by RonaldsS Wednesday, April 8, 2015 12:07 PM
    Wednesday, April 8, 2015 12:01 PM

All replies

  • are you looking for scheduled task with specific name ?
    $out | ? {$_.Name -eq 'TechSmith Updater' }
    $sched = New-Object -Com "Schedule.Service"
    $sched.Connect($env:computerName)
    $out = @()
    $sched.GetFolder("\").GetTasks(0) | % {
        $xml = [xml]$_.xml
        $out += New-Object psobject -Property @{
            "Name" = $_.Name 
            "Status" = switch($_.State) {0 {"Unknown"} 1 {"Disabled"} 2 {"Queued"} 3 {"Ready"} 4 {"Running"}}
            "NextRunTime" = $_.NextRunTime
            "LastRunTime" = $_.LastRunTime
            "LastRunResult" = $_.LastTaskResult
            "Author" = $xml.Task.Principals.Principal.UserId
            "Created" = $xml.Task.RegistrationInfo.Date
        }
    $out | ? {$_.Name -eq 'something' }
    }



    Regards Chen V [MCTS SharePoint 2010]


    • Edited by Chen VMVP Wednesday, April 8, 2015 10:09 AM
    Wednesday, April 8, 2015 10:08 AM
  • I tried this, but I don't know why it's not working. Maybe I

    I look for scheduled task with name that contains "$something"


    And Yes, I tried to change from -eq, to -like and -contain.
    • Edited by RonaldsS Wednesday, April 8, 2015 11:02 AM
    Wednesday, April 8, 2015 11:02 AM
  • Just see the values in $out and check the Name. The name may differ.No results will be produced if it;s not eq to your $something

    $sched = New-Object -Com "Schedule.Service"
    $sched.Connect($env:ComputerName)
    $something = 'GoogleUpdateTaskMachineCore'
    $out = @()
    $sched.GetFolder("\").GetTasks(0) | % {
        $xml = [xml]$_.xml
        $out += New-Object psobject -Property @{
            "Name" = $_.Name
            "Status" = switch($_.State) {0 {"Unknown"} 1 {"Disabled"} 2 {"Queued"} 3 {"Ready"} 4 {"Running"}}
            "NextRunTime" = $_.NextRunTime
            "LastRunTime" = $_.LastRunTime
            "LastRunResult" = $_.LastTaskResult
            "Author" = $xml.Task.Principals.Principal.UserId
            "Created" = $xml.Task.RegistrationInfo.Date
        }
    $out | ? {$_.Name -eq "$something"}
    }


    Regards Chen V [MCTS SharePoint 2010]



    • Edited by Chen VMVP Wednesday, April 8, 2015 11:15 AM
    Wednesday, April 8, 2015 11:13 AM
  • Yes, If I write full name, then everything working like it should, but I need to to look for one part of name.
    example, there is 3 jobs with names:

    "server1.25.04.2015"

    "server1.31.04.2015"

    "server2.15.04.2015"

    I want to look up jobs with name "server1", because dates can change etc. 

    Ronald

    Wednesday, April 8, 2015 11:42 AM
  • Use -like operator

    $sched = New-Object -Com "Schedule.Service"
    $sched.Connect($env:ComputerName)
    $something = 'Server1*'
    $out = @()
    $sched.GetFolder("\").GetTasks(0) | % {
        $xml = [xml]$_.xml
        $out += New-Object psobject -Property @{
            "Name" = $_.Name
            "Status" = switch($_.State) {0 {"Unknown"} 1 {"Disabled"} 2 {"Queued"} 3 {"Ready"} 4 {"Running"}}
            "NextRunTime" = $_.NextRunTime
            "LastRunTime" = $_.LastRunTime
            "LastRunResult" = $_.LastTaskResult
            "Author" = $xml.Task.Principals.Principal.UserId
            "Created" = $xml.Task.RegistrationInfo.Date
        }
    $out | ? {$_.Name -like "$something"}
    }
    In your $something variable use wild card Server1*


    Regards Chen V [MCTS SharePoint 2010]


    • Edited by Chen VMVP Wednesday, April 8, 2015 11:47 AM
    • Marked as answer by RonaldsS Wednesday, April 8, 2015 12:07 PM
    Wednesday, April 8, 2015 11:46 AM
  • THat does not search all tasks.  It only finds tasks in the root folder.

    Use this:

    schtasks /s <remoteserver> /query /FO CSV |convertFrom-Csv |?{$_.TaskName -match 'server'}

    It recurses all tasks and folders


    \_(ツ)_/

    • Marked as answer by RonaldsS Wednesday, April 8, 2015 12:07 PM
    Wednesday, April 8, 2015 12:01 PM
  • Thanks! I forgot about *, everything works like charm. Thanks again!

    Ronald

    Wednesday, April 8, 2015 12:08 PM
  • This works too, thanks!
    Wednesday, April 8, 2015 12:11 PM