Last active
August 1, 2022 13:20
-
-
Save ljtill/6b493abd17ca9851e937b56a1afbdbeb to your computer and use it in GitHub Desktop.
Provides the ability to report on virtual machine information
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function Get-AzVMInfo { | |
| [CmdletBinding()] | |
| param() | |
| begin { | |
| $filePath = (Get-Location).Path + "\Azure_Virtual_Machine_Details_" + (Get-Date -UFormat "%m-%d-%Y") + ".csv" | |
| if (Test-Path -Path $filePath) { Remove-Item -Path $filePath -Force } | |
| $subscriptions = Get-AzSubscription -TenantId $TenantId | |
| } | |
| process { | |
| foreach ($subscription in $subscriptions) { | |
| Select-AzSubscription -SubscriptionId $subscription.SubscriptionId | Out-Null | |
| $report = @() | |
| $vms = Get-AzVM | |
| $publicIps = Get-AzPublicIpAddress | |
| $nics = Get-AzNetworkInterface | Where-Object { $null -NE $_.VirtualMachine } | |
| foreach ($nic in $nics) { | |
| $vm = $vms | Where-Object -Property Id -eq $nic.VirtualMachine.id | |
| $info = [PSCustomObject]@{ | |
| VmName = "" | |
| ResourceGroupName = "" | |
| Region = "" | |
| VmSize = "" | |
| VirtualNetwork = "" | |
| Subnet = "" | |
| PrivateIpAddress = "" | |
| OsType = "" | |
| OsSku = "" | |
| PublicIPAddress = "" | |
| NicName = "" | |
| } | |
| for ($i = 0; $i -le 30; $i++) { | |
| $info | Add-Member "DataDisk$i" "" | |
| $info | Add-Member "DataDiskSize$i" "" | |
| $info | Add-Member "StorageAccountType$i" "" | |
| } | |
| $info.VMName = $vm.Name | |
| $info.ResourceGroupName = $vm.ResourceGroupName | |
| $info.Region = $vm.Location | |
| $info.VmSize = $vm.HardwareProfile.VmSize | |
| $info.VirtualNetwork = $nic.IpConfigurations.subnet.Id.Split("/")[-3] | |
| $info.Subnet = $nic.IpConfigurations.subnet.Id.Split("/")[-1] | |
| $info.PrivateIpAddress = $nic.IpConfigurations.PrivateIpAddress | |
| $info.OsType = $vm.StorageProfile.OsDisk.OsType | |
| $info.OsSku = $vm.StorageProfile.ImageReference.Sku | |
| foreach ($publicIp in $publicIps) { | |
| if ($nic.IpConfigurations.id -eq $publicIp.ipconfiguration.Id) { | |
| $info.PublicIPAddress = $publicIp.ipaddress | |
| } | |
| } | |
| $info.NicName = $nic.Name | |
| for ($i = 0 ; $i -le 30; $i++) { | |
| $info."DataDisk$i" = $vm.StorageProfile.DataDisks[$i] ? $vm.StorageProfile.DataDisks[$i].Name : $null | |
| $info."DataDiskSize$i" = $vm.StorageProfile.DataDisks[$i] ? $vm.StorageProfile.DataDisks[$i].diskSizeGB : $null | |
| $info."StorageAccountType$i" = $vm.StorageProfile.DataDisks[$i] ? $vm.StorageProfile.DataDisks[$i].managedDisk.StorageAccountType : $null | |
| } | |
| $report += $info | |
| } | |
| $report | Export-Csv -Path $filePath -Append | |
| } | |
| } | |
| } | |
| Get-AzVMInfo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment