Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I am not sure how to do this in PowerShell, but I dont think it is supported.

For example:

$tasks = schtasks /query 

Will store all the tasks into the variable $tasks

Example output:

Folder: \Microsoft\Windows\WindowsBackup
TaskName                                 Next Run Time          Status
======================================== ====================== ===============
ConfigNotification                       Disabled

The property "TaskName" cannot be referred to that easily (ie $tasks.TaskName)

Is there some other way to refer to the TaskName property within the $tasks variable, sort of like how you can reference properties in the get-service cmdlet?

$services = gsv
$services | Where-Object { Write-Output $_.Name }
share|improve this question
    
Has something to do with Select-String, but I do not know how to configure it –  nulz May 31 '11 at 8:07
    
added the script to my answer; hope it helps you out –  Matrix Mole Jul 3 '11 at 23:28
add comment

3 Answers

Use the /fo parameter in schtasks to print the output as CSV and then convert the output to a Powershell object with the ConvertFrom-CSV command.

$tasks = schtasks /query /fo CSV | ConvertFrom-CSV

You can then use search for a particular task name as follows

$myTask = $tasks | Where-Object {$_.TaskName -eq "My_Scheduled_Task"}
share|improve this answer
add comment

To do what you're wanting you'll need to build a custom object, and fill the object with the details from the schtasks output. This will give you the ability to do what you're looking for. I did a project at work for this some time back. I'll have access to that code Sunday, and will post it up then.

Here's the Scheduled Task query Script I built, since it was one of my first scripts, it's not very pretty but it should have all the information you need:

function query_tasks
{
    if ($args -eq '/?')
    {
        Write-Output "Usage: query_tasks ComputerName taskname"
        Write-Output "Task name defaults to wildcard search and can be multiple words."
        Write-Output "Computer name, and task names must be included."
    }
    elseif ($args.length -lt 2)
    {
        Write-Output "Error: Must include computer name and partial task name to search for."
    }
    else
    {
        $CompName = $args[0]
        $Tasks = $args[1..($args.length-1)]
        $arrTasks = $(schtasks /query /v /fo csv /s $CompName)
        $taskName = "`"*$Tasks*`""
        $arrTask = $arrTasks -like $taskName -split '","'

        $arrSchTasksAttributes = @( )

        $arrSchTasksAttributes += "HostName"
        $arrSchTasksAttributes += "TaskName"
        $arrSchTasksAttributes += "NextRunTime"
        $arrSchTasksAttributes += "Status"
        $arrSchTasksAttributes += "LastRunTime"
        $arrSchTasksAttributes += "LastResult"
        $arrSchTasksAttributes += "Creator"
        $arrSchTasksAttributes += "Schedule"
        $arrSchTasksAttributes += "TaskToRun"
        $arrSchTasksAttributes += "StartIn"
        $arrSchTasksAttributes += "Comment"
        $arrSchTasksAttributes += "ScheduledTaskState"
        $arrSchTasksAttributes += "ScheduledType"
        $arrSchTasksAttributes += "StartTime"
        $arrSchTasksAttributes += "StartDate"
        $arrSchTasksAttributes += "EndDate"
        $arrSchTasksAttributes += "Days"
        $arrSchTasksAttributes += "Months"
        $arrSchTasksAttributes += "RunAsUser"
        $arrSchTasksAttributes += "DeleteTaskIfNotRescheduled"
        $arrSchTasksAttributes += "StopTaskIfRunsXHoursandXMins"
        $arrSchTasksAttributes += "Repeat_Every"
        $arrSchTasksAttributes += "Repeat_Until_Time"
        $arrSchTasksAttributes += "Repeat_Until_Duration"
        $arrSchTasksAttributes += "Repeat_StopIfStillRunning"
        $arrSchTasksAttributes += "IdleTime"
        $arrSchTasksAttributes += "PowerManagement"


        $arrTaskObj = $null
        $arrTaskObj = New-Object psobject
        for ($t = 0; $t -lt $arrTask.length; $t++)
        {
            Add-Member -InputObject $arrTaskObj -MemberType NoteProperty `
            -Name $arrSchTasksAttributes[$t] -Value $arrTask[$t]
        }

        $listHeaders = @{Expression={$_.HostName};Label="Host Name"}, @{Expression={$_.TaskName};Label="Task Name"}, @{Expression={$_.NextRunTime};Label="Next Run Time"}, @{Expression={$_.LastRunTime};Label="Last Run Time"}, @{Expression={$_.LastResult};Label="Last Result"}, @{Expression={$_.Status};Label="Current Status"}
        $arrTaskObj | Format-List $listHeaders
    }
}

It essentially queries the server for the entire scheduled task list and receives the output as an array. It then searches the output for the line that contains the specific scheduled task. Once that is found, it generates an array of the searched tasks line of data (which has no header information at this point). Then using the header information that scheduled task returns as an array, it builds a custom object with each piece of header information as a property that is then filled with the data from the specific task's array.

Because of the way that the object is created, you could probably tweak this script to allow for the inclusion of multiple scheduled tasks. As it stands, the output is filtered through the $listHeaders variable, but you could strike that and just output the $arrTaskObj object to the pipe which would allow you to call and access the properties for.

share|improve this answer
add comment

I am using VisualCron which is an automation tool like Task scheduler. It has a specific Task for executing Powershell. There are two modes - inprocess which lets you enter PS code in VisualCron and there is also possible to link to an existing PS file.

Either way you can use VisualCron built in Variables to pass information directly into Powershell script.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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