rao76
Contributor
Contributor

Roundtrip script for setting custom attributes for Folder/vApp/VM

Hi All,

I would like to develop a roundtrip scrpt that ensure all Folder/vApp/VM in vCenter have a custom attribute owner tag shortly after creation. There must be a scheduler task which tags any Folder/vApp/VM which are not tagged. These are the requirements:

Custom attribute name: Owner, Project

Define: Global

First phase:

  • Extract custom attributes owner/project information from VM folders, vApp and VMs where it has been set already
  • Ensure all top level VM folders have custom attribute owner/project set
  • Use a script to push these owner/project custom attributes to every Folder, vApp and VM, keeping any that already exist and flowing those down instead
  • End result: all VMs have an custom attribute owner/project based on the tree above them, or on the manual owner/project they already had

Main phase: runs on a scheduled automatic job, maybe once a day, maybe more.

  • Extract owner/project information from VM folders, vApps and VM where it has been set already
  • If any are found that don’t have an custom attribute owner/project set, then based on what level they are at do this:
    • Top level: warning message so we can figure it out manually
    • Folder/vApp: set it based on the folder above them
    • VM: set it based on the Folder/vApp above it
  • After the run, if any owners/project were changed then email with a list of new Folders/vApps/VMs and their owners/projects, and any un-owned top level folders. Don’t email if nothing to report.
  • Repeat!

I am PowerCLI novice and would appreciate any help the community can offer.

0 Kudos
4 Replies
LucD
Leadership
Leadership

What kind of scheduler task are you considering?

If you run a VCSA, it is not advised to run scripts on there.

What code do you already have for these requirements?


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

0 Kudos
rao76
Contributor
Contributor

Hi LucD,

Appreciate your quick reply. I'm planning to use Windows task scheduler. We have an admin box that can be used to setup ad-hoc tasks.

The only code I have now is extract list of custom attributes for Folders/vApp/VMs and export to CSV. Once they are verified, or fixed, then import it back to vCenter. This is a manual task and I'm looking to automate it using roundtrip script.

Here is the script.

Get-VM |

ForEach-Object {

  $VM = $_

  $Report = "" | Select-Object VM, FQDN, IP, OS, Cluster, Folder, Owner, Project

  $Report.VM = $VM.Name

  $FQDN = ($VM.ExtensionData.Guest.IPStack[0].DnsConfig.HostName, $VM.ExtensionData.Guest.IPStack[0].DnsConfig.DomainName -join '.')

  $Report.FQDN = $FQDN

  $Report.IP = $VM.Guest.IPAddress[0]

  $Report.OS = $VM.GuestId

  $Report.Cluster = $_.VMHost.Parent

  $Report.Folder = $VM.Folder

  $Report.Owner = $_.CustomFields.Item("Owner")

  $Report.Project = $_.CustomFields.Item("Project")

  $Report

} | Export-Csv vm-export-all.csv -NoTypeInformation -UseCulture

Import-Csv -Path vm-export-all.csv | Where-Object {$_.VM} |

ForEach-Object {

  Get-VM $_.VM | Set-Annotation -CustomAttribute Owner -Value $_.Owner

  Get-VM $_.VM | Set-Annotation -CustomAttribute Project -Value $_.Project

}

0 Kudos
LucD
Leadership
Leadership

That script doesn't even look at Folders nor vApps.
And on the import it only sets the CA on the VM where the CA was already present, it does not flow down/up the hierarchy of Folders and vApps.

Further, what do you define as a "top level folder"?
Perhaps an overview of your Folder-vApp-VM hierarchy would clarify.


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

0 Kudos
rao76
Contributor
Contributor

I have another script that handles Folders and vApps. This one is for VMs.

Correct, the present script does not flow down/up the hierarchy of Folders/vApps. This is currently done manually. I extract CSV of Folder/vApps/VMs and use excel to flow down/up the CA. The final CSV then imported back to vCenter.

The top level folder starts Data Center then follow by project folder. For eg:


Data Center

     Project Folder

          VM1

          VM2

          Subfolder1

               VM3

               VM4

          Subfolder2

               vApp1

               vApp2

     ...

Hope you got the point.

0 Kudos