VMware Cloud Community
pamiller21
Enthusiast
Enthusiast
Jump to solution

Creation Date Script

I am looking around and found a few methods to find the creation date of a VM, but not one where I could take that date and input it in a custom attribute, is there something out there to help with that?

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Looks like you missed a newline between those lines.
Those are 2 separate lines.
I'll attach the code as a file

 

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

Reply
0 Kudos
16 Replies
LucD
Leadership
Leadership
Jump to solution

Note that this property was introduced in vSphere 6.7, VMs created in earlier versions will have no value in that property.

You can check with

Get-VM |
Select Name,@{N='CreationDate';E={$_.ExtensionData.Config.CreateDate}}

To set the Custom Attribute, when the value is available, you could do

try{
    $caCreateDate = Get-CustomAttribute -Name CreationDate -TargetType VirtualMachine -ErrorAction Stop
}
catch{
    $caCreateDate = New-CustomAttribute -Name CreationDate -TargetType VirtualMachine -Confirm:$false
}

Get-VM |
Where{$_.ExtensionData.Config.CreateDate -ne ''} |
ForEach-Object -Process {
    Set-Annotation -Entity $_ -CustomAttribute $caCreateDate -Value $_.ExtensionData.Config.CreateDate -Confirm:$false
}

Note that this snippet will create the Custom Attribute when it doesn't exist yet.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
pamiller21
Enthusiast
Enthusiast
Jump to solution

Looks pretty sweet, but 2 problems:

1) Lots are showing up as created in 1970, so whats up with that?
2) Can I format the date input?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

1. That probably means the field is empty (created before the property was introduced).
And empty or 0, returns the Unix epoch date

 

2. Not sure what you mean by that.
Do you want to only see VMs created after a specific date?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
pamiller21
Enthusiast
Enthusiast
Jump to solution

1. Gotcha, anyway to tell the script, if its null to ignore?

2. I mean can I format the date as 12.14.20?

3. I know I'm adding, but anyway to ignore VMs with _replica added? And then add a part to copy a VMs date to its replica (ie VMName1 copies its date to VMName_replica)

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

We can test for $null instead of ''.
Let's see if that helps

try{
  $caCreateDate = Get-CustomAttribute -Name CreationDate -TargetType VirtualMachine -ErrorAction Stop
}
catch{
  $caCreateDate = New-CustomAttribute -Name CreationDate -TargetType VirtualMachine -Confirm:$false
}

Get-VM |
Where{$_.Name -notmatch "_replica$" -and $_.ExtensionData.Config.CreateDate -ne $null} |
ForEach-Object -Process {
  Set-Annotation -Entity $_ -CustomAttribute $caCreateDate -Value $_.ExtensionData.Config.CreateDate.ToString('MM.dd.yy') -Confirm:$false
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
pamiller21
Enthusiast
Enthusiast
Jump to solution

It is ignoring replicas correctly 😄 but still inputting 1.1.70 😞

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Since I don't have any such VMs, I wonder what exactly is in that property.
Can you run the following and show what the output is for a VM that shows this 1.1.70?

Get-VM | Select Name,
    @{N='Created';E={$_.ExtensionData.Config.CreateDate}},
    @{N='CreatedShort';E={$_.ExtensionData.Config.CreateDate.ToString('MM.dd.yy')}},
    @{N='Null';E={$_.ExtensionData.Config.CreateDate -eq $null}},
    @{N='Blank';E={$_.ExtensionData.Config.CreateDate -eq ''}},
    @{N='Null';E={$_.ExtensionData.Config.CreateDate -eq $null}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
pamiller21
Enthusiast
Enthusiast
Jump to solution

PS /root> Get-VM "FieldOps-Utils" | Select Name, @{N='Created';E={$_.ExtensionData.Config.CreateDate}}, @{N='CreatedShort';E={$_.ExtensionData.Config.CreateDate.ToString('MM.dd.yy')}}, @{N='Null';E={$_.ExtensionData.Config.CreateDate -eq $null}}, @{N='Blank';E={$_.ExtensionData.Config.CreateDate -eq ''}}, @{N='Null';E={$_.ExtensionData.Config.CreateDate -eq $null}}
Select-Object: The property cannot be processed because the property "Null" already exists.

Name : FieldOps-Utils
Created : 1/1/1970 12:00:00 AM
CreatedShort : 01.01.70
Null : False
Blank : False

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

So that field actually seems to have the value 1.1.70 in there.
Then we can test for that (the format of the constant date string '01/01/70 00:00' might vary depending on your regional settings.
Could be that you have to do some experimenting there

try{
  $caCreateDate = Get-CustomAttribute -Name CreationDate -TargetType VirtualMachine -ErrorAction Stop
}
catch{
  $caCreateDate = New-CustomAttribute -Name CreationDate -TargetType VirtualMachine -Confirm:$false
}

Get-VM |
Where{$_.Name -notmatch "_replica$" -and $_.ExtensionData.Config.CreateDate -ne [DateTime]'01/01/70 00:00'} |
ForEach-Object -Process {
  Set-Annotation -Entity $_ -CustomAttribute $caCreateDate -Value $_.ExtensionData.Config.CreateDate.ToString('MM.dd.yy') -Confirm:$false
}

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
pamiller21
Enthusiast
Enthusiast
Jump to solution

"So that field actually seems to have the value 1.1.70 in there." --- maybe we did make the VMs in 1970 🤔

but that worked perfect.  Is there anyway to take the custom attributes from the production to the replica now?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

How is such a replica named?
The VM DisplayName with the suffix '_replica'?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
pamiller21
Enthusiast
Enthusiast
Jump to solution

Exactly, VMName1 (production) VMName1_replica (backup)

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try something like this

try{
  $caCreateDate = Get-CustomAttribute -Name CreationDate -TargetType VirtualMachine -ErrorAction Stop
}
catch{
  $caCreateDate = New-CustomAttribute -Name CreationDate -TargetType VirtualMachine -Confirm:$false
}

Get-VM |
Where{$_.Name -notmatch "_replica$" -and $_.ExtensionData.Config.CreateDate -ne [DateTime]'01/01/70 00:00'} |
ForEach-Object -Process {
  $replica = Get-VM -Name "$($_.Name)_replica"
  Set-Annotation -Entity $_,$replica -CustomAttribute $caCreateDate -Value $_.ExtensionData.Config.CreateDate.ToString('MM.dd.yy') -Confirm:$false
  
}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
pamiller21
Enthusiast
Enthusiast
Jump to solution

I am getting:

Line |
4 | … $replica = Get-VM -Name "$($_.Name)_replica" Set-Annotation -Entity …
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| A positional parameter cannot be found that accepts argument 'Set-Annotation'.
Get-VM:

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like you missed a newline between those lines.
Those are 2 separate lines.
I'll attach the code as a file

 

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
pamiller21
Enthusiast
Enthusiast
Jump to solution

Ahhh shoot ya I see it now, sorry.  Thank you so much. Happy Holidays dude!

Reply
0 Kudos