-
Notifications
You must be signed in to change notification settings - Fork 0
/
List-NSG-Rules.ps1
78 lines (54 loc) · 2.6 KB
/
List-NSG-Rules.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#------------------------------------------------------------------------------
#
#
# THIS CODE AND ANY ASSOCIATED INFORMATION ARE PROVIDED “AS IS” WITHOUT
# WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
# LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS
# FOR A PARTICULAR PURPOSE. THE ENTIRE RISK OF USE, INABILITY TO USE, OR
# RESULTS FROM THE USE OF THIS CODE REMAINS WITH THE USER.
#
#------------------------------------------------------------------------------
# To list the Rules of NSG
$rg = Read-Host "RG Name"
$nsgname = Read-Host "NSG Name"
$nsg2 = Get-AzNetworkSecurityGroup -ResourceGroupName $rg -Name $nsgname
$rule2 = Get-AzNetworkSecurityRuleConfig -NetworkSecurityGroup $nsg2
$rule2 | FT -Property Name, Protocol ,SourcePortRange, DestinationPortRange, SourceAddressPrefix, DestinationAddressPrefix, Access , Priority , Direction
$FN = $nsg2.Name
$path = $env:TEMP+$FN+'.txt'
$path2 = $env:TEMP+$FN+'.csv'
Write-Host "Output files '$FN'.csv'' and '$FN'.txt'' will be created in $($env:TEMP)" -ForegroundColor Green
"Name + Protocol + SourcePortRange + SourceAddressPrefix + DestinationPortRange + DestinationAddressPrefix + Access + Priority + Direction" | Out-File $path
#$rule2 | FT -Property Name, Protocol ,SourcePortRange, DestinationPortRange
$Access = @()
$Access += $($rule2.Access)
$Priority = @()
$Priority += $($rule2.Priority)
$Direction = @()
$Direction += $($rule2.Direction)
$Protocol = @()
$Name = @()
$Protocol += $($rule2.Protocol)
$Name += $($rule2.Name)
$SourcePortRange = @()
$SourcePortRange += $($rule2.SourcePortRange)
$SourceAddressPrefix =@()
$SourceAddressPrefix += $($rule2.SourceAddressPrefix)
$DestinationPortRange = @()
$DestinationPortRange += $($rule2.DestinationPortRange)
$DestinationAddressPrefix =@()
$DestinationAddressPrefix += $($rule2.DestinationAddressPrefix)
for ($i=0; $i -lt $rule2.Count; $i++){
$ProtocolF = $Protocol[$i]
$Namef = $Name[$i]
$SourcePortRangef = $SourcePortRange[$i]
$SourceAddressPrefixf = $SourceAddressPrefix[$i]
$DestinationPortRangef = $DestinationPortRange[$i]
$DestinationAddressPrefixf = $DestinationAddressPrefix[$i]
$Accessf = $Access[$i]
$Priorityf = $Priority[$i]
$Directionf= $Direction[$i]
"$namef + $Protocolf + $SourcePortRangef + $SourceAddressPrefixf + $DestinationPortRangef + $DestinationAddressPrefixf + $Accessf + $Priorityf + $Directionf" | Out-File $path -Append
}
Import-Csv -Path $path -Delimiter "+" | Export-Csv -Path $Path2 -NoTypeInformation -Append
Import-Csv $path2 | FT