VMware Cloud Community
pizzim13
Contributor
Contributor

How to speedup get-stat?

I am trying to get stats on ~800 vm in about 5 five minutes. The following command returns output in about 0.91 seconds per VM

$vm | Get-Stat -Realtime -MaxSamples 1

where $vm is a VM object. If I run this as psjob, each job has to load the snapin and connect to the viserver, adding to the time to complete. Is there any way to pass an authenicated session to a job? Is there a faster way to do this?

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership

What is in $vm ? A single VM ?

You can collect the stats for all the VMs in 1 go and then use for example the Group-Object cmdlet to split out the results per VM.

Can you give some more details on what you are actually trying to do ?


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

pizzim13
Contributor
Contributor

Using goo.gl/fDVBd as an example, I broke my array of VM names into smaller pieces and started a job on each one. Without including the time suffered by the .NET compiling goo.gl/jMfMZ, I can query 850 VMs in three minutes instead of 15.

$VMs = Get-View -ViewType "VirtualMachine" -Property Name -Filter @{"Runtime.PowerState"="PoweredOn"} | Select-Object -ExpandProperty Name

$ArrayDivisor = 6
$VMLists = @{}
$Counter = 0
$VMs | ForEach-Object {$VMLists[$Counter % $ArrayDivisor] += @($_);$Counter++}
0..($ArrayDivisor-1) | ForEach-Object `
     {
     Start-Job -ScriptBlock {
                            param ($VIServer, $VMLists, $Creds)
                            Add-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue
                            Connect-VIServer -Server $VIServer -Credential $Creds | Out-Null
                            $VMObjs = get-vm -Name $VMLists
                            $VMStats = $VMObjs | Get-Stat -Realtime -MaxSamples 1
                            $VMStats | Export-Csv -Path "c:\temp\$(get-random).csv" #For testing only
                            Disconnect-VIServer -Force -Confirm:$false
                            } -ArgumentList ($VIServer, $VMLists[$_], $Creds)
     }

Reply
0 Kudos
LucD
Leadership
Leadership

Did you also time a run with only 1 call to Get-Stat, passing all the VMs in that 1 call ?

Wouldn't the script be faster if you do it like this

$VMs = Get-VM | where {$_.PowerState -eq "PoweredOn"}

Get-Stat -Entity $VMs -Realtime -MaxSamples 1 |
Export-Csv -Path "c:\temp\$(get-random).csv" 

Only 1 Get-VM and 1 Get-Stat call.


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

pizzim13
Contributor
Contributor

For 848 VMs, the total run time is ~13.5 minutes

Reply
0 Kudos