VMware Cloud Community
tickermcse76
Contributor
Contributor

Having trouble filtering Get-Task on StartTime

I have a script where I store the start time in a variable $SCRIPT_START, and then I want to call Get-VIEvent and Get-Task and filter on only events that were logged after the script started (ie Get-Task | ? {$_.StartTime -gt $SCRIPT_START}).

The filtering, when run from the script, works fine for Get-VIEvent, but not for Get-Task.  But if I execute the same commands manually filtering on Get-Task works correctly.  Not sure if this is a bug or I'm missing something.  Here are the stripped down code blocks relevant to the issue, all you need to do is substitue in $SERVERS and $CLUS_NAME.  To test, move $SERVERS off of $CLUS_NAME and execute to the script.

$SCRIPT_START = Get-Date

$SERVERS = 'SERVER_A', 'SERVER_B'

$SERVERS


Function Check-vMotion {

Param (
    [Parameter(Mandatory=$TRUE)]$SERVERS,
    [Parameter(Mandatory=$TRUE)]$SCRIPT_START
    )

$vMotion_Count = 0

###$RECENT_TASKS = Get-Task | ? {$_.Name -eq 'RelocateVM_Task' -AND $_.StartTime -gt $SCRIPT_START}
$RECENT_TASKS = Get-Task | ? {$_.StartTime -gt $SCRIPT_START}

Write-Host "recent tasks"

$RECENT_TASKS

$VIEVENTS = Get-VIEvent | ? {$_.CreatedTime -gt $SCRIPT_START}

Write-host "vi events"

$VIEVENTS


    ForEach ($SERVER in $SERVERS) {


        $CHECK = $RECENT_TASKS | ? {$_.ObjectID -eq ((Get-VM $SERVER).Id)}
    

        If ($CHECK.State -eq 'Success' -and $CHECK.PercentComplete -eq '100') {$vMotion_Count++}

    }

$vMotion_Count

}

Start-Sleep -s 3

$VM_CLUS = Get-Cluster SOME_CLUSTER

Move-VM SERVER_A -Destination $VM_CLUS
Move-VM SERVER_B -Destination $VM_CLUS

$vMotion_Count = Check-vMotion $SERVERS $SCRIPT_START

$vMotion_Count

Now if I run this manually it works correctly:

PowerCLI C:\> $SCRIPT_START = (Get-Date).AddMinutes(-5)

PowerCLI C:\> $RECENT_TASKS = Get-Task | ? {$_.StartTime -gt $SCRIPT_START}

PowerCLI C:\> $recent_tasks.count

6

Within the script the value is empty.

Reply
0 Kudos
2 Replies
RvdNieuwendijk
Leadership
Leadership

In your script, you will only return the tasks that happened in the last three seconds. This will probably return nothing most of the time. In the second example, you will return all of the tasks that happened in the last five minutes. This gives you a much bigger chance of getting some output. Try modifying the first line of your script into the following:

$SCRIPT_START = (Get-Date).AddMinutes(-5)

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
tickermcse76
Contributor
Contributor

In your script, you will only return the tasks that happened in the last three seconds

No, all the vMotions execute after $SCRIPT_START is recorded.  The 3 second padding isn't really necessary.  Also both Get-Task and Get-VIEvents capture the events, but only Get-VIEvents filters correctly.  I only used -5 as a safe example to guarantee I would capture the data.

You could just insert a time stamp before the vMotions for confirmation:

Write-Host "script start"

$SCRIPT_START

Start-Sleep -s 3

Write-Host "current time"

Get-Date

$VM_CLUS = Get-Cluster SOME_CLUSTER

Move-VM SERVER_A -Destination $VM_CLUS
Move-VM SERVER_B -Destination $VM_CLUS

Reply
0 Kudos