Gabrie1
Commander
Commander

Select makes script very slow

Jump to solution

Talking to vCloud Director I retrieve a list of VMs with:

$vmlist = get-civm

Then I want to display them in an out-gridview, but that takes forever. Also when I leave out the gridview part it is very slow, takes about 5min to finish. It displays the 5 first VMs and then is processing I don't know what:

$VMList | Select-object Org, vApp, Name, MemoryGB, CPUCount, Status, Description

Just a $vmlist is very fast and displays all the records immediately. Tried to run it in VSCode and PowerShell ISE and gives the same slow result.

Any ideas?

http://www.GabesVirtualWorld.com
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

Did you make a comparison, leaving out the VApp property?

That is a complex object in itself.
Meaning that PS needs to convert that object each time to a String.

Instead of VApp, try using the calculated property @{N='VApp';E={$_.VApp.Name}}


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

View solution in original post

6 Replies
LucD
Leadership
Leadership

There have been reports of the slowness of Get-CIVm.

Perhaps try the REST method as described in Get-CiVm CmdLet is much slower than the direct underlying REST calls


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

Gabrie1
Commander
Commander

When I do the get-civm, that is very fast. And since I load the result into $vmlist, then displaying $vmlist using a select, shouldn't do a query again, should it?

This is the code:

FAST:

$vmlist = get-civm
$vmlist | ft

 

SLOW:

$vmlist | select-object name, vapp, org, memoryGB

 

Doesn't make sense to me why the select-object slows it down so much. 

http://www.GabesVirtualWorld.com
0 Kudos
LucD
Leadership
Leadership

Did you make a comparison, leaving out the VApp property?

That is a complex object in itself.
Meaning that PS needs to convert that object each time to a String.

Instead of VApp, try using the calculated property @{N='VApp';E={$_.VApp.Name}}


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

View solution in original post

Gabrie1
Commander
Commander

Wow @LucD that makes a big difference. Leaving out vApp changes the line from 17 minutes to 5ms.

Is there a way I could have known this? Do you then need to inspect the vApp property or how did you find out?

 

Solution:

$VMLijst | Select-object Org, @{N='VApp';E={$_.VApp.Name}}, Name, MemoryGB, CPUCount, Status, Description

 

 

http://www.GabesVirtualWorld.com
0 Kudos
LucD
Leadership
Leadership

That is where the PowerCLI Reference comes in handy.
If you look up the Get-CIVM cmdlet, under the Output section, you'll see that the cmdlet produces objects of type VMware.VimAutomation.Cloud.Types.V1.CIVM

Clicking that type, shows you what is inside such an object.
When you add an object to an output cmdlet (Select-Object in this case), PS will automatically try to convert that complex object into a String.
It calls the ToString() method on the object, and if that does not exist, it uses a default ToString method.

Converting an object to a String takes time, and I suspect that a property like Status in this object, forces PS to go out again to collect the 'current' status.
Again adding to the execution time.

In short, never output objects, but be specific, like the $_.VApp.Name in this case, which is a String.


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

0 Kudos
Gabrie1
Commander
Commander

Thanks !!!

http://www.GabesVirtualWorld.com
0 Kudos