VMware {code} Community
Chris56
Contributor
Contributor

Script for daily update of VM custom fields in VCenter using a CSV file

Hello,

For information, I do not ask for a script already done but rather advice on how to approach the need.  I'm a bit new to PS so I don't necessarily have a good idea of ​​the possibilities.

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 VMs custom fields on VCenter are updated every night based on this file, using powercli and Opcon.  My boss asked me to keep using Custom Fields.

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!

Labels (3)
Tags (2)
0 Kudos
3 Replies
scott28tt
VMware Employee
VMware Employee

There is an area of the forums dedicated to PowerCLI, I've reported your thread to moderators so they should move it there.

 


-------------------------------------------------------------------------------------------------------------------------------------------------------------

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
Tags (1)
0 Kudos
Chris56
Contributor
Contributor

I created the topic on ¨PowerCli area a few minutes ago, I'm trying to delete this topic!

Thank you

0 Kudos
scott28tt
VMware Employee
VMware Employee

I see your duplicate, I've reported this thread again now asking a moderator to remove it.


-------------------------------------------------------------------------------------------------------------------------------------------------------------

Although I am a VMware employee I contribute to VMware Communities voluntarily (ie. not in any official capacity)
VMware Training & Certification blog
0 Kudos