Azure – Exporter les Firewall Policies en CSV

Explication

Petit bout de code pour vous permettre d’exporter de manière rapide vos règles firewall provenant d’un Azure Firewall dans un CSV compréhensible.

Il y a seulement deux variables à renseigner, la Firewall Policy Name et le Ressource Group dans lequel est le Firewall Azure.

Code

# Variables (Modify)
$fwpol = "Firewall_Policy_Name"
$fwrg = "Firewall_Ressource_Group_Name"

# Strategy from Azure Firewall
$azFwPol = Get-AzFirewallPolicy -Name $fwpol -ResourceGroupName $fwrg

# IDs from Rule Collections Groups
$rcgsIds = $azFwPol.RuleCollectionGroups

# RCG Name from RCG IDs
$rcgsNames = foreach ($rcgId in $rcgsIds) {
$rcgId.Id.Substring($rcgId.Id.LastIndexOf("/") + 1)
}

# Loop RCG
$returnObj = @()
foreach ($fwrcg in $rcgsNames) {
$fp = Get-AzFirewallPolicy -Name $fwpol -ResourceGroupName $fwrg
$rcg = Get-AzFirewallPolicyRuleCollectionGroup -Name $fwrcg -AzureFirewallPolicy $fp

foreach ($rulecol in $rcg.Properties.RuleCollection) {
foreach ($rule in $rulecol.rules) {
$properties = [ordered]@{
"Network Rule Collection" = if ($rulecol.Name) { $rulecol.Name } else { "N/A" }
"Rule Name" = if ($rule.Name) { $rule.Name } else { "N/A" }
"Source VM" = if ($rule.SourceFQDNs) { $rule.SourceFQDNs -join ", " } else { "N/A" }
"Source IP" = if ($rule.SourceAddresses) { $rule.SourceAddresses -join ", " } else { "N/A" }
"Source IP Group" = if ($rule.SourceIPGroups) { $rule.SourceIPGroups.Split("/")[-1] -join ", " } else { "N/A" }
"Protocol" = if ($rule.Protocols -eq "TCP" -or $rule.Protocols -eq "UDP" -or $rule.Protocols -eq "Any" -or $rule.Protocols -eq "ICMP" -or $rule.Protocols -eq "TCP, UDP") { $rule.Protocols -join ", " } else { "N/A" }
"Port" = if ($rule.DestinationPorts) { $rule.DestinationPorts -join ", " } else { "N/A" }
"Destination VM" = if ($rule.DestinationFQDNs) { $rule.DestinationFQDNs -join ", " } else { "N/A" }
"Destination IP" = if ($rule.DestinationAddresses) { $rule.DestinationAddresses -join ", " } else { "N/A" }
"Destination IP Group" = if ($rule.DestinationIPGroups) { $rule.DestinationIPGroups.Split("/")[-1] -join ", " } else { "N/A" }
}
$obj = New-Object psobject -Property $properties
$returnObj += $obj
}
}

# Export to CSV
$returnObj | Export-Csv .$fwpol.csv -NoTypeInformation
}

...BofBienTrès BienTop ! (Soit le premier à voter)
Loading...

Mathieu

Je suis actuellement ingénieur spécialisé dans le design d'environnements cloud virtualisés. Adepte des technologies de VMware, Nutanix, Citrix et Microsoft je propose à travers ce blog diverses astuces de troubleshooting.

S’abonner
Notification pour
guest

0 Commentaires
Commentaires en ligne
Afficher tous les commentaires