System.DateTime Objects in Powershell/PowerCLI

Just something cool I learned while listening to a technet webcast featuring Sean Kearney (aka the energized tech)  working w/ DateTime objects.  In particular, I was messing w/ pruning vm datastore statistical data.

Example:  Wanted to gather an average disk usage metric from vm’s on a particular datastore, but only for the weekdays between 8 and 5. 

Solution:

   1: #Gets the datastore information
   2: $DS = Get-Datastore Somedatastore 
   3: #Gets VM's associated w/ somedatastore and their disk.usage.average statistic.
   4: $VMStats = $DS | get-vm | get-stat -stat disk.usage.average
   5: #Filters out weekends and only pulls stats between 8am and 5pm.
   6: $VMStats | `
   7: where-object {
   8: $_.timestamp.hour -gt "7" `
   9: -and $_.timestamp.hour -lt "18" `
  10: -and $_.timestamp.dayofweek -notmatch "Saturday|Sunday"
  11: }

Let me tell you, it was awesome finding out the get-stat cmdlet pulls date/time properties back as proper datetime objects.  Thanks again Sean!


[Update: FYI for those who want to know how I determined properties of the timestamp object.  Simply run the script up to step 5, then type $VMStats.timestamp | get-member]

Comments

Popular posts from this blog

NSX-T: Release associated invalid node ID from certificate

NSX-T: vCenter and NSX-T Inventory out of Sync (Hosts in vSphere not showing up in NSX-T)

MacOS: AnyConnect VPN client was unable to successfully verify the IP forwarding table modifications.