Visitors

Exporting Virtual Machine information with PowerCLI

Just a quick script to export virtual machine inventory:

$report = @()
foreach ($DC in Get-Datacenter -Name MyDC){ # Specify Datacenter name if needed
foreach ($Cluster in Get-Cluster -Location $DC ){ # Specify Cluster name if needed
foreach ($VM in Get-VM -Location $Cluster ){

$row = "" | select DC, Cluster, ResourcePool, VMPath, VMhost, PowerState, Name, OS, IPaddress, CPU, Memory, ProvisionedSpaceGB, UsedSpaceGB, Datastore, CustomField1, CustomField2, Notes

  $row.DC = $DC
  $row.Cluster = $Cluster
     ##### If there is no Resource Pool configured in the Cluster, use the Cluster name
     if ($vm.ResourcePool -like "Resources") {$row.ResourcePool = $Cluster}
     else {$row.ResourcePool = $vm.ResourcePool}
        ##### Full VM path in the vCenter VM folder structure - start
        $current = $vm.Folder
        $path = $vm.Name
        do {
        $parent = $current
        if($parent.Name -ne "vm"){$path =  $parent.Name + "" + $path}
        $current = Get-View $current.Parent
        } while ($current.Parent -ne $null)
        $row.VMPath = $path
        ##### Full VM path in the vCenter VM folder structure - finish
  $row.VMhost = $vm.VMHost
  $row.Name = $vm.Name
  $row.PowerState = $vm.PowerState
  $row.OS = $vm.Guest.OSFullName
  $row.IPaddress = $vm.Guest.IPAddress | Out-String
  $row.CPU = $vm.NumCPU
  $row.Memory = $vm.MemoryGb
  $row.ProvisionedSpaceGB = [math]::round( $vm.ProvisionedSpaceGB , 2 )
  $row.UsedSpaceGB = [math]::round( $vm.UsedSpaceGB , 2 )
  $row.Datastore = ($vm | Get-Datastore).Name | Out-String
  $row.CustomField1 = $vm.CustomFields.item('CustomField1') # If you have Custom Fields...
  $row.CustomField2 = $vm.CustomFields.item('CustomField2') # If you have Custom Fields...
  $row.Notes = $vm.Notes

$report += $row

}}}

$report | Sort Name | Export-Csv -Path "D:VMs.csv"

Would you like to add any other VM details to the script output?

6 comments to Exporting Virtual Machine information with PowerCLI

  • I would love to get this exact information using a Get-View script, including the CustomFields /or AvailableFields in Get-View. I cannot seem to get it work correctly in my scripts. :(

  • If you can make a script using Get-View, I am not able to use Get-Cluster either, think the Admins locked it down. :((

    Get-Cluster : The cluster service is not running. Make sure that the service is running on all nodes in the cluster.

  • aenagy

    What modification is necessary if some/all of the virtual machines are not in a vSphere cluster, i.e. small remote locations?

  • Sri Harsha

    This is a beautiful script… Can i get the same script using array instead of for loop….?

  • Toufiq

    Dear All , i am looking script which can provide complete information of virtual machines inventory. we can do manual but not through power cli for example Uptime, UUID, POC1 & POC2.

  • Ivaylo

    This is exactly what I needed as a model to follow. Thank You

Leave a Reply

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>