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?
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
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
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?
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
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)
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
It is ignoring replicas correctly 😄 but still inputting 1.1.70 😞
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
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
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
"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?
How is such a replica named?
The VM DisplayName with the suffix '_replica'?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Exactly, VMName1 (production) VMName1_replica (backup)
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
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:
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
Ahhh shoot ya I see it now, sorry. Thank you so much. Happy Holidays dude!