VMware vSphere

 View Only
  • 1.  Need Assistance Creating A Script Tp Pull All VM Data

    Posted Feb 25, 2025 05:03 PM

    I currently need assistance creating a script to pull all the VM information from multiple vcenters.  Can someone please provide me with an example or help me with the one shown below?  The example I use below only saves the data for that last vcenter3 on the list to a csv file.   

    #Connect to vCenter
    $VCenter = @("vcenter1","vcenter2","vcenter3")
    $AdminUser = 'Admin'

    $Password = 'Test'

    Connect-VIServer -Server $VCenter -User $AdminUser -Password $Password | Select Name, Version, Build
     
    #Get all VMs in vCenter
    $allVMs = Get-VM
     
    # Store VM information
    $vmInfo = @()
     
    # Loop through each VM and retrieve its information
    foreach ($vm in $allVMs) 
        {
        $vmHost = Get-VMHost -VM $vm
        $vmDisk = $vm | Get-HardDisk
        $vmInfo += [PSCustomObject] @{
            Name = $vm.Name
            PowerState = $vm.PowerState
            NumCPU = $vm.NumCpu
            MemoryMB = $vm.MemoryMB
            GuestOS = $vm.Guest.OSFullName
            IPAddress = ($vm.Guest.IPAddress)[0]
            Cluster = ($vm | Get-Cluster).Name
            Datastore = ($vm | Get-Datastore).Name
            Host = $vmHost.Name
            ProvisionedSpaceGB = [Math]::Round(((($vmDisk.CapacityGB | Measure-Object -Sum).Sum)),2)
        }
    }

    # Export VM information to a CSV file
    $vmInfo | Export-Csv -Path "C:\Test\VM_Info.csv" -NoTypeInformation



  • 2.  RE: Need Assistance Creating A Script Tp Pull All VM Data

    Posted Feb 26, 2025 08:42 AM

    I use VCheck to fully document my environment every week. It hasn't been updated in a while but it still works and it's free.

    https://github.com/alanrenouf/vCheck-vSphere



    ------------------------------
    PB
    ------------------------------



  • 3.  RE: Need Assistance Creating A Script Tp Pull All VM Data
    Best Answer

    Posted Feb 26, 2025 09:22 AM

    I do similar scripts.

    I use a foreach for every vcenter server.  We have multiple clusters so I like to include that as well.

    I updated your code to include how I do things. You may notice I like to put a comment at the end of my loops with what the start is. Something I picked up from my Pascal teacher in college. Helps know where you are.

      
    #Connect to vCenter
    $VCenterList = @("vcenter1","vcenter2","vcenter3")
    $AdminUser = 'Admin'
    $Password = 'Test'
    Connect-VIServer -Server $VCenter -User $AdminUser -Password $Password | Select Name, Version, Build
     
    #Get all VMs in vCenter
    $allVMs = Get-VM
     
    # Store VM information
    $vmInfo = @()
     
    foreach ($vcenter in $vcenterlist) {

    #get name only of clusters and sort for output aesthetics

    $clusterlist = (Get-cluster).Name | Sort Name

    Foreach($cluster in $clusterlist) {

    # Loop through each VM and retrieve its information
    foreach ($vm in $allVMs)  {
        $vmHost = Get-VMHost -VM $vm
        $vmDisk = $vm | Get-HardDisk
        $vmInfo += [PSCustomObject] @{
            Name = $vm.Name
            PowerState = $vm.PowerState
            NumCPU = $vm.NumCpu
            MemoryMB = $vm.MemoryMB
            GuestOS = $vm.Guest.OSFullName
            IPAddress = ($vm.Guest.IPAddress)[0]
            Cluster = ($vm | Get-Cluster).Name
            Datastore = ($vm | Get-Datastore).Name
            Host = $vmHost.Name
            ProvisionedSpaceGB = [Math]::Round(((($vmDisk.CapacityGB | Measure-Object -Sum).Sum)),2)
        } 
    } #foreach ($vm in $allVMs)  {
    } #Foreach($cluster in $clusterlist) {
    } #foreach ($vcenter in $vcenterlist) {
    # Export VM information to a CSV file
    $vmInfo | Export-Csv -Path "C:\Test\VM_Info.csv" -NoTypeInformation