Skip to content

Instantly share code, notes, and snippets.

@emnavarro02
Created November 4, 2022 15:13
Show Gist options
  • Save emnavarro02/dade1391bdc16cd32869a4632c26419f to your computer and use it in GitHub Desktop.
Save emnavarro02/dade1391bdc16cd32869a4632c26419f to your computer and use it in GitHub Desktop.
Generates a CSV file with all Azure peered VNets
<#
.SYNOPSIS
Retrieves a list of VNet peerings and exports it as CSV.
.DESCRIPTION
Retrieves a list of VNet peerings and exports it as CSV.
Make sure you are logged in the right Azure Tenant/Subscription before running this script.
.PARAMETER ResourceGroupName
The name of the Resource Group
Example: "my-azure-resource-group"
.PARAMETER VNetName
The name of the Azure Virtual Network
Example: "my-azure-vnet"
.PARAMETER CSVPath
Full path to the CSV file.
Example: "C:\my-report.csv"
.PARAMETER Delimiter
The delimiter char of the CSV file (default: ,)
.PARAMETER Encoding
Character encoding option (default: UTF-8)
.NOTES
Version: 0.1.0 (2022-11-04)
Author: Emerson Navarro ([email protected])
Created: November, 2022
#>
[CmdletBinding()]
param(
[parameter(Mandatory = $true)]
[String]
$ResourceGroupName,
[parameter(Mandatory = $true)]
[String]
$VNetName,
[parameter(Mandatory = $false)]
[String]
$CSVPath = "$(Get-Date -UFormat %Y-%m-%d)_PeeredNetworks.csv",
[parameter(Mandatory = $false)]
[String]
$Delimiter = ",",
[parameter(Mandatory = $false)]
[String]
$Encoding = "UTF8"
)
$peer = Get-AzVirtualNetworkPeering -ResourceGroupName $ResourceGroupName -VirtualNetworkName $VNetName
$peer | Select-Object @{
name = "Name";
expression = { $_.Name }
},
@{
name = "PeeredRemoteAddressSpace";
expression = { $_."PeeredRemoteAddressSpace"."AddressPrefixes" }
},
@{
name = "RemoteVirtualNetworkName";
expression = { $_."RemoteVirtualNetwork"."Id".Split("/")[8] }
},
@{
name = "RemoteVirtualSubscriptionId";
expression = { $_."RemoteVirtualNetwork"."Id".Split("/")[2] }
},
@{
name = "PeeringState";
expression = { $_.PeeringState }
} | Export-Csv $CSVPath -Delimiter $Delimiter -Encoding $Encoding -NoTypeInformation -NoClobber -Force
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment