0% found this document useful (0 votes)
19 views3 pages

Get DFW Rules 2

Uploaded by

itquiz27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views3 pages

Get DFW Rules 2

Uploaded by

itquiz27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

Function 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

$hasSources = $Rule.PSObject.Properties | Where-Object


{ $_.Name -eq "sources" }
$hasDestinations = $Rule.PSObject.Properties | Where-Object
{ $_.Name -eq "destinations" }
$hasServices = $Rule.PSObject.Properties | Where-Object
{ $_.Name -eq "services" }
$hasApplied_tos = $Rule.PSObject.Properties | Where-Object
{ $_.Name -eq "applied_tos" }

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
}
}

You might also like