Modules/Public/InventoryModules/Compute/VirtualMachine.ps1
|
<# .Synopsis Inventory for Azure Virtual Machine .DESCRIPTION This script consolidates information for all microsoft.compute/virtualmachines resource provider in $Resources variable. Excel Sheet Name: Virtual Machines .Link https://github.com/microsoft/ARI/Modules/Public/InventoryModules/Compute/VirtualMachine.ps1 .COMPONENT This powershell Module is part of Azure Resource Inventory (ARI) .NOTES Version: 3.6.15 First Release Date: 19th November, 2020 Authors: Claudio Merola and Renato Gregio #> <######## Default Parameters. Don't modify this ########> param($SCPath, $Sub, $Intag, $Resources, $Retirements, $Task, $File, $SmaResources, $TableStyle, $Unsupported) If ($Task -eq 'Processing') { $vm = $Resources | Where-Object {$_.TYPE -eq 'microsoft.compute/virtualmachines'} $nic = $Resources | Where-Object {$_.TYPE -eq 'microsoft.network/networkinterfaces'} $vmexp = $Resources | Where-Object {$_.TYPE -eq 'microsoft.compute/virtualmachines/extensions'} $disk = $Resources | Where-Object {$_.TYPE -eq 'microsoft.compute/disks'} $VirtualNetwork = $Resources | Where-Object { $_.TYPE -eq 'microsoft.network/virtualnetworks' } $PPG = $Resources | Where-Object {$_.TYPE -eq 'microsoft.compute/proximityplacementgroups'} $VMExtraDetails = $Resources | Where-Object { $_.TYPE -eq 'ARI/VM/SKU' } $VMQuotas = $Resources | Where-Object { $_.TYPE -eq 'ARI/VM/Quotas' } # Lookup indexes, built once. Each per-VM loop below runs its original match logic # over the indexed candidates instead of every NIC, disk, extension and VNET. function Add-ARIIndexItem ($Index, $Key, $Item) { if ($Key -isnot [string] -or $Key -eq '') { return } if (!$Index.ContainsKey($Key)) { $Index[$Key] = [System.Collections.Generic.List[object]]::new() } if ($Index[$Key].Count -and [object]::ReferenceEquals($Index[$Key][-1], $Item)) { return } $Index[$Key].Add($Item) } function Get-ARIIndexItem ($Index, $Key) { if ($Key -is [string] -and $Index.ContainsKey($Key)) { $Index[$Key] } } $SubIndex = @{}; foreach ($Item in $SUB) { Add-ARIIndexItem $SubIndex ([string]$Item.id) $Item } $NicIndex = @{}; foreach ($Item in $nic) { Add-ARIIndexItem $NicIndex $Item.id $Item } $ExtIndex = @{}; foreach ($Item in $vmexp) { Add-ARIIndexItem $ExtIndex ($Item.id -split "/")[8] $Item } $RetirementIndex = @{}; foreach ($Item in $Retirements) { Add-ARIIndexItem $RetirementIndex $Item.id $Item } $PPGIndex = @{}; foreach ($Item in $PPG) { foreach ($Key in $Item.properties.virtualMachines.id) { Add-ARIIndexItem $PPGIndex $Key $Item } } $VnetIndex = @{}; foreach ($Item in $VirtualNetwork) { foreach ($Key in $Item.subnets.id) { Add-ARIIndexItem $VnetIndex $Key $Item } } $SkuIndex = @{}; foreach ($Location in $VMExtraDetails.properties) { foreach ($Sku in $Location.SKUs) { Add-ARIIndexItem $SkuIndex ([string]$Location.Location + '|' + $Sku.Name) $Sku } } # Disks keep their position so "last match wins" loops see them in the original order $DiskIndex = @{}; $DiskPos = 0; foreach ($Item in $disk) { Add-ARIIndexItem $DiskIndex $Item.id ([PSCustomObject]@{ Pos = $DiskPos; Disk = $Item }); $DiskPos++ } if($vm) { $tmp = foreach ($1 in $vm) { $ResUCount = 1 $sub1 = Get-ARIIndexItem $SubIndex ([string]$1.subscriptionId) | Where-Object { $_.id -eq $1.subscriptionId } $data = $1.PROPERTIES $timecreated = $data.timeCreated $timecreated = [datetime]$timecreated $timecreated = $timecreated.ToString("yyyy-MM-dd HH:mm") $dataSize = '' $OSDisk = '' $OSDiskSize = '' $VMDisks = foreach ($Key in @($data.storageProfile.osDisk.managedDisk.id) + @($data.storageProfile.dataDisks.managedDisk.id)) { Get-ARIIndexItem $DiskIndex $Key } $SeenDisk = @{} $VMDisks = foreach ($Candidate in ($VMDisks | Sort-Object Pos)) { if (!$SeenDisk.ContainsKey($Candidate.Pos)) { $SeenDisk[$Candidate.Pos] = $true; $Candidate.Disk } } $StorAcc = '' $VMPPG = '' $OSName = if(![string]::IsNullOrEmpty($data.extended.instanceView.osname)){$data.extended.instanceView.osname}else{$data.storageprofile.imagereference.offer} $OSVersion = if(![string]::IsNullOrEmpty($data.extended.instanceView.osversion)){$data.extended.instanceView.osversion}else{$data.storageprofile.imagereference.sku} # Extra VM Details $VMExtraDetail = Get-ARIIndexItem $SkuIndex ([string]$1.location + '|' + $data.hardwareProfile.vmSize) | Where-Object {$_.Name -eq $data.hardwareProfile.vmSize} foreach ($Capability in $VMExtraDetail.Capabilities) { if ($Capability.Name -eq 'vCPUs') {$vCPUs = $Capability.Value} if ($Capability.Name -eq 'vCPUsPerCore') {$vCPUsPerCore = $Capability.Value} if ($Capability.Name -eq 'MemoryGB') {$RAM = $Capability.Value} if ($Capability.Name -eq 'MaxDataDiskCount') {$MaxDataDiskCount = $Capability.Value} if ($Capability.Name -eq 'UncachedDiskIOPS') {$UncachedDiskIOPS = $Capability.Value} if ($Capability.Name -eq 'UncachedDiskBytesPerSecond') {$UncachedDiskBytesPerSecond = ([math]::Round($Capability.Value / 1024) / 1024)} if ($Capability.Name -eq 'MaxNetworkInterfaces') {$MaxNetworkInterfaces = $Capability.Value} } # Quotas $Size = $VMExtraDetail.Family $Quota = $VMQuotas.properties | Where-Object {$_.SubId -eq $1.subscriptionId} $Quota = $Quota | Where-Object {$_.Location -eq $1.location} $RemainingQuota = (($Quota.Data | Where-Object {$_.Name.Value -eq $Size}).Limit - ($Quota.Data | Where-Object {$_.Name.Value -eq $Size}).CurrentValue) $Retired = Foreach ($Retirement in (Get-ARIIndexItem $RetirementIndex $1.id)) { if ($Retirement.id -eq $1.id) { $Retirement } } if ($Retired) { $RetiredFeature = foreach ($Retire in $Retired) { $RetiredServiceID = $Unsupported | Where-Object {$_.Id -eq $Retire.ServiceID} $tmp0 = [PSCustomObject]@{ 'RetiredFeature' = $RetiredServiceID.RetiringFeature 'RetiredDate' = $RetiredServiceID.RetirementDate } $tmp0 } $RetiredFeature = $RetiredFeature | Where-Object { $_.RetiredFeature } | Sort-Object RetiredFeature -Unique $RetiringFeature = $RetiredFeature.RetiredFeature -join ', ' $RetiringDate = $RetiredFeature.RetiredDate -join ', ' # Advisor names a group of series ("LSv2, G, B - Series"); keep only the one this VM uses $SizeMatch = [regex]::Match([string]$data.hardwareProfile.vmSize, '^(?:Standard|Basic)_([A-Z]+)\d+(?:-\d+)?([a-z]*)(?:_v(\d+))?') if ($SizeMatch.Success) { $SeriesFamily = $SizeMatch.Groups[1].Value $SeriesS = if ($SizeMatch.Groups[2].Value -like '*s*') { 's' } else { '' } $SeriesCandidates = if ($SizeMatch.Groups[3].Value) { ($SeriesFamily + $SeriesS + 'v' + $SizeMatch.Groups[3].Value), ($SeriesFamily + 'v' + $SizeMatch.Groups[3].Value) } else { ($SeriesFamily + $SeriesS), $SeriesFamily } $RetiringFeature = foreach ($Feature in $RetiredFeature.RetiredFeature) { $SeriesHit = if ($Feature -match '^(.+?)\s*-\s*Series$') { $SeriesList = $Matches[1] -split '\s*,\s*'; foreach ($Candidate in $SeriesCandidates) { $SeriesList | Where-Object { $_ -eq $Candidate } | Select-Object -First 1 } } $SeriesHit = $SeriesHit | Select-Object -First 1 if ($SeriesHit) { $SeriesHit + '-series' } else { $Feature } } $RetiringFeature = $RetiringFeature -join ', ' } } else { $RetiringFeature = $null $RetiringDate = $null } # PPGs $VMPPG = Get-ARIIndexItem $PPGIndex $1.id | Where-Object { $_.properties.virtualMachines.id -eq $1.id } #Extensions $ext = @() $AzDiag = '' $Azinsights = '' $Lic = switch ($data.licenseType) { 'Windows_Server' { 'Azure Hybrid Benefit for Windows' } 'Windows_Client' { 'Windows client with multi-tenant hosting' } 'RHEL_BYOS' { 'Azure Hybrid Benefit for Redhat' } 'SLES_BYOS' { 'Azure Hybrid Benefit for SUSE' } default { $data.licenseType } } $Lic = if($Lic){$Lic}else{'None'} $ext = foreach ($vmextension in (Get-ARIIndexItem $ExtIndex $1.name)) { if (($vmextension.id -split "/")[8] -eq $1.name) { $vmextension.properties.Publisher } } if ($null -ne $ext) { $ext = foreach ($ex in $ext) { if ($ex | Where-Object { $_ -eq 'Microsoft.Azure.Performance.Diagnostics' }) { $AzDiag = $true } if ($ex | Where-Object { $_ -eq 'Microsoft.EnterpriseCloud.Monitoring' }) { $Azinsights = $true } $ex + ', ' } $ext = [string]$ext $ext = $ext.Substring(0, $ext.Length - 2) } if(![string]::IsNullOrEmpty($data.osprofile.windowsconfiguration.enableautomaticupdates)) { if($data.osprofile.windowsconfiguration.enableautomaticupdates -eq 'True') { $Autoupdate = $true } else { $Autoupdate = $false } } elseif(![string]::IsNullOrEmpty($data.osprofile.linuxconfiguration.patchsettings.patchmode)) { if($data.osprofile.linuxconfiguration.patchsettings.patchmode -eq 'automaticbyos') { $Autoupdate = $true } else { $Autoupdate = $false } } if (![string]::IsNullOrEmpty($data.availabilitySet)) { $AVSET = $true }else { $AVSET = $false } if ($data.diagnosticsProfile.bootDiagnostics.enabled -eq $true) { $bootdg = $true }else { $bootdg = $false } #Storage if($data.storageProfile.osDisk.vhd.uri) { $OSDisk = 'Custom VHD' $OSDiskSize = $data.storageProfile.osDisk.diskSizeGB } else { foreach ($VMDisk in $VMDisks) { if ($VMDisk.id -eq $data.storageProfile.osDisk.managedDisk.id) { $OSDisk = $VMDisk.sku.name $OSDiskSize = $VMDisk.properties.diskSizeGB } } # Disk resource not in the extract (e.g. other subscription): use the size on the VM if (!$OSDiskSize) { $OSDiskSize = $data.storageProfile.osDisk.diskSizeGB } } if ($data.storageProfile.dataDisks.managedDisk.id) { # Size and SKU come from the disk resource; when it is not in the extract # (e.g. other subscription), fall back to what the VM reports for that disk $DataDiskSizes = foreach ($DataDisk in @($data.storageProfile.dataDisks | Where-Object { $_.managedDisk.id })) { $Found = $VMDisks | Where-Object { $_.id -eq $DataDisk.managedDisk.id } | Select-Object -First 1 if ($Found) { $StorAcc = $Found.sku.name; $Found.properties.diskSizeGB } else { $StorAcc = $DataDisk.managedDisk.storageAccountType; $DataDisk.diskSizeGB } } $dataSize = ($DataDiskSizes | Measure-Object -Sum).Sum if ($data.storageProfile.dataDisks.managedDisk.id.count -ge 2) { $StorAcc = ($data.storageProfile.dataDisks.managedDisk.id.count.ToString() + ' Disks found.') } } else { $StorAcc = 'None' $dataSize = '0' } $Tags = if($InTag -and ![string]::IsNullOrEmpty($1.tags.psobject.properties)){$1.tags.psobject.properties}else{'0'} $VMNICS = if(![string]::IsNullOrEmpty($data.networkProfile.networkInterfaces.id)){$data.networkProfile.networkInterfaces.id}else{'0'} foreach ($2 in $VMNICS) { $vmnic = foreach ($netinterface in (Get-ARIIndexItem $NicIndex $2)) { if ($netinterface.id -eq $2) { $netinterface } } $vmnic = $vmnic | Select-Object -Unique $PIP = if(![string]::IsNullOrEmpty($vmnic.properties.ipConfigurations.properties.publicIPAddress.id)){$vmnic.properties.ipConfigurations.properties.publicIPAddress.id.split('/')[8]}else{''} $VNET = if(![string]::IsNullOrEmpty($vmnic.properties.ipConfigurations.properties.subnet.id)){$vmnic.properties.ipConfigurations.properties.subnet.id.split('/')[8]}else{''} $Subnet = if(![string]::IsNullOrEmpty($vmnic.properties.ipConfigurations.properties.subnet.id)){$vmnic.properties.ipConfigurations.properties.subnet.id.split('/')[10]} else {''} $vmnet = foreach ($VMVnet in $(foreach ($Key in $vmnic.properties.ipConfigurations.properties.subnet.id) { Get-ARIIndexItem $VnetIndex $Key })) { if ($VMVnet.subnets.id -eq $vmnic.properties.ipConfigurations.properties.subnet.id) { $VMVnet } } $vmnetsubnet = $vmnet.properties.subnets | Where-Object {$_.id -eq $vmnic.properties.ipConfigurations.properties.subnet.id} if(![string]::IsNullOrEmpty($vmnic.properties.dnsSettings.dnsServers)) { $DNSServer = $vmnic.properties.dnsSettings.dnsServers } else { $DNSServer = $vmnet.properties.dhcpoptions.dnsservers } if(![string]::IsNullOrEmpty($DNSServer)) { $FinalDNS = if ($DNSServer.count -gt 1) { $DNSServer | ForEach-Object { $_ + ' ,' } }else { $DNSServer } $FinalDNS = [string]$FinalDNS $FinalDNS = if ($FinalDNS -like '* ,*') { $FinalDNS -replace ".$" }else { $FinalDNS } $FinalDNS = ('VNET: ( ' + $FinalDNS + ')') } else { $FinalDNS = 'Default (Azure-provided)' } if(![string]::IsNullOrEmpty($vmnic.properties.networkSecurityGroup.id)) { $vmnsg = $vmnic.properties.networkSecurityGroup.id.split('/')[8] } elseif(![string]::IsNullOrEmpty($vmnetsubnet.properties.networksecuritygroup.id)) { $vmnsg = ('Subnet: ('+$vmnetsubnet.properties.networksecuritygroup.id.split('/')[8]+')') } else { $vmnsg = 'None' } if($vmnic.properties.enableAcceleratedNetworking -eq $true) { $AcceleratedNetwork = $true } else { $AcceleratedNetwork = $false } foreach ($Tag in $Tags) { $obj = @{ 'ID' = $1.id; 'Subscription' = $sub1.Name; 'Resource Group' = $1.RESOURCEGROUP; 'VM Name' = $1.NAME; 'Host Name' = $data.osProfile.computerName; 'Location' = $1.LOCATION; 'Retiring Feature' = $RetiringFeature; 'Retiring Date' = $RetiringDate; 'Availability Zone' = [string]$1.ZONES; 'Zones Available in the Region' = [string]$VMExtraDetail.LocationInfo.ZoneDetails.Name; 'Availability Set' = $AVSET; 'Proximity Placement Group' = if ($VMPPG) { $VMPPG.name } else { '' }; 'Disk Controller Type' = [string]$data.storageProfile.diskControllerType; 'VM Size' = $data.hardwareProfile.vmSize; 'Remaining Quota (vCPUs)' = [string]$RemainingQuota; 'vCPUs' = $vCPUs; 'vCPUs Per Core' = $vCPUsPerCore; 'RAM (GiB)' = $RAM; 'Max Remote Storage Disks' = $MaxDataDiskCount; 'Uncached Disk IOPS Limit' = $UncachedDiskIOPS; 'Uncached Disk Throughput Limit (MB/s)' = $UncachedDiskBytesPerSecond; 'Max Network Interfaces' = $MaxNetworkInterfaces; 'Image Reference' = $data.storageProfile.imageReference.publisher; 'Image Version' = $data.storageProfile.imageReference.exactVersion; 'Capabilities' = [string]$VMExtraDetail.LocationInfo.ZoneDetails.Capabilities.Name; 'Hybrid Benefit' = $Lic; 'Admin Username' = $data.osProfile.adminUsername; 'OS Type' = $data.storageProfile.osDisk.osType; 'OS Name' = $OSName; 'OS Version' = $OSVersion; 'Automatic Update' = $Autoupdate; 'Boot Diagnostics' = $bootdg; 'Performance Agent' = if ($azDiag -ne '') { $true }else { $false }; 'Azure Monitor' = if ($Azinsights -ne '') { $true }else { $false }; 'OS Disk Storage Type' = $OSDisk; 'OS Disk Size (GB)' = $OSDiskSize; 'Data Disk Storage Type' = $StorAcc; 'Data Disk Size (GB)' = $dataSize; 'VM generation' = $data.extended.instanceview.hypervgeneration; 'Power State' = $data.extended.instanceView.powerState.displayStatus; 'NIC Name' = [string]$vmnic.name; 'NIC Type' = [string]$vmnic.properties.nicType; 'DNS Servers' = $FinalDNS; 'Public IP' = $PIP; 'Virtual Network' = $VNET; 'Subnet' = $Subnet; 'NSG' = $vmnsg; 'Accelerated Networking' = $AcceleratedNetwork; 'IP Forwarding' = [string]$vmnic.properties.enableIPForwarding; 'Private IP Address' = [string]$vmnic.properties.ipConfigurations.properties.privateIPAddress; 'Private IP Allocation' = [string]$vmnic.properties.ipConfigurations.properties.privateIPAllocationMethod; 'Creation Time' = $timecreated; 'VM Extensions' = $ext; 'Resource U' = $ResUCount; 'Tag Name' = [string]$Tag.Name; 'Tag Value' = [string]$Tag.Value } if ($ResUCount -eq 1) { $ResUCount = 0 } $obj } } } $tmp } } else { If($SmaResources) { $TableName = ('VMTable_'+(($SmaResources.'Resource U' | Measure-Object -Sum).Sum)) $Style = @() $Style += New-ExcelStyle -HorizontalAlignment Center -AutoSize -NumberFormat '0' -VerticalAlignment Center $Style += New-ExcelStyle -HorizontalAlignment Left -Range AZ:AZ -Width 60 -WrapText $SheetName = 'Virtual Machines' $condtxt = @() #Automatic Updates $condtxt += New-ConditionalText false -Range X:X #Hybrid Benefit $condtxt += New-ConditionalText None -Range AA:AA #Boot Diagnostics $condtxt += New-ConditionalText false -Range AC:AC #Performance Agent $condtxt += New-ConditionalText false -Range AD:AD #Azure Monitor $condtxt += New-ConditionalText false -Range AE:AE #NSG $condtxt += New-ConditionalText None -Range AQ:AQ #Acelerated Network $condtxt += New-ConditionalText false -Range AT:AT #Retirement $condtxt += New-ConditionalText -Range N2:N100 -ConditionalType ContainsText $Exc = New-Object System.Collections.Generic.List[System.Object] $Exc.Add('Subscription') $Exc.Add('Resource Group') $Exc.Add('VM Name') $Exc.Add('Host Name') $Exc.Add('VM Size') $Exc.Add('Remaining Quota (vCPUs)') $Exc.Add('vCPUs') $Exc.Add('vCPUs Per Core') $Exc.Add('RAM (GiB)') $Exc.Add('Max Remote Storage Disks') $Exc.Add('Uncached Disk IOPS Limit') $Exc.Add('Uncached Disk Throughput Limit (MB/s)') $Exc.Add('Max Network Interfaces') $Exc.Add('Retiring Feature') $Exc.Add('Retiring Date') $Exc.Add('Availability Zone') $Exc.Add('Zones Available in the Region') $Exc.Add('Capabilities') $Exc.Add('Location') $Exc.Add('OS Type') $Exc.Add('OS Name') $Exc.Add('OS Version') $Exc.Add('Proximity Placement Group') $Exc.Add('Automatic Update') $Exc.Add('Image Reference') $Exc.Add('Image Version') $Exc.Add('Hybrid Benefit') $Exc.Add('Admin Username') $Exc.Add('Boot Diagnostics') $Exc.Add('Performance Agent') $Exc.Add('Azure Monitor') $Exc.Add('Disk Controller Type') $Exc.Add('OS Disk Storage Type') $Exc.Add('OS Disk Size (GB)') $Exc.Add('Data Disk Storage Type') $Exc.Add('Data Disk Size (GB)') $Exc.Add('VM generation') $Exc.Add('Power State') $Exc.Add('Availability Set') $Exc.Add('Virtual Network') $Exc.Add('Subnet') $Exc.Add('DNS Servers') $Exc.Add('NSG') $Exc.Add('NIC Name') $Exc.Add('NIC Type') $Exc.Add('Accelerated Networking') $Exc.Add('IP Forwarding') $Exc.Add('Private IP Address') $Exc.Add('Private IP Allocation') $Exc.Add('Public IP') $Exc.Add('Creation Time') $Exc.Add('VM Extensions') $Exc.Add('Resource U') if($InTag) { $Exc.Add('Tag Name') $Exc.Add('Tag Value') } $noNumberConversion = @() $noNumberConversion += 'OS Version' $noNumberConversion += 'Image Version' $noNumberConversion += 'Private IP Address' $noNumberConversion += 'DNS Servers' [PSCustomObject]$SmaResources | ForEach-Object { $_ } | Select-Object $Exc | Export-Excel -Path $File -WorksheetName $SheetName -TableName $TableName -TableStyle $tableStyle -MaxAutoSizeRows 100 -ConditionalText $condtxt -Style $Style -NoNumberConversion $noNumberConversion } } |