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:
Main phase: runs on a scheduled automatic job, maybe once a day, maybe more.
I am PowerCLI novice and would appreciate any help the community can offer.
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
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
}
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
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.