Understanding Why Code Is Slow

I believe this article has to do with the issue i’m experiencing but I don’t really understand it. http://blogs.vmware.com/PowerCLI/2009/03/why-is-my-code-so-slow.html


Basically, why is $VM faster than $VM.name? If $VM = “TEST1” and vice versa for $VM.name aren’t those both strings and therefore should result in an object look up? I also noticed that the code is way faster when running from SITEA(Local) instead of SITEB. I’m assuming this is because “All filtering is done on the client side”?


Would it be better/faster to use something like this instead of importing a CSV?

 $Details_ = @' Name,Template,OSCustomization Test1,Temp1,OS2 Test2,Temp1,OS2 '@| ConvertFrom-Csv 

Using another method other than importing a CSV and calling out it’s properties seems to shave some time. Maybe I could get away with it using the above option(ConvertFrom-CSV)?

Currently fastest working option(Same time benchmark as performed in GUI):

 $VMs = ("TEST1") foreach ($vm in $vms){ $VMHost = (Get-Datacenter "SITEA"| Get-Cluster "Cluster"| Get-VMHost "SITEA_esxi") $Datastore = (Get-Datacenter "SITEA"| Get-Cluster "Cluster"| Get-Datastore "SITEA_DS") $Template = (Get-datacenter "SITEA"| Get-template "temp1") $OSCustomization = Get-OSCustomizationSpec "OS2" New-VM -Name $vm -OSCustomizationSpec $OSCustomization ` -template $Template -VMHost $VMHost -Datastore $Datastore ` -RunAsync } 

submitted by /u/EndlessPain11616
[link] [comments]

Leave a Reply