VMware Cloud Community
Chris56
Contributor
Contributor

Script for daily update of VM Custom fields using a CSV file as reference

For information, I do not ask for a script already done but rather advice on how to approach the need.

We rely a lot on VM custom fields in VMWare to have up-to-date information (contacts, services, phone, applications, backup, etc...), but with manual input in the Vcenter by the new system administrator, me.

In my opinion it is unmanageable on a daily basis because we lack visibility and at the slightest change, nothing is updated and the server administrators do not send us any information, so I have to go hunting for all the details.

To involve everyone in the process (so each server admin), I want to have a reference CSV on a drive accessible by everyone with a line for each VM and the columns that correspond to the custom fields and that the VM on VCenter are updated every night based on this file, using powercli and Opcon.

For the moment, I thought of using compare-object and the side indicators between the list of VMs in the CSV file, and the list of VMs in the GET-VM. Based on the side indicators, I could probably be able to update the CSV file.

  • If the VM exists in GET-VM but not in the CSV, create the line in the CSV with the custom fields.

  • If the VM exists in the CSV but not in GET-VM, delete the line from the CSV.

  • If the VM exists in the GET-VM and in the CSV, ensure that the custom fields in the vCenter match those in the CSV.

Something like that, it is just a quick draft with only write-host for an opinion:

 $ReferenceVms = Import-Csv .\current_servers.csv -Delimiter ',' 
 $VcenterVms = Import-Csv .\vcenter_servers.csv -Delimiter ',' #I don't know if if I can use a temporary array from GET-VM instead of a CSV
    
 $Vms = Compare-Object -Property VM -ReferenceObject $ReferenceVms -DifferenceObject $VcenterVms -PassThru -IncludeEqual
        
 foreach ($vm in $Vms) {
        
    if ($vm.SideIndicator -eq "=>") {
        Write-Host 'The VM' $vm.VM 'has been added to reference file'
    }
        
    elseif ($vm.SideIndicator -eq "<=") {
        Write-Host 'The VM' $vm.VM 'has been removed from reference file since it does not exist in VCenter'
    }
        
    else {
        Write-Host 'The VM' $vm.VM 'is OK'
    }

Can you tell me if this method seems to work or if there is something more suitable?

Thank you!

0 Kudos
1 Reply
LucD
Leadership
Leadership

No, in my opinion, the Compare-Object cmdlet is the best approach to compare two arrays.


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

0 Kudos