Get DFW Rules 2
Get DFW Rules 2
param(
[Parameter(Mandatory = $true)]
[String]$Server,
[Parameter(Mandatory = $true)]
[String]$Username,
[String]$credPair
)
begin{
$SecuredPassword = Read-Host "Password" -AsSecureString
$Password =
[System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropSe
rvices.Marshal]::SecureStringToBSTR($SecuredPassword))
$credPair = "$($Username):$($Password)"
if(-not $global:defaultNsxServers.isConnected){
try{
Write-Host "Establishing connection to NSX-T manager " +
$Server + " for user " + $Username
Connect-NsxtServer -Server $Server -User $Username -
Password $Password
}
catch{
throw "Unable to establish the connection to NSX-T manager
" + $Server + " for user " + $Username
}
}
else{
try{
Connect-NsxtServer -Menu -ErrorAction Stop
}
catch{
throw "Could not connect to an NSX-T Manager, please try again"
}
}
class FW_Rule{
[String]$Section_id
[String]$Section_name
[String]$Id
[String]$Name
[String]$Description
[String]$Sources_name_type
[String]$Destinations_name_type
[String]$Services_name_type
[String]$Applied_tos_name_type
[String]$Action
[String]$Disabled
[String]$Direction
[String]$Ip_protocol
}
}
process{
$FW_Sections_API = Get-NsxtService -Name
'com.vmware.nsx.firewall.sections'
$FW_Sections = $FW_Sections_API.list().results
$FW_Section_Rules_API = Get-NsxtService -Name
'com.vmware.nsx.firewall.sections.rules'
$FW_Rules = @()
foreach($Section in $FW_Sections){
$FW_Section_Rules =
$FW_Section_Rules_API.list($Section.id).results
foreach($Rule in $FW_Section_Rules){
$FW_New_Rule = [FW_Rule]::new()
$FW_New_Rule.Section_id = $Section.id
$FW_New_Rule.Section_name = $Section.display_name
$FW_New_Rule.Id = $Rule.id
$FW_New_Rule.Name = $Rule.display_name
$FW_New_Rule.Description = $Rule.description
$FW_New_Rule.Action = $Rule.action
$FW_New_Rule.Disabled = $Rule.disabled
$FW_New_Rule.Direction = $Rule.direction
$FW_New_Rule.Ip_protocol = $Rule.ip_protocol
if($FW_New_Rule.Description.Contains('default.Policy_Default_Infra-')){
continue
}
if($hasSources){
foreach($Source in $Rule.sources){
$FW_New_Rule.Sources_name_type +=
$Source.target_display_name + " ; " + $Source.target_type + "`n"
}
if($Rule.Sources_name_type.Length -gt 0){
$FW_New_Rule.Sources_name_type =
$FW_New_Rule.Sources_name_type.TrimEnd()
}
}
if($hasDestinations){
foreach($Destination in $Rule.destinations){
$FW_New_Rule.Destinations_name_type +=
$Destination.target_display_name + " ; " + $Destination.target_type + "`n"
}
if($Rule.Destinations_name_type.Length -gt 0){
$FW_New_Rule.Destinations_name_type =
$FW_New_Rule.Destinations_name_type.TrimEnd()
}
}
if($hasServices){
foreach($Service in $Rule.services){
$FW_New_Rule.Services_name_type +=
$Service.target_display_name + " ; " + $Service.target_type + "`n"
}
if($Rule.Services_name_type.Length -gt 0){
$FW_New_Rule.Services_name_type =
$FW_New_Rule.Services_name_type.TrimEnd()
}
}
if($hasApplied_tos){
foreach($Applied_to in $Rule.applied_tos){
$FW_New_Rule.Applied_tos_name_type +=
$Applied_to.target_display_name + " ; " + $Applied_to.target_type + "`n"
}
if($Rule.Applied_tos_name_type.Length -gt 0){
$FW_New_Rule.Applied_tos_name_type =
$FW_New_Rule.Applied_tos_name_type.TrimEnd()
}
}
$FW_Rules += $FW_New_Rule
}
}
$scriptDirectory = $PSScriptRoot
$filePath = Join-Path -Path $scriptDirectory -ChildPath "output.csv"
$FW_Rules | Export-Csv -Path $filePath
}
}