VMware Cloud Community
THEL1ZARDKING
Contributor
Contributor
Jump to solution

PowerCLI Not Calculating Year Into Date Statement

Hello,

I have a Custom Attribute for Dates(DD/MM/YYYY) in vCenter and I am attempting to gather VM's prior to that date however my statement does not take seem to take year into account.  So if I set a date in the future e.g. 1/15/2012 the script lists them as prior to current date which is incorrect and any dates beyond the current date but still in 2011 are not shown which is correct.  Any ideas would be greatly appreciated.

$currentdate = Get-Date
$currentdate = $currentdate.ToShortDateString()

Get-VM | Get-Annotation -CustomAttribute "Expires" | ?{$_.Value -le "$currentdate"}

Verions:

vCenter - 4.1.0.12319

PowerCLI - 4.1.1.2816

Cheers!

0 Kudos
1 Solution

Accepted Solutions
aerodevil
Hot Shot
Hot Shot
Jump to solution

Have you considered converting the date you get from the attribute.  Try this

$currentdate = Get-Date

Get-VM | Get-Annotation -CustomAttribute "Expires" | ?{(Get-Date $_.Value) -le $currentdate}

Josh Atwell @Josh_Atwell http://www.vtesseract.com http://github.com/joshatwell/

View solution in original post

0 Kudos
3 Replies
aerodevil
Hot Shot
Hot Shot
Jump to solution

Have you considered converting the date you get from the attribute.  Try this

$currentdate = Get-Date

Get-VM | Get-Annotation -CustomAttribute "Expires" | ?{(Get-Date $_.Value) -le $currentdate}

Josh Atwell @Josh_Atwell http://www.vtesseract.com http://github.com/joshatwell/
0 Kudos
THEL1ZARDKING
Contributor
Contributor
Jump to solution

That worked like champ, thank you so much!

0 Kudos
aerodevil
Hot Shot
Hot Shot
Jump to solution

No problem.  Glad I could help.

The trick is keeping them both in System.DateTime format for the compare.

Josh Atwell @Josh_Atwell http://www.vtesseract.com http://github.com/joshatwell/
0 Kudos