Enthusiast
Enthusiast

How to increase speed get-vm get-view

Jump to solution

Hi,

I'm new in PowerCLI.

I'm using next script to obtain Virtual Machine Inventory:

$outputFileVM = 'D:\PowerShell_scripts\Inventory.csv'

$VMReport = @()

get-vm | sort name | %{

$VM = Get-View $_.ID

$row = "" | Select-Object VM_Name, VM_Host, VM_Host_Model, VM_Memory, VM_vCpu, VM_CpuLimit, VM_CpuShares

$row.VM_Name = $VM.Config.Name

$row.VM_Host = Get-VMHost -VM $_

$row.VM_Host_Model = Get-VMHost -VM $_ | Get-View | % {$_.Hardware.SystemInfo.Model}

$row.VM_Memory = $VM.Config.Hardware.memoryMB

$row.VM_vCpu = $VM.Config.Hardware.numCPU

$row.VM_CpuLimit = $VM.Config.CpuAllocation.Limit

$row.VM_CpuShares = $VM.Config.CpuAllocation.Shares.Shares

$VMReport += $row

}

$VMReport | Export-Csv $outputFileVM -NoTypeInformation

This script runs very slow. I read something about how to increase speed using get-view -viewType. But I don't know how to use it to refactory this script.

Anyone can help me?

Thanks.

Eduard.

0 Kudos
1 Solution

Accepted Solutions
Immortal
Immortal

To continue LucD's improvements and make it run very fast, make use of get-view's "-Property " parameter and specify the properties of the VirtualMachine or ESX host that should be retrieved. This prevents all the properties you aren't interested in using from being retrieved, and speeds up your code dramatically. In other words, replace your call to "Get-VM" with this:

  Get-View -ViewType VirtualMachine -Property Name,Runtime.Host,Config.Hardware,Config.CpuAllocation

because you are only interested in the Name, Runtime.Host, Config.Hardware, and Config.CpuAllocation properties of VirtualMachine. Replace your call to "Get-VMHost" in a similar fashion.

$outputFileVM = 'D:\PowerShell_scripts\Inventory.csv'
$VMReport = @()
Get-View -ViewType VirtualMachine -Property Name,Runtime.Host,Config.Hardware,Config.CpuAllocation | Sort-Object -Property Name | %{
	$row = "" | Select-Object VM_Name, VM_Host, VM_Host_Model, VM_Memory, VM_vCpu, VM_CpuLimit, VM_CpuShares
	$row.VM_Name = $_.Name
	$esx = Get-View $_.Runtime.Host -Property Name,Hardware.SystemInfo.Model
	$row.VM_Host = $esx.Name
	$row.VM_Host_Model = $esx.Hardware.SystemInfo.Model
	$row.VM_Memory = $_.Config.Hardware.memoryMB
	$row.VM_vCpu = $_.Config.Hardware.numCPU
	$row.VM_CpuLimit = $_.Config.CpuAllocation.Limit
	$row.VM_CpuShares = $_.Config.CpuAllocation.Shares.Shares
	$VMReport += $row
}
$VMReport | Export-Csv $outputFileVM -NoTypeInformation

For some more details on this, you may wish to check out: http://www.vmdev.info/?p=125

Thanks,

Keshav

View solution in original post

0 Kudos
16 Replies
Leadership
Leadership

You can replace the sequence

Get-VM | Get-View

by

Get-View -ViewType VirtualMachine

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
Enthusiast
Enthusiast

Thanks LucD,

I know it, but i don't know how to apply in this script.

I'm beginner in PowerCLI...

0 Kudos
Leadership
Leadership

This should make it a bit faster

$outputFileVM = 'D:\PowerShell_scripts\Inventory.csv'
$VMReport = @()
Get-View -ViewType VirtualMachine | Sort-Object -Property Name | %{
	$row = "" | Select-Object VM_Name, VM_Host, VM_Host_Model, VM_Memory, VM_vCpu, VM_CpuLimit, VM_CpuShares
	$row.VM_Name = $_.Name
	$esx = Get-View $_.Runtime.Host
	$row.VM_Host = $esx.Name
	$row.VM_Host_Model = $esx.Hardware.SystemInfo.Model
	$row.VM_Memory = $_.Config.Hardware.memoryMB
	$row.VM_vCpu = $_.Config.Hardware.numCPU
	$row.VM_CpuLimit = $_.Config.CpuAllocation.Limit
	$row.VM_CpuShares = $_.Config.CpuAllocation.Shares.Shares
	$VMReport += $row
}
$VMReport | Export-Csv $outputFileVM -NoTypeInformation

____________

Blog: LucD notes

Twitter: lucd22


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

Immortal
Immortal

To continue LucD's improvements and make it run very fast, make use of get-view's "-Property " parameter and specify the properties of the VirtualMachine or ESX host that should be retrieved. This prevents all the properties you aren't interested in using from being retrieved, and speeds up your code dramatically. In other words, replace your call to "Get-VM" with this:

  Get-View -ViewType VirtualMachine -Property Name,Runtime.Host,Config.Hardware,Config.CpuAllocation

because you are only interested in the Name, Runtime.Host, Config.Hardware, and Config.CpuAllocation properties of VirtualMachine. Replace your call to "Get-VMHost" in a similar fashion.

$outputFileVM = 'D:\PowerShell_scripts\Inventory.csv'
$VMReport = @()
Get-View -ViewType VirtualMachine -Property Name,Runtime.Host,Config.Hardware,Config.CpuAllocation | Sort-Object -Property Name | %{
	$row = "" | Select-Object VM_Name, VM_Host, VM_Host_Model, VM_Memory, VM_vCpu, VM_CpuLimit, VM_CpuShares
	$row.VM_Name = $_.Name
	$esx = Get-View $_.Runtime.Host -Property Name,Hardware.SystemInfo.Model
	$row.VM_Host = $esx.Name
	$row.VM_Host_Model = $esx.Hardware.SystemInfo.Model
	$row.VM_Memory = $_.Config.Hardware.memoryMB
	$row.VM_vCpu = $_.Config.Hardware.numCPU
	$row.VM_CpuLimit = $_.Config.CpuAllocation.Limit
	$row.VM_CpuShares = $_.Config.CpuAllocation.Shares.Shares
	$VMReport += $row
}
$VMReport | Export-Csv $outputFileVM -NoTypeInformation

For some more details on this, you may wish to check out: http://www.vmdev.info/?p=125

Thanks,

Keshav

View solution in original post

0 Kudos
Leadership
Leadership

Great article Keshav and indeed an extra speed improvement.

To give an idea of what these speed improvements are, I ran some tests

Original script                 100.0 %
Get-View                          9.7 %
Get-View & Property               3.9 % 

In other words, Keshav's method is more than 25 times faster than the original script !

____________

Blog: LucD notes

Twitter: lucd22


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

0 Kudos
Enthusiast
Enthusiast

Wow!!!

My script: --> 5 minutes 51 seconds

LucD script --> 1 minutes 4 seconds !!!

Keshav08 script --> 12 seconds !!!

My script is a turtle...

Thanks!!!

0 Kudos
Contributor
Contributor

In the latest script with get-view -viewitem how do i get the following additional info appended? i couldn't figure out from the document from VMW on this property

- list clustername for the esx host

- list guest IP address, guest tools status, vm disk sizes, vm mac address

any help is appreciated.

$outputFileVM = 'c:\tmp\Inventory.csv'
$VMReport = @()
Get-View -ViewType VirtualMachine -Property Name,Runtime.Host,Config.Hardware,Config.CpuAllocation | Sort-Object -Property Name | %{
     $row = "" | Select-Object VM_Name, VM_Host, VM_Host_Model, VM_Memory, VM_vCpu, VM_CpuLimit, VM_CpuShares
     $row.VM_Name = $_.Name
     $esx = Get-View $_.Runtime.Host -Property Name,Hardware.SystemInfo.Model
     $row.VM_Host = $esx.Name
     $row.VM_Host_Model = $esx.Hardware.SystemInfo.Model
     $row.VM_Memory = $_.Config.Hardware.memoryMB
     $row.VM_vCpu = $_.Config.Hardware.numCPU
     $row.VM_CpuLimit = $_.Config.CpuAllocation.Limit
     $row.VM_CpuShares = $_.Config.CpuAllocation.Shares.Shares
     $VMReport += $row
}
$VMReport | Export-Csv $outputFileVM -NoTypeInformation

0 Kudos
Leadership
Leadership

Try this version

$outputFileVM = 'c:\tmp\Inventory.csv' 
$VMReport
= @() Get-View -ViewType VirtualMachine -Property Name,Runtime.Host,Config.Hardware,Config.CpuAllocation,Guest.Net,Guest.ToolsStatus, Guest.ToolsVersionStatus | Sort-Object -Property Name | %{     $row = "" | Select-Object VM_Name, VM_Host, VM_Host_Model, VM_Host_Cluster,VM_Memory, VM_vCpu, VM_CpuLimit, VM_CpuShares, VM_IP, VM_MAC, VM_Tools_Status, VM_Tools_VersionSTatus, VM_Harddisk_SizeKB    $row.VM_Name = $_.Name     $esx = Get-View $_.Runtime.Host -Property Name,Hardware.SystemInfo.Model,Parent     $row.VM_Host = $esx.Name     $row.VM_Host_Model = $esx.Hardware.SystemInfo.Model     $row.VM_Host_Cluster = (Get-View $esx.Parent).Name     $row.VM_Memory = $_.Config.Hardware.memoryMB     $row.VM_vCpu = $_.Config.Hardware.numCPU     $row.VM_CpuLimit = $_.Config.CpuAllocation.Limit     $row.VM_CpuShares = $_.Config.CpuAllocation.Shares.Shares     if($ip = ($_.Guest.Net | %{$_.IpAddress})){         $row.VM_IP = [string]::Join(',',$ip)     }     if(($mac = $_.Guest.Net | %{$_.MacAddress})){         $row.VM_MAC = [string]::Join(',',$mac)     }     $row.VM_Tools_Status = $_.Guest.ToolsStatus     $row.VM_Tools_VersionSTatus = $_.Guest.ToolsVersionStatus     if(($hdSize = $_.Config.Hardware.Device | where {$_.GetType().Name -eq "VirtualDisk"} | %{$_.CapacityInKB})){         $row.VM_Harddisk_SizeKB = [string]::Join(',',$hdSize)     }     $VMReport += $row
}
$VMReport | Export-Csv $outputFileVM -NoTypeInformation


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

0 Kudos
Contributor
Contributor

I have deleted the error message as there was indeed and CR/LF issue that i was finally able to track down. the script appears to be running without errors at this point in time. Once complete and results are verified I will post the final script as attachment for others to use.

0 Kudos
Contributor
Contributor

OK I have a  fully working script now which also does the math to show HDD size in GB and beauty of this script  is that the size includes comma separated values for all the HDDs that are created for a given VM.

Major thanks to LucD for assisting in re-engineering the script.

0 Kudos
Contributor
Contributor

Is it possible to use get-view's filter parameter with multiple properties?

Works:

Get-View -ViewType VirtualMachine -Filter @{"Guest.toolsVersionStatus" = "guestToolsUnmanaged"}

Doesn't work:

Get-View -ViewType VirtualMachine -Filter @{"Guest.toolsVersionStatus" = "guestToolsUnmanaged"} `

-and @{"RunTime.PowerState" = "PoweredOn"}

0 Kudos
Leadership
Leadership

The filter condition(s) need to be passed as a hash table.

A hash table with multiple entries is constructed like this

Get-View -ViewType VirtualMachine `
-Filter @{"Guest.toolsVersionStatus" = "guestToolsUnmanaged";"RunTime.PowerState" = "PoweredOn"}


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

0 Kudos
Contributor
Contributor

That returns an error:

8/22/2011 2:21:39 PM    Get-View              

At :line:1 char:9

+ Get-View  <<<< -ViewType VirtualMachine `

0 Kudos
Contributor
Contributor

Also, is there an equivalent of != for filters?

0 Kudos
Leadership
Leadership

Strange, that works for me without a problem.

Are you sure the copy/paste was ok ?

Nom there is no != operator.

In fact the '=' in the has table is not a comparison operator, it's an assignment operator.

In hash table you assign a value (right operand) to a key (left operand).

Different key-value assignments are separated by a semi-column.


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

0 Kudos
Contributor
Contributor

Good to go. Thanks for the info.

0 Kudos