Export or Backup Azure Network Security Groups into CSV using PowerShell

There could be many use cases where you may want to export Network Security Groups into CSV. You might have question, how to export or backup Azure Network Security Groups into CSV. Here is the PowerShell script that you can use to export Azure Network Security Groups into CSV using PowerShell script. This script will export Network Security Group along with rules of all Active subscriptions into a CSV.


$outputfinal=@()
foreach ( $Subscription in $(Get-AzSubscription| Where-Object {$_.State -ne "Disabled"}) )
{
Select-AzSubscription -SubscriptionId $Subscription.SubscriptionId
$nsgs=Get-AzNetworkSecurityGroup
foreach ($nsg in $nsgs)
{
$securityrules=$nsg.SecurityRules
foreach ($securityrule in $securityrules)
{
$outputtemp = "" | SELECT  NSGName,NSGLocation,RGName,Direction,Priority,RuleName,DestinationPort,Protocol,SourceAddress,SourcePort,DestinationAddress,Action,Description
$outputtemp.NSGName=$nsg.name
$outputtemp.NSGLocation=$nsg.location
$outputtemp.RGName=$nsg.ResourceGroupName
$outputtemp.Direction=$securityrule.direction
$outputtemp.Priority=$securityrule.Priority
$outputtemp.RuleName=$securityrule.Name
$outputtemp.DestinationPort=$securityrule.DestinationPortRange -join ", "
$outputtemp.Protocol=$securityrule.Protocol -join ", "
$outputtemp.SourceAddress=$securityrule.SourceAddressPrefix -join ", "
$outputtemp.SourcePort=$securityrule.SourcePortRange -join ", "
$outputtemp.DestinationAddress=$securityrule.DestinationAddressPrefix -join ", "
$outputtemp.Action=$securityrule.Access
$outputtemp.Description=$securityrule.Description
$outputfinal += $outputtemp
}
}
}
$outputfinal | Export-Csv ./clouddrive/.cloudconsole/NSGs_"$((Get-Date).ToString("yyyyMMdd_HHmmss")).csv" -NoTypeInformation



 If you want to export the CSV to Azure Storage file share, you can remove the last line from above script and add following lines.


$outputfinal | Export-Csv "$Env:temp/NSGs" -NoTypeInformation

$resourceGroupName="myresourcegroup"  
$storageAccName="mystorageaccount" 
$fileShareName="myfileshare"  
$fileName="$Env:temp/NSGs" 
$folderPath="/" 

Function UploadFiles  
{   Set-AzContext -SubscriptionId 2gdj2342-gl39-390r-h208-84957dg897g8
    $ctx=(Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName).Context  
    Set-AzStorageFileContent -ShareName $fileShareName -context $ctx -Source $fileName -Path $folderPath/"NSGs_$((Get-Date).ToString("yyyyMMdd_HHmmss")).csv" 
}  
UploadFiles   



If you are looking for a script that can export Azure Route Tables the similar way, you can check out the script at Export or Backup Azure Route Table into CSV using PowerShell

 

  

Comments

Post a Comment

Popular posts from this blog

Anyconnect SSL-Client VPN with Self-signed Certificate on Cisco ASA

Filtering Routes in BGP using Route-maps and Prefix-list

Open Shortest Path First (OSPF)

IKEv2 IPsec Site-to-Site VPN configuration on Cisco ASA 8.4(x)

IPsec VPN as a Backup for Point-to-Point Link using IP SLA

Border Gateway Protocol (BGP)

Cisco ASA Active/Active Failover Configuration

Bypassing Proxy Server in Google Chrome

Cisco ASA Active/Standby Failover Configuration