Forum Discussion
Extracting Virtual Machine Information in Azure.
LainRobertson
Hi Lain
Yeah now I'm already started how to script using PowerShell its very difficult from the start but once you already familiarized the syntax its not that difficult anymore. So far my code speed from 20mins to 17sec. But your code is beyond my level. Yeah i will try your code hope it can beat my current 17 sec. 😄
Thanks again. I will try decoding this.
All good. You're doing well so far.
With the re-write above, here's a brief explainer on some of the thought processes behind it - but the general motivators were seeing if we could improve performance:
- Try and use server-side filtering wherever it exists rather than client-side, as when used against larger result sets, it's frequently much faster;
- Consider eliminating as many web calls as is practical to do, as they also can be expensive;
- Consider the size/complexity of the returned object, as that can cause things to slow down both on the server fetching the data as well as the transmission of it back to you - if the result is large enough, of course.
In reality, it's always a case-by-case balancing act as to which combination is better, but here's how those thoughts translated:
- Managed to remove the call to Get-AzVM (relates to point 2 above) by getting more data from the existing call to Get-AzResource (relates to point 3 above);
- On line 18, we brought the "ResourceType" out of the client-side filter (i.e. inside the Where-Object) back over to being a server-side filter by using the "-ResourceType" parameter on Get-AzResource (relates to point 1 above);
- Line 20 - 26: This is an example of something called a "hash table" (starting with "@{" and ending with the closing "}"). The [PSCustomObject] is like an overriding directive saying, "hey, PowerShell! Convert the following hash table to a PSCustomObject for me, please.";
- Lines 9, 14 and 22: PowerShell leverages .NET classes, and this is an example of doing just that. We're creating a new .NET Dictionary class (System.Collections.Generic.Dictionary, line 9) and then adding the Azure private IP addresses to it (line 14). Later (line 22), we're looking up those stored addresses so we can populate the hash table with the value we want in the report.
That's a pretty basic explanation but it may serve as some useful information in your own learning journey!
Cheers,
Lain
Edited for spelling and punctuation.
- Alan2022May 13, 2022Iron Contributor