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.
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
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
Thanks LucD,
I know it, but i don't know how to apply in this script.
I'm beginner in PowerCLI...
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
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
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
Wow!!!
My script: --> 5 minutes 51 seconds
LucD script --> 1 minutes 4 seconds !!!
Keshav08 script --> 12 seconds !!!
My script is a turtle...
Thanks!!!
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
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
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.
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"}
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
That returns an error:
8/22/2011 2:21:39 PM Get-View
At :line:1 char:9
+ Get-View <<<< -ViewType VirtualMachine `
Also, is there an equivalent of != for filters?
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
Good to go. Thanks for the info.