VMware Cloud Community
willster07
Contributor
Contributor
Jump to solution

add a date into a custom attribute

I want to add the date a VM is deployed to a custom attribute in vsphere that's already created, named BuildDate

It's proving to be a little more difficult than i thought.

I'm pretty sure this command gives me the correct date I'm looking for, and appears to be unaffected by snapshots:

(Get-VM -Name vmserver1).ExtensionData.Config.createDate

The output for vmserver1 would look like:

Thursday, August 19, 2021 7:55:57 PM

I want that output to be the value XXXX below, which would need to be a variable....

Set-Annotation -Entity $vmName -CustomAttribute "BuildDate" -Value XXXX

anyone got any ideas for a less than novice PowerCli user?

 

Thanks in advance

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

I would do something like this.

- check if the CA exists, if not, create it
- check if the VM has a value in the CreateData property (older VMs don't have it)
- assign the value to the CA (use the pipeline, no need for a variable)

$caName = 'BuildDate'
try{
  $ca = Get-CustomAttribute -Name $caName -ErrorAction Stop
}
catch{
  $ca = New-CustomAttribute -Name $caName -TargetType VirtualMachine
}
Get-VM |
Where-Object { $_.ExtensionData.Config.CreateDate } |
ForEach-Object -Process {
  Set-Annotation -Entity $_ -CustomAttribute $ca -Value $_.ExtensionData.Config.CreateDate
}

 


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

View solution in original post

5 Replies
LucD
Leadership
Leadership
Jump to solution

I would do something like this.

- check if the CA exists, if not, create it
- check if the VM has a value in the CreateData property (older VMs don't have it)
- assign the value to the CA (use the pipeline, no need for a variable)

$caName = 'BuildDate'
try{
  $ca = Get-CustomAttribute -Name $caName -ErrorAction Stop
}
catch{
  $ca = New-CustomAttribute -Name $caName -TargetType VirtualMachine
}
Get-VM |
Where-Object { $_.ExtensionData.Config.CreateDate } |
ForEach-Object -Process {
  Set-Annotation -Entity $_ -CustomAttribute $ca -Value $_.ExtensionData.Config.CreateDate
}

 


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

willster07
Contributor
Contributor
Jump to solution

Wow this is awesome!  I replied yesterday that I wouldn’t be able to test it until Monday…not seeing that post. Not sure what happened but didn’t want to leave this unacked still. Thank you!

Reply
0 Kudos
willster07
Contributor
Contributor
Jump to solution

It worked flawlessly.  Thank you!

The interesting thing when i ran it in a powershell prompt the output looked like

AnnotatedEntity Name Value
--------------- ---- -----
NSLCE4 BuildDate 05/01/2022 08:19:37
NSPVS4C BuildDate 10/04/2022 12:00:37

when i saved it as a PS1 file and ran it, output looked like

 

AnnotatedEntityId : VirtualMachine-vm-40735
AnnotatedEntity : VARP4F
Name : BuildDate
Value : 08/19/2021 19:55:57
Uid : /VIServer=vsphere.local\admin@esxp7:443/VirtualMachine=VirtualMachine-vm-40735/Annotat
ionValue=BuildDate/


AnnotatedEntityId : VirtualMachine-vm-39770
AnnotatedEntity : ORBP1C-CLONE
Name : BuildDate
Value : 08/14/2021 06:31:07
Uid : /VIServer=vsphere.local\admin@esxp7:443/VirtualMachine=VirtualMachine-vm-39770/Annotat
ionValue=BuildDate/

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The number of properties PS shows is defined in the so-called Types files.
In those files different formats are defined (Table, List, Wide, Custom).

The selection of the format is determined by the width of the output.
That width can be different, depending on where you run the code (prompt, .ps1 file).


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

willster07
Contributor
Contributor
Jump to solution

Well thank you again

Reply
0 Kudos