Extracting Virtual Machine Information in Azure.

%3CLINGO-SUB%20id%3D%22lingo-sub-3362157%22%20slang%3D%22en-US%22%3EExtracting%20Virtual%20Machine%20Information%20in%20Azure.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3362157%22%20slang%3D%22en-US%22%3E%3CP%3EI%20just%20want%20to%20shared%20my%20modified%20code%20in%20azure%20vm%20information%20extraction.%3CBR%20%2F%3E%3CBR%20%2F%3EData%20will%20be%20the%20list%20of%26nbsp%3BVMName%2C%20IPAddress%2C%20ResourceGroup%2C%20VmSize%20%26amp%3B%20Tag.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%20User%20Authentication%0A%24ua%20%3D%20Get-StoredCredential%20-Target%20AzureAccount%0A%24credential%20%3D%20New-Object%20-TypeName%20%22System.Management.Automation.PSCredential%22%20-ArgumentList%20%24ua.UserName%2C%24ua.Password%0A%0A%23%20Login%20to%20your%20Azure%20Account%0AConnect-AzAccount%20-Tenant%20'%3CTENANT%20id%3D%22%22%3E'%20-Credential%20%24credential%0A%0A%23%20Get%20All%20Virtual%20Machines%20%26amp%3B%20VMSize%0A%24resultVMs%20%3D%20Get-AzVM%20%7C%20Where-Object%20%7B%20%24_.Name%20-and%20%24_.ResourceGroupName%20-and%20%24_.HardwareProfile.VmSize%20%7D%20%60%0A%7C%20Select-Object%20-Property%20%40%7Bn%3D%22Resource%22%3B%20e%3D%7B%20%24_.ResourceGroupName%20%7D%7D%2C%20%40%7Bn%3D%22VMName%22%3B%20e%3D%7B%20%24_.Name%20%7D%7D%2C%20%40%7Bn%3D%22VmSize%22%3B%20e%3D%7B%20%24_.HardwareProfile.VmSize%20%7D%7D%20%60%0A%0A%23%20Get%20All%20Virtual%20Machines%20%26amp%3B%20WorkNo%20Tag%0A%24resultTags%20%3D%20Get-AzResource%20-ResourceType%20%22Microsoft.Compute%2FvirtualMachines%22%20%60%0A%7C%20Where-Object%20%7B%20%24_.Name%20-and%20%24_.ResourceGroupName%20-and%20%24_.Tags.WorkNo%20%7D%20%60%0A%7C%20Select-Object%20-Property%20%40%7Bn%3D%22Resource%22%3B%20e%3D%7B%20%24_.ResourceGroupName%20%7D%7D%2C%20%40%7Bn%3D%22VMName%22%3B%20e%3D%7B%20%24_.Name%20%7D%7D%2C%20%40%7Bn%3D%22Tags%22%3B%20e%3D%7B%20%24_.Tags.WorkNo%20%7D%7D%20%60%0A%0A%23%20Get%20All%20Virtual%20Machines%20%26amp%3B%20Ip%20Address%0A%24resultIPAdds%20%3D%20Get-AzNetworkInterface%20%7C%20Where-Object%20%7B%20%24_.ResourceGroupName%20-and%20%24_.VirtualMachine%20-and%20%24_.IPConfigurations%20-and%20%24_.IPConfigurations.PrivateIPAddress%20%7D%20%60%0A%7C%20Select-Object%20-Property%20%40%7Bn%3D%22Resource%22%3B%20e%3D%7B%20%24_.ResourceGroupName%20%7D%7D%2C%20%40%7Bn%3D%22VMName%22%3B%20e%3D%7B%20%24_.VirtualMachine.Id.Split(%22%2F%22)%5B-1%5D%20%7D%7D%2C%20%40%7Bn%3D%22PrivateIPAddress%22%3B%20e%3D%20%7B%20%24_.IpConfigurations.PrivateIPAddress%20%7D%7D%20%60%0A%0A%23%20Create%20Report%20Array%0A%24report%20%3D%20%40()%0A%0A%23%20Loop%20All%20VM's%0Aforeach(%24resultVM%20in%20%24resultVMs)%7B%0A%0A%20%20%20%20%23%20Creating%20Report%20Header%0A%20%20%20%20%24reportdetails%20%3D%20%22%22%20%7C%20Select%20VMName%2C%20IPAddress%2C%20ResourceGroup%2C%20VmSize%2C%20Tag%0A%0A%20%20%20%20%23%20Save%20the%20VM%20Name%0A%20%20%20%20%24reportdetails.VMName%20%3D%20%24resultVM.VMName%0A%0A%20%20%20%20%23%20Save%20the%20Resource%20Group%20Name%0A%20%20%20%20%24reportdetails.ResourceGroup%20%3D%20%24resultVM.Resource%0A%0A%20%20%20%20%23%20Save%20the%20VmSize%20%0A%20%20%20%20%24reportdetails.VmSize%20%3D%20%24resultVM.VmSize%0A%0A%20%20%20%20%23%20Save%20the%20IP%20Address%0A%20%20%20%20%24temp%20%3D%20%40((%24resultIPAdds%20%7C%20Where-Object%20%7B%20%24_.%22VMName%22%20-match%20%24resultVM.VMName%20%7D).PrivateIPAddress)%0A%20%20%20%20%24reportdetails.IPAddress%20%3D%20%24temp%5B0%5D%0A%0A%20%20%20%20%23%20Save%20the%20Tag%0A%20%20%20%20%24temp%20%3D%20%40((%24resultTags%20%7C%20Where-Object%20%7B%20%24_.%22VMName%22%20-match%20%24resultVM.VMName%20%7D).Tags)%0A%20%20%20%20%24reportdetails.Tag%20%3D%20%24temp%5B0%5D%0A%0A%20%20%20%20%23%20Save%20Report%0A%20%20%20%20%24report%2B%3D%24reportdetails%0A%7D%0A%0A%23%20Generate%20Report%0A%24report%20%7C%20Export-Excel%20%22.%5CDesktop%5CVMList_%20%24(get-date%20-f%20yyyy.MM.dd.HH.mm.ss).xlsx%22%0AWrite-Host%20%22Finished...%22%20%3C%2FTENANT%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CBR%20%2F%3EHope%20it%20can%20help%20others%20too.%20%3Abeaming_face_with_smiling_eyes%3A%3Abeaming_face_with_smiling_eyes%3A%3Abeaming_face_with_smiling_eyes%3A.%3CBR%20%2F%3E%3CBR%20%2F%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-3362157%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EAzure%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EAzure%20Automation%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EWindows%20PowerShell%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3363249%22%20slang%3D%22en-US%22%3ERe%3A%20Extracting%20Virtual%20Machine%20Information%20in%20Azure.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3363249%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1381637%22%20target%3D%22_blank%22%3E%40Alan2022%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHey%2C%20Alan.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIt's%20good%20to%20see%20the%20final%20version%20of%20what%20you%20were%20working%20towards%20with%20the%20previous%20questions%20-%20well%20done!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EHere's%20a%20variation%20of%20your%20finished%20product%20that%20will%20work%20for%20others%20courtesy%20of%20some%20minor%20changes.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3COL%3E%3CLI%3EUsing%20Export-Csv%20rather%20than%20Export-Excel%2C%20as%20Export-Csv%20exists%20%22out%20of%20the%20box%22%3B%3C%2FLI%3E%3CLI%3ERemove%20%22Desktop%22%20from%20the%20Csv%20export%20path%20as%20that%20will%20fail%20for%20anyone%20that%20doesn't%20have%20a%20subfolder%20named%20%22Desktop%22.%3C%2FLI%3E%3C%2FOL%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20not%20currently%20working%20in%20an%20Azure-facing%20role%20meaning%20I%20can't%20see%20how%20well%20this%20alternative%20scales%20performance-wise%20-%20I%20only%20have%20visibility%20of%20a%20small%20number%20of%20Azure%20virtual%20machines.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat%20said%2C%20it%20will%20consume%20less%20memory%20as%20it%20is%20working%20from%20the%20pipeline%20rather%20than%20from%20data%20stored%20in%20variables%20-%20with%20the%20unavoidable%20exception%20of%20the%20private%20IP%20address%2C%20since%20they're%20not%20exposed%20via%20Get-AzResource.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThis%20reduces%20the%20overhead%20from%20three%20variables%20storing%20the%20full%20set%20of%20Azure%20VM%20data%20down%20to%20one%2C%20while%20the%20rest%20can%20be%20released%20as%20soon%20as%20they've%20been%20processed%20meaning%20memory%20utilisation%20won't%20scale%20as%20steeply%20in%20larger%20environments.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAnyhow%2C%20give%20it%20a%20try%20and%20let%20me%20know%20how%20it%20compares%20from%20a%20run%20time%20perspective.%20I'm%20hoping%20it's%20favourable%20or%20better%20but%20since%20using%20%22-ExpandProperties%22%20results%20in%20more%20data%20being%20returned%2C%20it'll%20be%20interesting%20to%20see%20if%20that%20outweighs%20the%20benefit%20of%20removing%20the%20separate%20call%20to%20Get-AzVM.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%23%20User%20Authentication%0A%24ua%20%3D%20Get-StoredCredential%20-Target%20AzureAccount%0A%24credential%20%3D%20New-Object%20-TypeName%20%22System.Management.Automation.PSCredential%22%20-ArgumentList%20%24ua.UserName%2C%24ua.Password%0A%0A%23%20Login%20to%20your%20Azure%20Account%0AConnect-AzAccount%20-Tenant%20'%3CTENANT%20id%3D%22%22%3E'%20-Credential%20%24credential%0A%0A%23%20Build%20a%20dictionary%20of%20private%20IP%20addresses%2C%20given%20they%20can't%20be%20obtained%20via%20Get-AzResource%20-ExpandProperties.%0A%5BSystem.Collections.Generic.Dictionary%5Bint%2C%20string%5D%5D%20%24PrivateAddressesById%20%3D%20%5BSystem.Collections.Generic.Dictionary%5Bint%2C%20string%5D%5D%3A%3Anew()%3B%0A%0AGet-AzNetworkInterface%20%7C%0A%20%20%20%20Where-Object%20%7B%20%24_.ResourceGroupName%20-and%20%24_.VirtualMachine%20-and%20%24_.IPConfigurations%20-and%20%24_.IPConfigurations.PrivateIPAddress%20%7D%20%7C%0A%20%20%20%20%20%20%20%20ForEach-Object%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%24PrivateAddressesById.Add(%24_.Id.GetHashCode()%2C%20(%24_.IpConfigurations.PrivateIPAddress%20-join%20%22%2C%20%22))%3B%0A%20%20%20%20%20%20%20%20%7D%0A%0A%23%20Now%2C%20fetch%20the%20virtual%20machine%20information%2C%20making%20use%20of%20the%20speedy%20dictionary%20lookups%20to%20flesh%20out%20the%20IPAddress%20data.%0AGet-AzResource%20-ResourceType%20%22Microsoft.Compute%2FvirtualMachines%22%20-ExpandProperties%20%7C%0A%20%20%20%20ForEach-Object%20%7B%0A%20%20%20%20%20%20%20%20%5BPSCustomObject%5D%40%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20VMName%20%3D%20%24_.Name%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20IPAddress%20%3D%20%24PrivateAddressesById%5B(%24_.Properties.networkProfile.networkInterfaces.id.GetHashCode())%5D%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20ResourceGroup%20%3D%20%24_.ResourceGroupName%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20VmSize%20%3D%20%24_.Properties.hardwareProfile.vmSize%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20Tag%20%3D%20%24_.Tags%5B%22WorkNo%22%5D%3B%0A%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%7D%20%7C%20Export-Csv%20-NoTypeInformation%20-Path%20%22.%5CVMList_%24(%5Bdatetime%5D%3A%3ANow.ToString(%22yyyy.MM.dd.HH.mm.ss%22)).csv%22%3B%0A%0A%23%20Clean%20up%20and%20bail.%0A%24PrivateAddressesById.Clear()%3B%0A%5Bgc%5D%3A%3ACollect()%3B%0A%0AWrite-Host%20%22Finished...%22%3C%2FTENANT%3E%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECheers%2C%3C%2FP%3E%3CP%3ELain%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3367895%22%20slang%3D%22en-US%22%3ERe%3A%20Extracting%20Virtual%20Machine%20Information%20in%20Azure.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3367895%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F314983%22%20target%3D%22_blank%22%3E%40LainRobertson%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3EHi%20Lain%3CBR%20%2F%3E%3CBR%20%2F%3EYeah%20now%20I'm%20already%20started%20how%20to%20script%20using%20PowerShell%20its%20very%20difficult%20from%20the%20start%20but%20once%20you%20already%20familiarized%20the%20syntax%20its%20not%20that%20difficult%20anymore.%20So%20far%20my%20code%20speed%20from%2020mins%20to%2017sec.%20But%20your%20code%20is%20beyond%20my%20level.%20Yeah%20i%20will%20try%20your%20code%20hope%20it%20can%20beat%20my%20current%2017%20sec.%20%3AD%3C%2Fimg%3E%3CBR%20%2F%3EThanks%20again.%20I%20will%20try%20decoding%20this.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3367961%22%20slang%3D%22en-US%22%3ERe%3A%20Extracting%20Virtual%20Machine%20Information%20in%20Azure.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3367961%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1381637%22%20target%3D%22_blank%22%3E%40Alan2022%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAll%20good.%20You're%20doing%20well%20so%20far.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EWith%20the%20re-write%20above%2C%20here's%20a%20brief%20explainer%20on%20some%20of%20the%20thought%20processes%20behind%20it%20-%20but%20the%20general%20motivators%20were%20seeing%20if%20we%20could%20improve%20performance%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3COL%3E%3CLI%3ETry%20and%20use%20server-side%20filtering%20wherever%20it%20exists%20rather%20than%20client-side%2C%20as%20when%20used%20against%20larger%20result%20sets%2C%20it's%20frequently%20much%20faster%3B%3C%2FLI%3E%3CLI%3EConsider%20eliminating%20as%20many%20web%20calls%20as%20is%20practical%20to%20do%2C%20as%20they%20also%20can%20be%20expensive%3B%3C%2FLI%3E%3CLI%3EConsider%20the%20size%2Fcomplexity%20of%20the%20returned%20object%2C%20as%20that%20can%20cause%20things%20to%20slow%20down%20both%20on%20the%20server%20fetching%20the%20data%20as%20well%20as%20the%20transmission%20of%20it%20back%20to%20you%20-%20if%20the%20result%20is%20large%20enough%2C%20of%20course.%3C%2FLI%3E%3C%2FOL%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EIn%20reality%2C%20it's%20always%20a%20case-by-case%20balancing%20act%20as%20to%20which%20combination%20is%20better%2C%20but%20here's%20how%20those%20thoughts%20translated%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CUL%3E%3CLI%3EManaged%20to%20remove%20the%20call%20to%20Get-AzVM%20(relates%20to%20point%202%20above)%20by%20getting%20more%20data%20from%20the%20existing%20call%20to%20Get-AzResource%20(relates%20to%20point%203%20above)%3B%3C%2FLI%3E%3CLI%3EOn%20line%2018%2C%20we%20brought%20the%20%22ResourceType%22%20out%20of%20the%20client-side%20filter%20(i.e.%20inside%20the%20Where-Object)%20back%20over%20to%20being%20a%20server-side%20filter%20by%20using%20the%20%22-ResourceType%22%20parameter%20on%20Get-AzResource%20(relates%20to%20point%201%20above)%3B%3C%2FLI%3E%3CLI%3ELine%2020%20-%2026%3A%20This%20is%20an%20example%20of%20something%20called%20a%20%22hash%20table%22%20(starting%20with%20%22%40%7B%22%20and%20ending%20with%20the%20closing%20%22%7D%22).%20The%20%5BPSCustomObject%5D%20is%20like%20an%20overriding%20directive%20saying%2C%20%22hey%2C%20PowerShell!%20Convert%20the%20following%20hash%20table%20to%20a%20PSCustomObject%20for%20me%2C%20please.%22%3B%3C%2FLI%3E%3CLI%3ELines%209%2C%2014%20and%2022%3A%20PowerShell%20leverages%20.NET%20classes%2C%20and%20this%20is%20an%20example%20of%20doing%20just%20that.%20We're%20creating%20a%20new%20.NET%20Dictionary%20class%20(System.Collections.Generic.Dictionary%2C%20line%209)%20and%20then%20adding%20the%20Azure%20private%20IP%20addresses%20to%20it%20(line%2014).%20Later%20(line%2022)%2C%20we're%20looking%20up%20those%20stored%20addresses%20so%20we%20can%20populate%20the%20hash%20table%20with%20the%20value%20we%20want%20in%20the%20report.%3C%2FLI%3E%3C%2FUL%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThat's%20a%20pretty%20basic%20explanation%20but%20it%20may%20serve%20as%20some%20useful%20information%20in%20your%20own%20learning%20journey!%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ECheers%2C%3C%2FP%3E%3CP%3ELain%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EEdited%20for%20spelling%20and%20punctuation.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-3367993%22%20slang%3D%22en-US%22%3ERe%3A%20Extracting%20Virtual%20Machine%20Information%20in%20Azure.%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-3367993%22%20slang%3D%22en-US%22%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F314983%22%20target%3D%22_blank%22%3E%40LainRobertson%3C%2FA%3E%3CBR%20%2F%3E%3CBR%20%2F%3EHi%20Lain%3CBR%20%2F%3E%3CBR%20%2F%3EThanks%20for%20the%20tips.%3CBR%20%2F%3EI%20will%20always%20remember%20that.%3CBR%20%2F%3E%3Athumbs_up%3A%3Athumbs_up%3A%3Athumbs_up%3A%3CBR%20%2F%3E%3C%2FLINGO-BODY%3E
Occasional Contributor

I just want to shared my modified code in azure vm information extraction.

Data will be the list of VMName, IPAddress, ResourceGroup, VmSize & Tag.

 

# User Authentication
$ua = Get-StoredCredential -Target AzureAccount
$credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $ua.UserName,$ua.Password

# Login to your Azure Account
Connect-AzAccount -Tenant '<Tenant ID>' -Credential $credential

# Get All Virtual Machines & VMSize
$resultVMs = Get-AzVM | Where-Object { $_.Name -and $_.ResourceGroupName -and $_.HardwareProfile.VmSize } `
| Select-Object -Property @{n="Resource"; e={ $_.ResourceGroupName }}, @{n="VMName"; e={ $_.Name }}, @{n="VmSize"; e={ $_.HardwareProfile.VmSize }} `

# Get All Virtual Machines & WorkNo Tag
$resultTags = Get-AzResource -ResourceType "Microsoft.Compute/virtualMachines" `
| Where-Object { $_.Name -and $_.ResourceGroupName -and $_.Tags.WorkNo } `
| Select-Object -Property @{n="Resource"; e={ $_.ResourceGroupName }}, @{n="VMName"; e={ $_.Name }}, @{n="Tags"; e={ $_.Tags.WorkNo }} `

# Get All Virtual Machines & Ip Address
$resultIPAdds = Get-AzNetworkInterface | Where-Object { $_.ResourceGroupName -and $_.VirtualMachine -and $_.IPConfigurations -and $_.IPConfigurations.PrivateIPAddress } `
| Select-Object -Property @{n="Resource"; e={ $_.ResourceGroupName }}, @{n="VMName"; e={ $_.VirtualMachine.Id.Split("/")[-1] }}, @{n="PrivateIPAddress"; e= { $_.IpConfigurations.PrivateIPAddress }} `

# Create Report Array
$report = @()

# Loop All VM's
foreach($resultVM in $resultVMs){

    # Creating Report Header
    $reportdetails = "" | Select VMName, IPAddress, ResourceGroup, VmSize, Tag

    # Save the VM Name
    $reportdetails.VMName = $resultVM.VMName

    # Save the Resource Group Name
    $reportdetails.ResourceGroup = $resultVM.Resource

    # Save the VmSize 
    $reportdetails.VmSize = $resultVM.VmSize

    # Save the IP Address
    $temp = @(($resultIPAdds | Where-Object { $_."VMName" -match $resultVM.VMName }).PrivateIPAddress)
    $reportdetails.IPAddress = $temp[0]

    # Save the Tag
    $temp = @(($resultTags | Where-Object { $_."VMName" -match $resultVM.VMName }).Tags)
    $reportdetails.Tag = $temp[0]

    # Save Report
    $report+=$reportdetails
}

# Generate Report
$report | Export-Excel ".\Desktop\VMList_ $(get-date -f yyyy.MM.dd.HH.mm.ss).xlsx"
Write-Host "Finished..." 

 


Hope it can help others too. :beaming_face_with_smiling_eyes::beaming_face_with_smiling_eyes::beaming_face_with_smiling_eyes:.

4 Replies

@Alan2022 

 

Hey, Alan.

 

It's good to see the final version of what you were working towards with the previous questions - well done!

 

Here's a variation of your finished product that will work for others courtesy of some minor changes.

 

  1. Using Export-Csv rather than Export-Excel, as Export-Csv exists "out of the box";
  2. Remove "Desktop" from the Csv export path as that will fail for anyone that doesn't have a subfolder named "Desktop".

 

I'm not currently working in an Azure-facing role meaning I can't see how well this alternative scales performance-wise - I only have visibility of a small number of Azure virtual machines.

 

That said, it will consume less memory as it is working from the pipeline rather than from data stored in variables - with the unavoidable exception of the private IP address, since they're not exposed via Get-AzResource.

 

This reduces the overhead from three variables storing the full set of Azure VM data down to one, while the rest can be released as soon as they've been processed meaning memory utilisation won't scale as steeply in larger environments.

 

Anyhow, give it a try and let me know how it compares from a run time perspective. I'm hoping it's favourable or better but since using "-ExpandProperties" results in more data being returned, it'll be interesting to see if that outweighs the benefit of removing the separate call to Get-AzVM.

 

# User Authentication
$ua = Get-StoredCredential -Target AzureAccount
$credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $ua.UserName,$ua.Password

# Login to your Azure Account
Connect-AzAccount -Tenant '<Tenant ID>' -Credential $credential

# Build a dictionary of private IP addresses, given they can't be obtained via Get-AzResource -ExpandProperties.
[System.Collections.Generic.Dictionary[int, string]] $PrivateAddressesById = [System.Collections.Generic.Dictionary[int, string]]::new();

Get-AzNetworkInterface |
    Where-Object { $_.ResourceGroupName -and $_.VirtualMachine -and $_.IPConfigurations -and $_.IPConfigurations.PrivateIPAddress } |
        ForEach-Object {
            $PrivateAddressesById.Add($_.Id.GetHashCode(), ($_.IpConfigurations.PrivateIPAddress -join ", "));
        }

# Now, fetch the virtual machine information, making use of the speedy dictionary lookups to flesh out the IPAddress data.
Get-AzResource -ResourceType "Microsoft.Compute/virtualMachines" -ExpandProperties |
    ForEach-Object {
        [PSCustomObject]@{
            VMName = $_.Name;
            IPAddress = $PrivateAddressesById[($_.Properties.networkProfile.networkInterfaces.id.GetHashCode())];
            ResourceGroup = $_.ResourceGroupName;
            VmSize = $_.Properties.hardwareProfile.vmSize;
            Tag = $_.Tags["WorkNo"];
        }
    } | Export-Csv -NoTypeInformation -Path ".\VMList_$([datetime]::Now.ToString("yyyy.MM.dd.HH.mm.ss")).csv";

# Clean up and bail.
$PrivateAddressesById.Clear();
[gc]::Collect();

Write-Host "Finished..."

 

Cheers,

Lain

@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. :D
Thanks again. I will try decoding this.

@Alan2022 

 

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:

 

  1. 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;
  2. Consider eliminating as many web calls as is practical to do, as they also can be expensive;
  3. 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.

@LainRobertson

Hi Lain

Thanks for the tips.
I will always remember that.
:thumbs_up::thumbs_up::thumbs_up: