none
Powershell: Get-ScheduledTask state RRS feed

  • Question

  • Hi. I have a problem to use the status of a specific task in task scheduler to send notification if task from "Ready" to "Running" go.

    I did so:

    $TaskName="Test task"
    function GetTaskState() {
    $task=Get-ScheduledTask | Where TaskName -eq "$TaskName"
    $taskstate=$task.State
    }
    $Count=1
    While ($task.State -eq 'Running') {
    TaskProc = GetTaskState; Start-Sleep -Seconds 60 ;$count++
    }
    if (($task.State -eq 'Ready') -and ($Count2 -gt "5")) {
    "Task state is Ready "
    }
    Else {
    "Warning.Task stopped" ; Exit
    }
    
    
    
    


    Just did so:

    do { 
    
    $1 = Get-ScheduledTask | Where TaskName -eq "$TaskName" 
    
    } While ($1.State = 'Ready')

    And get error:

    'State' is a ReadOnly property.
    At line:7 char:10
    + } While ($1.State = 'Ready')
    +          ~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
        + FullyQualifiedErrorId : PropertyAssignmentException

    Help Please, does not work :(

    Thursday, February 5, 2015 7:15 PM

Answers

  • We can also simplify it this way:

    $count=0
    $taskname='Test task'
    While(($task=Get-ScheduledTask $taskname ).State -eq 'Running') {
        $count++
        Start-Sleep -Seconds 60 
    }
    Write-Host ('Task:{0} Count:{1} State:{2}' -f $taskname,$count,$task.State) -ForegroundColor green
    


    ¯\_(ツ)_/¯

    Thursday, February 5, 2015 7:56 PM

All replies

  • You want -eq, not = (= assigns a value, -eq tests for equality).


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Mike Laughlin Thursday, February 5, 2015 7:29 PM
    Thursday, February 5, 2015 7:25 PM
    Moderator
  • In the first example, I used -eq and script not work
    Thursday, February 5, 2015 7:32 PM
  • Sorry, I don't understand your question then.

    -- Bill Stewart [Bill_Stewart]

    Thursday, February 5, 2015 7:35 PM
    Moderator
  • I don't understand either.  I think you have just overcomplicated things.

    Start simple until you underastand how the code works theen add bells and whistles.

    $task=Get-ScheduledTask 'Test task'
    $count=1
    While ($task.State -eq 'Running') {
        Start-Sleep -Seconds 60 
        $task=Get-ScheduledTask 'Test task'
        $count++
    }
    Write-Host ('{0}{1}' -f $count,$task.State) -ForegroundColor green


    ¯\_(ツ)_/¯



    • Edited by jrv Thursday, February 5, 2015 7:51 PM
    Thursday, February 5, 2015 7:50 PM
  • We can also simplify it this way:

    $count=0
    $taskname='Test task'
    While(($task=Get-ScheduledTask $taskname ).State -eq 'Running') {
        $count++
        Start-Sleep -Seconds 60 
    }
    Write-Host ('Task:{0} Count:{1} State:{2}' -f $taskname,$count,$task.State) -ForegroundColor green
    


    ¯\_(ツ)_/¯

    Thursday, February 5, 2015 7:56 PM
  • We can also simplify it this way:

    $count=0
    $taskname='Test task'
    While(($task=Get-ScheduledTask $taskname ).State -eq 'Running') {
        $count++
        Start-Sleep -Seconds 60 
    }
    Write-Host ('Task:{0} Count:{1} State:{2}' -f $taskname,$count,$task.State) -ForegroundColor green


    ¯\_(ツ)_/¯


    Thx! :) What I need. Work!
    Friday, February 6, 2015 5:49 AM
  • Simple usually wins the day.

    Good luck.


    ¯\_(ツ)_/¯

    Friday, February 6, 2015 5:56 AM