Powershell: Get-ScheduledTask state

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 :(

February 5th, 2015 10:15pm

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

Free Windows Admin Tool Kit Click here and download it now
February 5th, 2015 10:25pm

In the first example, I used -eq and script not work
February 5th, 2015 10:32pm

Sorry, I don't understand your question then.
Free Windows Admin Tool Kit Click here and download it now
February 5th, 2015 10:35pm

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
February 5th, 2015 10:50pm

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

Free Windows Admin Tool Kit Click here and download it now
February 5th, 2015 10:56pm

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
February 6th, 2015 8:49am

Simple usually wins the day.

Good luck.

Free Windows Admin Tool Kit Click here and download it now
February 6th, 2015 8:56am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics