Skip to content

Instantly share code, notes, and snippets.

@jschlackman
Last active September 26, 2023 16:57
Show Gist options
  • Save jschlackman/731e253172a9a085823493840d48801a to your computer and use it in GitHub Desktop.
Save jschlackman/731e253172a9a085823493840d48801a to your computer and use it in GitHub Desktop.
# Name: Get-AzurePrivateIpAddresses.ps1
# Author: James Schlackman
# Last Modified: Sep 26 2023
#
# Lists all defined network interfaces with their private IPs and VM allocations.
#Requires -Modules Az.Accounts, Az.Compute, Az.Network
Param(
[Parameter()] [String] $AzTenant,
[Parameter()] [String] $AzSubscription,
[Parameter()] [String] $OutputPath = "$((Get-Date).ToString("yyMMdd")) Azure IP Addresses.csv"
)
# Connect to Azure
Connect-AzAccount
# Set context to specified tenant or subscription if provided
If ([bool]$AzTenant) {
Set-AzContext -Subscription $AzTenant
} ElseIf ([bool]$AzSubscription) {
Set-AzContext -Subscription $AzSubscription
}
# Get all network interfaces
$NICs = Get-AzNetworkInterface
Write-Host "`nInterfaces found: $($NICs.Count)"
# Get all VMs
$VMs = Get-AzVM
# Define output properties
$OutputProperties = `
'Name',
'ResourceGroupName',
'Location',
'Tags',
@{Name='IP'; Expression={($_.IpConfigurations | Select -ExpandProperty PrivanteIpAddress) -join ', '}},
@{Name='AllocationMethod'; Expression={($_.IpConfigurations | Select -ExpandProperty PrivateIpAllocationMethod) -join ', '}},
@{Name='AssignedTo'; Expression={($VMs | Where-Object -Property Id -eq $_.VirtualMachine.Id).Name}},
'Id'
# Format output
$AuditOutput = $NICs | Select -Property $OutputProperties
# Display output
$AuditOutput | Out-GridView
Write-Host 'See grid export for details.'
# Optionally export output to file
Write-Host "`nExport details to CSV? " -ForegroundColor Cyan -NoNewline
If ((Read-Host '[y/N]').ToUpper() -eq 'Y') {
Write-Host 'Exporting to ' -NoNewline
Write-Host $OutputPath -ForegroundColor Green
$AuditOutput | Export-Csv -NoTypeInformation -Path ($OutputPath) -Encoding UTF8
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment