VMware Cloud Community
eaalvare
Contributor
Contributor
Jump to solution

ESXi and vm inventory Report

Looking for a way to tweak this script or a better script. Currently I have a two liner script that queries all poweron/off vm guest first and them goes out to get the esxi hosts. Once complete it exports the results to a csv file. The problem? It is taking a very long time to complete. The vSphere only has about 600 vms and I am guessing why it is taking a long time is that it is searching for the esx host that the vm is been query on.

Here is the code in question.

$vc = connect-viserver "VCNAME"
Get-VM | Select Name, @{N="ESX Host";E={Get-VMHost -VM $_}} | `
Export-Csv -NoTypeInformation C:\VM_Host.csv

Hope to hear from someone soon.

Thanks,

E

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Could it be that you have a host that has no VMs defined on it ?

Try it like this

$report = foreach($esx in (Get-View -ViewType HostSystem -Property Name,VM)){
    if($esx.Vm){
        Get-View $esx.VM |
        select @{N="VM";E={$_.Name}},
            @{N="Host";E={$esx.Name}}
    }
}
$report | Export-Csv -NoTypeInformation C:\VM_Host.csv


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

View solution in original post

0 Kudos
14 Replies
LucD
Leadership
Leadership
Jump to solution

The Get-View cmdlet is way faster than the Get-VM cmdlet.

Do something like this

Get-View -ViewType VirtualMachine -Property Name,Runtime.Host |
Select Name,@{N="Host";E={(Get-View $_.Runtime.Host).Name}}


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

0 Kudos
eaalvare
Contributor
Contributor
Jump to solution

This is an interesting one.. Can we maybe script that it would first query esxi host and then get their guests? Maybe that would speed things up.

Thanks

E

0 Kudos
eaalvare
Contributor
Contributor
Jump to solution

I am getting some weird output when complete...and promptly me to enter something.

cmdlet Export-Csv at command pipeline position 1
Supply values for the following parameters:
InputObject:

here is the code again. Did I miss something

Connect-VIServer "VCNAME"
Get-View -ViewType VirtualMachine -Property Name,Runtime.Host |
Select Name,@{N="Host";E={(Get-View $_.Runtime.Host).Name}}
Export-Csv -NoTypeInformation C:\VM_Host.csv

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You missed to place the objects on the pipeline ('|') to feed the Export-Csv cmdlet.

Get-View -ViewType VirtualMachine -Property Name,Runtime.Host |
Select Name,@{N="Host";E={(Get-View $_.Runtime.Host).Name}} |
Export-Csv -NoTypeInformation C:\VM_Host.csv 


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is also possible (everything is possible with PowerShell Smiley Wink).

But I don't think that this way will be faster.

foreach($esx in (Get-View -ViewType HostSystem -Property Name,VM)){
    Get-View $esx.VM |
    select @{N="VM";E={$_.Name}},
        @{N="Host";E={$esx.Name}}
}


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

0 Kudos
eaalvare
Contributor
Contributor
Jump to solution

So it would look like this. Is this correct?

======================================================================

Connect-VIServer "vcname"
foreach($esx in (Get-View -ViewType HostSystem -Property Name,VM)){     Get-View $esx.VM |     select @{N="VM";E={$_.Name}},         @{N="Host";E={$esx.Name}} }

Export-Csv -NoTypeInformation C:\VM_Host.csv

======================================================================

E

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Since we produce the output in a foreach loop, we will have to do it slightly different.

Like this

$report = foreach($esx in (Get-View -ViewType HostSystem -Property Name,VM)){
    Get-View $esx.VM |
    select @{N="VM";E={$_.Name}},
        @{N="Host";E={$esx.Name}}
}
$report | Export-Csv -NoTypeInformation C:\VM_Host.csv


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

eaalvare
Contributor
Contributor
Jump to solution

I am getting the following after executing

==========================================================

Connect-viserver "vsphere"
$report = foreach($esx in (Get-View -ViewType HostSystem -Property Name,VM)){
    Get-View $esx.VM |
    select @{N="VM";E={$_.Name}},
        @{N="Host";E={$esx.Name}}
}
$report | Export-Csv -NoTypeInformation C:\VM_Host.csv

==========================================================

Get-View : Cannot validate argument on parameter 'VIObject'. The argument is null, empty, or an element of the argument collection contains a null
lue. Supply a collection that does not contain any null values and then try the command again.
At C:\CLI_Scripts\test\testrun1.ps1:3 char:13
+     Get-View <<<<  $esx.VM |
    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could it be that you have a host that has no VMs defined on it ?

Try it like this

$report = foreach($esx in (Get-View -ViewType HostSystem -Property Name,VM)){
    if($esx.Vm){
        Get-View $esx.VM |
        select @{N="VM";E={$_.Name}},
            @{N="Host";E={$esx.Name}}
    }
}
$report | Export-Csv -NoTypeInformation C:\VM_Host.csv


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

0 Kudos
eaalvare
Contributor
Contributor
Jump to solution

Luc.. This worked!! Thank you for your help!!!!  Kuddos

0 Kudos
GreenC201110141
Contributor
Contributor
Jump to solution

Hello eealvare,

LucD as right, everything is possible with PowerShell Smiley Wink

I had the same request from my boss on Monday (With Esx version) and I tried, alone on my side to script this kind of code. I have exactly the same constraint: time to process the report.

I made a first version, but I was not very happy, too long --> 4 hours for ~400 Esxs and ~4,000 guests.

So please find attached script I use now. The script will ouput for each Guest, It's name, state, host HostVersion and HostID.

$ReportingHosts = @()

$AllHosts = Get-View -ViewType HostSystem -Property Name, Summary.Config.Product.Version, Summary.Host

Foreach ($VmHost in $AllHosts) {

     $Row = "" | Select Name, Version, HostID

     $Row.Name = $VmHost.Name

     $Row.Version = $VmHost.Summary.Config.Product.Version

     $Row.HostID = $VmHost.Summary.Host.Type+"-"+$VmHost.Summary.Host.Value

     $ReportingHosts += $Row }

$ReportingGuests = @()

$AllVms = Get-View -ViewType VirtualMachine -Property Name, Summary.Runtime.PowerState, Summary.Runtime.Host

Foreach ($Guest in $AllVms) {

     $Row = "" | Select Name, State, HostName, HostVersion, HostID

     $Row.Name = $Guest.Name

     $Row.State = $Guest.Summary.Runtime.PowerState

     $HostID = $Guest.Summary.Runtime.Host

     $Row.HostID = $HostID

     Foreach ($Entry in $ReportingHosts) {

         If ($HostID -eq $Entry.HostID){

              $Row.HostName = $Entry.Name

               $Row.HostVersion = $Entry.Version}

          }

     $ReportingGuests += $Row

}

$ReportingGuests| Export-Csv -NoTypeInformation -Path 'C:\Inventory.csv' -Delimiter ';'

You can sort object to group status or Host version :

     $ReportingGuests | Group-Object State, HostVersion

This script take less than a second to compute in, my case, 26 differents ESX and 196 machines.

For my entire environment (~400 Esxs and ~4,000 guests), the script take only 35 seconds Smiley Wink

Tell me if it's good for you.

Kind Regards,

Chris.

Ce message a été modifié par: GreenC2011101…

0 Kudos
vmhyperv
Contributor
Contributor
Jump to solution

Hi,

The script is execlllent but the output generating in csv not in proper format.

Can you modify a little bit so that output in csv  in proper row and columnwise.

thanks

vmguy

0 Kudos
vmhyperv
Contributor
Contributor
Jump to solution

Thanks

its resolved i removed the delimter switch but any how its very fast and good one.As we have 400 ESX and 4000 vms too.

thanks

vmguy

0 Kudos
GreenC201110141
Contributor
Contributor
Jump to solution

Hi,

Oups, I use this delimiter as we're using Office in fr-CH Culture and the delimiter is the semi-colon.

You can use the switch 'Useculture' if you want to export directly in the right culture used on your machine.

Regards,

Chris.

0 Kudos