VMware Cloud Community
ewingfox
Contributor
Contributor

PowerCLI performance vs clicking 'Export' button in vsphere UI.

Hello!

I'm in the process of trying to automate some report gathering and data massaging that I've been tasked to do.  This has led me down the road into PowerCLI, and many posts and answers (especially LuciD's) have been very helpful! While I have several tasks that are pulling specific information about my cluster's health, etc - I've been asked to automate the 'Export' button.  I have a bit over 1800 machines in the cluster, and the 'export' button takes about half a second to generate what I'm looking for - but the same report (still missing a few columns, I'm a total newb) takes HOURS to run.  Am I missing something?  

If someone has figured out an efficient way to create this .csv file progmramatically? 

 

Thank you in advance,


Ewing

Reply
0 Kudos
2 Replies
LucD
Leadership
Leadership

Most of these reports are run on an interval, so the moment you click Export, you get the latest version.
A script on the other hand collects the actual state at the time it runs.

I would need to see the code you actually use to see if the execution time can be improved.


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

Reply
0 Kudos
ewingfox
Contributor
Contributor

I've done something as basic as this:

Get-VM | Export-Csv -path "c:\PowerBI\vminventory.csv" -NoTypeInformation

and more complex like this (see below)  - both have multi-hour runtimes - any help would be appreciated!

Function Main

{

    Try

    {

        $vms = Get-VM

        $output = foreach($vm in $vms)

        {

            $vm | select Name,

                PowerState,

                VmHost,

                ProvisionedSpaceGB,

                UsedSpaceGB,

                NumCpu,

                MemoryMB,

                Notes,

                DNSAddress,

                 @{N="Up Time (d.hh:mm:ss)";E={[timespan]::FromSeconds((Get-Stat -Entity $vm.Name -Stat sys.uptime.latest -Realtime -MaxSamples 1).Value)}},

                 @{N='MemAvgPercent';E={$script:stats | where{$_.MetricId -eq 'mem.usage.average'} | Select -ExpandProperty Value}},      

                 @{N='FQDN';E={$vm.ExtensionData.Guest.IPStack[0].DnsConfig.HostName, $vm.ExtensionData.Guest.IPStack[0].DnsConfig.DomainName -join '.'}},

                 @{Name='ToolsVersion';Expression={$_.Guest.ToolsVersion}},

                 @{N = "VMNICCount"; E = {$vm.Networkadapters.count}},

                 @{N="Tools Status";E={$vm.ExtensionData.Guest.ToolsStatus}},

                 @{N="Configured OS";E={$_.Config.GuestFullName}},

                 @{N="Running OS";E={$_.Guest.GuestFullName}}

               

        }

        $output | Export-Csv -Path "C:\PowerBI\Vmware_Export_Full.csv " 

    }

    catch [Exception]

    {

        $exception = $_.Exception

        Write-Host "Error - $exception" -foreground red

        Exit

    }

}

Function Main
{
    Try
    {
        $vms = Get-VM
        $output = foreach($vm in $vms)
        {
            $vm | select Name,
                PowerState,
                VmHost,
                ProvisionedSpaceGB,
                UsedSpaceGB,
                NumCpu,
                MemoryMB,
                Notes,
                DNSAddress,
                 @{N="Up Time (d.hh:mm:ss)";E={[timespan]::FromSeconds((Get-Stat -Entity $vm.Name -Stat sys.uptime.latest -Realtime -MaxSamples 1).Value)}},
                 @{N='MemAvgPercent';E={$script:stats | where{$_.MetricId -eq 'mem.usage.average'} | Select -ExpandProperty Value}},      
                 @{N='FQDN';E={$vm.ExtensionData.Guest.IPStack[0].DnsConfig.HostName, $vm.ExtensionData.Guest.IPStack[0].DnsConfig.DomainName -join '.'}},
                 @{Name='ToolsVersion';Expression={$_.Guest.ToolsVersion}},
                 @{N = "VMNICCount"; E = {$vm.Networkadapters.count}},
                 @{N="Tools Status";E={$vm.ExtensionData.Guest.ToolsStatus}},
                 @{N="Configured OS";E={$_.Config.GuestFullName}},
                 @{N="Running OS";E={$_.Guest.GuestFullName}}
               
        }
        $output | Export-Csv -Path "C:\PowerBI\Vmware_Export_Full.csv " 
    }
    catch [Exception]
    {
        $exception = $_.Exception
        Write-Host "Error - $exception" -foreground red
        Exit
    }
}
Reply
0 Kudos