I’m observing a strange performance hit in one my Powershell scripts when I import some Az modules.
My script is quite complex (4k lines) so it’s a bit of a pain to try and isolate precisely what went wrong between the previous version and the new one I uploaded to the server recently. The script aggregates several data sources and produces a large report on 75k devices. The main part of the script is a giant loop that processes all devices one by one by retrieving device information from each data source. I use hash tables to make the lookups faster. Let’s call this script’s version v1.
I recently added a new data source (Azure) to get VDI information for all Azure Virtual Desktop (AVD) session hosts and Azure VMs. To do that, I use several cmdlets from the Az.Accounts
, Az.DesktopVirtualization
and Az.Compute
Powershell modules. Most notably Get-AzWvdSessionHost
and Get-AzVM
This is script version v2.
V1 used to take roughly 150ms per device. Now, with AVD data, v2 takes 450ms.
I have since determined that if I simply add the below line at the top of v1 (and change nothing else), v1 becomes as slow as v2:
Import-Module -Name 'Az.Accounts', 'Az.DesktopVirtualization', 'Az.Compute'
This seems to indicate that the Az modules are somehow responsible for the performance degradation. I even tried Remove-Module -Name 'Az.Accounts', 'Az.DesktopVirtualization', 'Az.Compute'
right after the import line, but I still observed the performance decline, which seems to indicate that the modules are probably still loaded in the current session somehow, and are having a big impact.
I really can’t explain this strange behaviour and haven’t been able to find any clue online so far.
I guess I could avoid the Az modules completely and query the Azure API endpoints directly with Invoke-RestMethod
, but I would prefer avoiding this !
Any suggestions ?
Thanks in advance