VMware Cloud Community
Azarou
Enthusiast
Enthusiast
Jump to solution

Inventory with multiple vcenter

Hi guys,

Trying to get an inventory with multiple vcenter connected,

Have some trouble with the output some VM are assigned to wrong Vcenter and running OS is not always correct.

Can someone help ?

Here the script :

$VmInfo = Foreach ($vcenter in $global:DefaultVIServers) {

   ForEach ($Datacenter in (Get-Datacenter | Sort-Object -Property Name)) {

   ForEach ($Cluster in ($Datacenter | Get-Cluster | Sort-Object -Property Name)) {

     ForEach ($VM in ($Cluster | Get-VM | Sort-Object -Property Name)) {

       ForEach ($HardDisk in ($VM | Get-HardDisk | Sort-Object -Property Name)) {

        "" | Select-Object -Property @{N="VM";E={$VM.Name}},

  @{N="Vcenter";E={$vcenter.Name}},

  @{N="OS";E={$vm.Guest.OsFullName}},

        @{N="Datacenter";E={$Datacenter.name}},

        @{N="Cluster";E={$Cluster.Name}},

        @{N="Host";E={$vm.VMHost.Name}},

  @{N="HostVersion";E={$vm.VMHost.version}}

      }

      }

    }

  }

}

$VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "D:\report.csv"

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this.

Does that produce less lines ?

$VmInfo = Foreach ($vcenter in $global:DefaultVIServers) {

   ForEach ($Datacenter in (Get-Datacenter -Server $vcenter | Sort-Object -Property Name)) {

       ForEach ($Cluster in ($Datacenter | Get-Cluster -Server $vcenter | Sort-Object -Property Name)) {

         ForEach ($VM in ($Cluster | Get-VM -Server $vcenter | Sort-Object -Property Name)) {

           ForEach ($HardDisk in ($VM | Get-HardDisk -Server $vcenter | Sort-Object -Property Name)) {

                "" | Select-Object -Property @{N="VM";E={$VM.Name}},

                @{N="Vcenter";E={$vcenter.Name}},

                @{N="OS";E={$vm.Guest.OsFullName}},

                @{N="Datacenter";E={$Datacenter.name}},

                @{N="Cluster";E={$Cluster.Name}},

                @{N="Host";E={$vm.VMHost.Name}},

                @{N="HostVersion";E={$vm.VMHost.version}}

           }

         }

      }

   }

}

$VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "D:\report.csv"


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

View solution in original post

0 Kudos
14 Replies
Azarou
Enthusiast
Enthusiast
Jump to solution

the output i got was about 90000 lines ...

Don't know where is the mistake.:(

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try like this.

Does that produce less lines ?

$VmInfo = Foreach ($vcenter in $global:DefaultVIServers) {

   ForEach ($Datacenter in (Get-Datacenter -Server $vcenter | Sort-Object -Property Name)) {

       ForEach ($Cluster in ($Datacenter | Get-Cluster -Server $vcenter | Sort-Object -Property Name)) {

         ForEach ($VM in ($Cluster | Get-VM -Server $vcenter | Sort-Object -Property Name)) {

           ForEach ($HardDisk in ($VM | Get-HardDisk -Server $vcenter | Sort-Object -Property Name)) {

                "" | Select-Object -Property @{N="VM";E={$VM.Name}},

                @{N="Vcenter";E={$vcenter.Name}},

                @{N="OS";E={$vm.Guest.OsFullName}},

                @{N="Datacenter";E={$Datacenter.name}},

                @{N="Cluster";E={$Cluster.Name}},

                @{N="Host";E={$vm.VMHost.Name}},

                @{N="HostVersion";E={$vm.VMHost.version}}

           }

         }

      }

   }

}

$VmInfo | Export-Csv -NoTypeInformation -UseCulture -Path "D:\report.csv"


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

0 Kudos
Azarou
Enthusiast
Enthusiast
Jump to solution

No one ?

0 Kudos
Azarou
Enthusiast
Enthusiast
Jump to solution

thanks Lucd !

its working fine now, can you please explain the solution ?

thx !

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, by adding the Server parameter on the cmdlets, you restrict them to only return the objects for that specific vCenter.

Otherwise, when you are connected to multiple vSphere Server (vCenter and ESXi nodes) and you have PowerCLI configured for "Multiple" mode (see the Set-PowerCLIConfiguration cmdlet), the cmdlet will return objects for all these connections.


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

0 Kudos
Azarou
Enthusiast
Enthusiast
Jump to solution

Great ! thanks

0 Kudos
wball
Contributor
Contributor
Jump to solution

LucD

LucDHow can we add other items such as RAM, CPU, HDD1, HDD2, HDD3, etc. Thank you!

0 Kudos
vin01
Expert
Expert
Jump to solution

‌Check this you will get complete vm information..this might helpfull for fetching information on large scale environment.

Complete VM information

Regards Vineeth.K
0 Kudos
wball
Contributor
Contributor
Jump to solution

Thanks, Vineeth.  That is a nice script as well.  However, I'm still interested in LucD's response.

0 Kudos
vin01
Expert
Expert
Jump to solution

‌Cool 

Regards Vineeth.K
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The thread mentioned by Vineeth has most of the addition info you want.

On the harddisk properties, do want these properties fixed (HD1, HD2 and HD3), even if a VM might no have 3 HDs ?


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

0 Kudos
wball
Contributor
Contributor
Jump to solution

Yes, I have many VMs with multiple HDDs.   Thanks.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What I meant is do you want to have a variable number of columns in the output ?

A VM with 1 HDD will have less properties than on with 3 HDD.

Variable length arrays of objects tend to have an issue in Export-Csv, unless the array is sorted in descending number of properties order (longest entry at the top).

An alternative is to have a fixed number of properties, but then some of the will be blank.

How do you want to the resulting CSV file to look ?


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

0 Kudos
wball
Contributor
Contributor
Jump to solution

No, I don't need a variable number of columns.  I took a deeper look at Complete VM information and it looks to fit my needs.  Thank you LucD  & vin01‌!

0 Kudos