VMware Cloud Community
Jon2597
Contributor
Contributor
Jump to solution

Some help required amending a script to include a filter

Hi,

I'm new to scripting and would like some help if possible.

I'd like to add a filter to the following script to only bring back results for VMs that have been rebooted in the last 24 hours, currently the script brings back all VM's in my datacentre.  The script also needs a filter to exclude a cluster call "Test Cluster"

The script is as follows.

$LastBootProp = @{
  Name = 'LastBootTime'
    Expression = {
      ( Get-Date ) - ( New-TimeSpan -Seconds $_.Summary.QuickStats.UptimeSeconds )
    }
}
Get-VM | Get-View | select Name, $LastBootProp

Any help or advise would be greatly appreciated.

Thanks.

0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, Jon2597-

You can use the Where-Object cmdlet to filter which items are returned.  To filter on the the items you said (VMs where LastBootTime is less than 24 hours ago, and exclude VMs from the cluster "Test Cluster"), try something like:

## hash table to be used in calculated property
$LastBootProp = @{
    Name
= 'LastBootTime'
    Expression
= {(Get-Date) - (New-TimeSpan -Seconds $_.Summary.QuickStats.UptimeSeconds)}
}

## number of hours to which to compare gap since last boot
$intHoursToGoBack = 24
## get VM .Net View objects where time since last boot is less than 24 hours
Get-Cluster | Where-Object {$_.Name -ne "Test Cluster"} | Get-VM | Get-View -Property Name, Summary.QuickStats.UptimeSeconds | select Name, $LastBootProp | Where-Object {$_.LastBootTime -and ((New-Timespan -End (Get-Date) -Start (Get-Date $_.LastBootTime)).TotalHours -lt $intHoursToGoBack)}

That gets cluster where the cluster name is not equal to "Test Cluster", gets their VMs, gets a .Net View object of the VMs (with a few select properties), and displays the ones where the last boot time is within the last 24 hours.  A bit convoluted vs. just getting VMs whose uptime is less than 24 hours, but should give the same results.  The output would be something like:

Name     LastBootTime
----     ------------
myVM01   6/12/2012 8:21:42 AM
myVM03   6/12/2012 6:50:37 AM
...

Work out for you ok?

View solution in original post

0 Kudos
4 Replies
mattboren
Expert
Expert
Jump to solution

Hello, Jon2597-

You can use the Where-Object cmdlet to filter which items are returned.  To filter on the the items you said (VMs where LastBootTime is less than 24 hours ago, and exclude VMs from the cluster "Test Cluster"), try something like:

## hash table to be used in calculated property
$LastBootProp = @{
    Name
= 'LastBootTime'
    Expression
= {(Get-Date) - (New-TimeSpan -Seconds $_.Summary.QuickStats.UptimeSeconds)}
}

## number of hours to which to compare gap since last boot
$intHoursToGoBack = 24
## get VM .Net View objects where time since last boot is less than 24 hours
Get-Cluster | Where-Object {$_.Name -ne "Test Cluster"} | Get-VM | Get-View -Property Name, Summary.QuickStats.UptimeSeconds | select Name, $LastBootProp | Where-Object {$_.LastBootTime -and ((New-Timespan -End (Get-Date) -Start (Get-Date $_.LastBootTime)).TotalHours -lt $intHoursToGoBack)}

That gets cluster where the cluster name is not equal to "Test Cluster", gets their VMs, gets a .Net View object of the VMs (with a few select properties), and displays the ones where the last boot time is within the last 24 hours.  A bit convoluted vs. just getting VMs whose uptime is less than 24 hours, but should give the same results.  The output would be something like:

Name     LastBootTime
----     ------------
myVM01   6/12/2012 8:21:42 AM
myVM03   6/12/2012 6:50:37 AM
...

Work out for you ok?

0 Kudos
Jon2597
Contributor
Contributor
Jump to solution

Hi Matt,

Thanks for the post, works perfectly.  Smiley Happy

Cheers.

0 Kudos
AlbertWT
Virtuoso
Virtuoso
Jump to solution

Hi Matt,

Many thanks for the script, however it picked up the VM that is already shutdown / turned off as the VM that was just shutdown few sceonds ago as the script run ?

So there may be a false positive with the script result.

/* Please feel free to provide any comments or input you may have. */
0 Kudos
mattboren
Expert
Expert
Jump to solution

Hello, AlbertWT-

Yes, you are welcome.  Ah, interesting.  Sounds like possible lag somewhere (vCenter) in the updating of that "Summary.QuickStats.UptimeSeconds" property of the .Net View object.

To only include VMs that are currently powered on, you could add another Where-Object filter.  Say, after the Get-VM portion of the last line of the script.  So, the last line would be like:

...
Get-Cluster | Where-Object {$_.Name -ne "Test Cluster"} | Get-VM | ?{$_.PowerState -eq "PoweredOn"} | Get-View -Property Name, Summary.QuickStats.UptimeSeconds | select Name, $LastBootProp | Where-Object {$_.LastBootTime -and ((New-Timespan -End (Get-Date) -Start (Get-Date $_.LastBootTime)).TotalHours -lt $intHoursToGoBack)}

Thanks for pointing that out.

0 Kudos