Automation

 View Only
  • 1.  Unable to get VMs info from multiple vcenter

    Posted May 28, 2020 01:24 PM

    Hi,

    I am trying to get the VM information from multiple vCenter and it shows blank output without any errors

    Please help

    Script

    $vCenters = @(

    "vcenter1"

    "vcenter2"

    )

    Connect-viserver -Server $vCenters

    #Actual Script

    $vmInfo = Foreach ($vc in $global:DefaultVIServers)

    {

        Get-Folder 'Universal-Dev' -Server $vc | Get-VM -Server $vc |

        ForEach-Object -Process {

            $obj = [HashTable]::new()

            $obj.Add('Name', $_.Name)

            $obj.Add('ESX_Host', $_.VMHost)

            $obj.Add('vCenter', ([uri]$_.ExtensionData.Client.ServiceUrl).Host)

            $obj.Add('VM PowerState', $_.PowerState)

            $obj.Add('Operating System', $_.guest.OSFullName)

            $obj.Add('Folder', $_.Folder.Name)

            $obj.Add('IPAddress', $_.guest.IpAddress[0])

            $obj.Add('CPU Count', $_.NumCPU)

            $obj.Add('MemoryInGB)', ([math]::round($_.MemoryGB)))

            $obj.Add('HDDInGB)', ([math]::Round($_.Capacity / 1GB)))

            $obj.Add('Notes', $_.Notes)

            $obj.Add('Description', (Get-Annotation -Entity $_ -Name "Description" | Select -ExpandProperty Value))

        }

    }

    $vmInfo |  ft -auto



  • 2.  RE: Unable to get VMs info from multiple vcenter

    Posted May 28, 2020 02:06 PM

    You forgot to put $obj on the pipeline.

    $vCenters = @(

    "vcenter1"

    "vcenter2"

    )


    Connect-viserver -Server $vCenters


    #Actual Script

    $vmInfo = Foreach ($vc in $global:DefaultVIServers)

    {

        Get-Folder 'Universal-Dev' -Server $vc | Get-VM -Server $vc |

        ForEach-Object -Process {

            $obj = [HashTable]::new()

            $obj.Add('Name', $_.Name)

            $obj.Add('ESX_Host', $_.VMHost)

            $obj.Add('vCenter', ([uri]$_.ExtensionData.Client.ServiceUrl).Host)

            $obj.Add('VM PowerState', $_.PowerState)

            $obj.Add('Operating System', $_.guest.OSFullName)

            $obj.Add('Folder', $_.Folder.Name)

            $obj.Add('IPAddress', $_.guest.IpAddress[0])

            $obj.Add('CPU Count', $_.NumCPU)

            $obj.Add('MemoryInGB)', ([math]::round($_.MemoryGB)))

            $obj.Add('HDDInGB)', ([math]::Round($_.Capacity / 1GB)))

            $obj.Add('Notes', $_.Notes)

            $obj.Add('Description', (Get-Annotation -Entity $_ -Name "Description" | Select -ExpandProperty Value))

            $obj

        }

    }


    $vmInfo |  ft -auto



  • 3.  RE: Unable to get VMs info from multiple vcenter

    Posted May 28, 2020 02:33 PM

    oops, My bad. Thank you very much for your help.

    I am unable to get formatted output using ft -auto



  • 4.  RE: Unable to get VMs info from multiple vcenter
    Best Answer

    Posted May 28, 2020 02:44 PM

    I didn't notice you were creating a hash table for each VM.

    Not sure why.

    In any case, the following should do the trick

    #Actual Script


    $vmInfo = Foreach ($vc in $global:DefaultVIServers)

    {

        Get-Folder 'Universal-Dev' -Server $vc | Get-VM -Server $vc |

        ForEach-Object -Process {

            $obj = [ordered]@{}

            $obj.Add('Name', $_.Name)

            $obj.Add('ESX_Host', $_.VMHost)

            $obj.Add('vCenter', ([uri]$_.ExtensionData.Client.ServiceUrl).Host)

            $obj.Add('VM PowerState', $_.PowerState)

            $obj.Add('Operating System', $_.guest.OSFullName)

            $obj.Add('Folder', $_.Folder.Name)

            $obj.Add('IPAddress', $_.guest.IpAddress[0])

            $obj.Add('CPU Count', $_.NumCPU)

            $obj.Add('MemoryInGB)', ([math]::round($_.MemoryGB)))

            $obj.Add('HDDInGB)', ([math]::Round($_.Capacity / 1GB)))

            $obj.Add('Notes', $_.Notes)

            $obj.Add('Description', (Get-Annotation -Entity $_ -Name "Description" | Select -ExpandProperty Value))

           

             New-Object -TypeName PSObject -Property $obj

        }

    }

    $vmInfo | ft -AutoSize



  • 5.  RE: Unable to get VMs info from multiple vcenter

    Posted May 28, 2020 03:08 PM

    That worked, Thank you very much LucD :smileyhappy: