Skip to content

Instantly share code, notes, and snippets.

@Rugby-Ball
Last active May 22, 2024 14:12
Show Gist options
  • Select an option

  • Save Rugby-Ball/b658122bca346a109e6f87ddff80d9d1 to your computer and use it in GitHub Desktop.

Select an option

Save Rugby-Ball/b658122bca346a109e6f87ddff80d9d1 to your computer and use it in GitHub Desktop.
Pull all VM's for an Azure Subscription, includes details. #Public #Inventory #Azure #Azure_VM
# azure-vm-detail-inventory.ps1
<#
Description: Pull all VM's for an Azure Subscription, includes details.
Written: Ed Walsh
PowerShell.Core tested: Tested
Version: 1.3
Create Date : 12/15/2023
Revised Date: 3/22/2024
#>
#Install-Module only needed once to install the Modules.
#Install-Module -name AZ.Accounts, AZ.Compute -Scope CurrentUser
#
Import-Module -name Az.Accounts, Az.Compute
# You don't need the next two lines for every run of script, once a day should be enough.
# Connect-AzAccount
# Login-AzAccount -Subscription "Your Subscription Name"
$stopWatch = [System.Diagnostics.Stopwatch]::StartNew()
$stopwatch.Start()
Write-Host "Script started on $(Get-Date -f "MM-dd-yyyy hh:mm tt") This script can take 10+ minutes to run." -ForegroundColor Yellow -BackgroundColor Red
# Import-Module -name az
#Setup File Name attributes
$timestamp = get-date -format yyyyMMddHHmmss
$subfolder = if (($PSVersionTable.PSEdition) -eq "Core") { if ( $True -eq $iswindows ) { "\Documents\" } Else { "" } } Else {"\Documents\"}
$mydocuments = $home + $subfolder
$fileName = "azure-vm-detail-inventory_"+ [string]$timestamp + ".csv"
$filePath = Join-Path $mydocuments $fileName
# $azNetInter = Get-AzNetworkInterface
$vms = Get-AzVM -status | select-object ResourceGroupName,VmId, Name, @{n="HostName";e={$_.osProfile.computerName} },TimeCreated,location,
@{n="Admin_User_Name";e={$_.OSProfile.AdminUsername} },
@{n="EnableAutoUpdates";e={$_.OSProfile.WindowsConfiguration.EnableAutomaticUpdates} },
@{n="Patch_Mode";e={$_.OSProfile.WindowsConfiguration.PatchSettings.PatchMode} },@{n="Enable_Hot_Patching";e={$_.OSProfile.WindowsConfiguration.PatchSettings.EnableHotpatching} },
@{n="OS_Type";e={$_.storageProfile.osDisk.osType} },
OsName,OsVersion,@{n="OS_SKU";e={$_.StorageProfile.ImageReference.Sku} },
@{n="OS_Disk_Size_GB";e={$_.StorageProfile.OSDisk.DiskSizeGB} },
@{n="OS_Disk_Type";e={$_.StorageProfile.OSDisk.ManagedDisk.StorageAccountType} },
#@{n="OS_Disk_Encrypted";e={$_.StorageProfile.OSDisk.ManagedDisk.OS_Disk_EncyrptionSet} },
@{n="Data_Disk_Count";e={(($_.StorageProfile.DataDisks.DiskSizeGB) | Measure-Object).Count } },
@{n="Data_Disk_Cumalitive_Size_GB";e={(($_.StorageProfile.DataDisks.DiskSizeGB) | Measure-Object -sum ).sum } },
# @{n="OS_Disk_Encrypted";e={(Get-AzVmDiskEncryptionStatus -ResourceGroupName $_.ResourceGroupName -VMName $_.Name).OsVolumeEncrypted} },
# @{n="Data_Disk_Encrypted";e={(Get-AzVmDiskEncryptionStatus -ResourceGroupName $_.ResourceGroupName -VMName $_.Name).DataVolumesEncrypted} },
@{n="VM_Size";e={$_.HardwareProfile.vmsize} },
@{n="NIC";e={$_.NetworkProfile.NetworkInterfaces[0].Id -replace '.*\/'} },
@{n="Primary_Internal_IP";e={(Get-AzNetworkInterfaceIpConfig -NetworkInterface (Get-AzNetworkInterface -name ($_.NetworkProfile.NetworkInterfaces.Id -replace '.*\/') )| Where-Object Primary -eq $True ).PrivateIpAddress} },
@{n="External_IP";e={(Get-AzPublicIpAddress -ResourceGroupName $_.ResourceGroupName -Name ((Get-AzNetworkInterface -ResourceGroupName $_.ResourceGroupName -Name ($_.NetworkProfile.NetworkInterfaces[0].Id.Split('/') | select -Last 1)).IpConfigurations.PublicIpAddress.Id.Split('/') | select -Last 1)).IpAddress} },
# @{n="Primary_Internal_IP";e={(Get-AzNetworkInterfaceIpConfig -NetworkInterface (($_.NetworkProfile.NetworkInterfaces.Id -replace '.*\/') | $azNetInter | Where-Object $_.Name -eq $a -and $_.Primary -eq $True | Select-Object $_.Name )).PrivateIpAddress} },
PowerState
$stopwatch.Stop()
$time = $stopwatch.Elapsed
Write-Host "Script finished on $(Get-Date -f "MM-dd-yyyy hh:mm tt"), Elapsed time to run script (HH:MM:SS.MS): $Time" -ForegroundColor Red -BackgroundColor Yellow
$vms| Out-GridView
$vms | Export-Csv -NoTypeInformation -Path $filepath
Write-Output "Exported to: $filePath"
Write-Output "\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\|||||||////////////////////////////////////"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment