VMware vSphere

 View Only

Need Help With Custom Attribute Script

  • 1.  Need Help With Custom Attribute Script

    Posted Apr 16, 2025 12:22 PM

    I currently have a script that is allowing me to pull most of the information I am looking for, but the only thing I'm stuck on is pulling either all the Custom attributes or even just the one specific one I'm looking for such as last backed up date/time.  Can someone look at this script below and let me know where I can add lines to make that happen.

    VCenterList = @("vcenter1","vcenter2","vcenter3")
    $credPath = Path Private
    $fileCred = Import-CliXML -Path $credPath

    # Initialize an array to store the results
    $result = @()

    foreach ($vcenter in $vcenterlist) {
    $esxconnect = Connect-VIServer -Server $VCenter -Credential $fileCred 

    # Get all clusters
    $clusters = Get-Cluster

    # Loop through each cluster
    foreach ($cluster in $clusters) {
        # Get cluster tags
        $clusterTags = Get-TagAssignment -Entity $cluster | Select-Object -ExpandProperty Tag

        # Get VMs in the cluster
        $vms = Get-VM -Location $cluster

        # Loop through each VM
        foreach ($vm in $vms) {
            $vmHost = Get-VMHost -VM $vm
            $vmTags = Get-TagAssignment -Entity $vm | Select-Object -ExpandProperty Tag
            $vmDisk = $vm | Get-HardDisk
            # Create a custom object for each VM
            $result += [PSCustomObject]@{
                vCenter = $esxconnect.Name
                vCenterVersion = $esxconnect.Version
                vCenterBuild = $esxconnect.Build
                ClusterName = $cluster.Name
                ClusterTags = ($clusterTags -join ", ")
                VMName = $vm.Name
                VMTags = ($vmTags | ForEach-Object {$_.Name}) -join ", "
                PowerState = $vm.PowerState
                NumCPU = $vm.NumCpu
                MemoryMB = $vm.MemoryMB
                ProvisionedSpaceGB = [Math]::Round(((($vmDisk.CapacityGB | Measure-Object -Sum).Sum)),2)
                GuestOS = $vm.Guest.OSFullName
                IPAddress = ($vm.Guest.IPAddress)[0]
                Datastore = ($vm | Get-Datastore).Name
                Host = $vmHost.Name
                HostVersion = $VMHost.Version
                HostBuild = $VmHost.Build
            }
        }
    }

    # Disconnect from the vCenter Server
    Disconnect-VIServer -Server $esxConnect -Confirm:$false -ErrorAction SilentlyContinue
    }
    # Export the results to a CSV file
    $outputFile = "C:\test\VM_info.csv"
    $result | Export-Csv -Path $outputFile -NoTypeInformation