-
Notifications
You must be signed in to change notification settings - Fork 0
/
EXAMPLES.ps1
263 lines (180 loc) · 11.5 KB
/
EXAMPLES.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#******************************************************************************#
# IPv4 #
#******************************************************************************#
############################# Convert-IPv4Address ##############################
# Convert IP address between different formats
# - Quad dot (without mask) eg. "192.168.1.2"
# - Integer (uint32) eg. 3232235778
# - Binary (32 long string) eg. "11000000101010000000000100000010"
Convert-IPv4Address -IP 192.168.1.2 -Binary # Returns 11000000101010000000000100000010
Convert-IPv4Address -IP 11000000101010000000000100000010 # Returns 192.168.1.2
3232235778 | Convert-IPv4Address -QuadDot # Returns 192.168.1.2
Convert-IPv4Address 192.168.1.2 -Integer # Returns 3232235778
############################### Convert-IPv4Mask ###############################
# Convert IP subnet mask between different formats
# - Quad dot eg. "255.255.128.0"
# - Mask length (0-32) eg. "17"
# - Mask length with slash eg. "/17"
# - Integer (uint32) eg. "4294934528"
# - Binary (32 long string) eg. "11111111111111111000000000000000"
Convert-IPv4Mask -Mask 255.255.128.0 # Returns /17
Convert-IPv4Mask -Mask /17 -Length # Returns 17
Convert-IPv4Mask -Mask /17 # Returns 255.255.128.0
Convert-IPv4Mask -Mask 17 # Returns 255.255.128.0
Convert-IPv4Mask -Mask 17 -Binary # Returns 11111111111111111000000000000000
############################### Get-IPv4Address ################################
# Get subnet address in CIDR format for an IP address
Get-IPv4Address -IP 127.0.0.1/8 -Subnet # Returns 127.0.0.0/8
# Get subnet address with mask in quad dot format
Get-IPv4Address -IP 127.0.0.1/8 -Subnet -WithMask # Returns 127.0.0.0 255.0.0.0
# Get broadcast address with mask in quad dot format
Get-IPv4Address -IP 127.0.0.1/8 -Broadcast -WithMask # Returns 127.255.255.255 255.0.0.0
# Get broadcast address without mask
Get-IPv4Address -IP 127.0.0.1/8 -Broadcast -IPOnly # Returns 127.255.255.255
# Get all available addresses in same net as 10.100.200.201/30 is in
Get-IPv4Address -IP 10.100.200.201 -Mask /30 -All -WithMask # Returns array...:
# 10.100.200.201 255.255.255.252
# 10.100.200.202 255.255.255.252
# Get different info for 192.168.0.150/25
Get-IPv4Address -IP 192.168.0.150/255.255.255.128 -Info # Returns object...:
# IP : 192.168.0.150
# Subnet : 192.168.0.128
# FirstIP : 192.168.0.129
# LastIP : 192.168.0.254
# Broadcast : 192.168.0.255
# Integer : @{IP=3232235670; Subnet=3232235648; FirstIP=3232235...
# Binary : @{IP=11000000101010000000000010010110; Subnet=11000...
# MaskQuadDot : 255.255.255.128
# MaskLength : 25
################################# Get-IPv4Mask #################################
# Get subnet mask in different formats from IP address (this funtion is just a wrapper for Get-IPv4Address)
Get-IPv4Mask 9.8.7.6/22 # Returns 255.255.252.0
Get-IPv4Mask 9.8.7.6/22 -LengthWithSlash # Returns /22
Get-IPv4Mask 9.8.7.6/255.255.252.0 -Length # Returns 22
################################ Get-IPv4Subnet ################################
# Get subnet in different formats from IP address (this funtion is just a wrapper for Get-IPv4Address)
Get-IPv4Subnet 127.0.0.1/8 # Returns 127.0.0.0/8
Get-IPv4Subnet -IP 10.20.30.40/28 -WithMask # Returns 10.20.30.32 255.255.255.240
Get-IPv4Subnet -IP '10.20.30.40 255.255.255.240' -IPOnly # Returns 10.20.30.32
############################### Test-IPv4Address ###############################
# Test if input is a valid IPv4 address (without subnet mask)
Test-IPv4Address -IP 127.0.0.1 # Returns True
Test-IPv4Address -IP 127.0.0.256 # Returns False
Test-IPv4Address -IP 127.0.0.1/32 # Returns False
# Test if input is a valid IPv4 address - subnet mask is allowed, but not required
Test-IPv4Address -AllowMask -IP 127.0.0.1/32 # Returns True
Test-IPv4Address -AllowMask "127.0.0.1 255.255.255.255" # Returns True
Test-IPv4Address -AllowMask -IP "127.0.0.1" # Returns True
# Test if input is a valid IPv4 address - subnet mask is required
Test-IPv4Address -RequireMask -IP 127.0.0.1/32 # Returns True
Test-IPv4Address -RequireMask -IP 127.0.0.1 # Returns False
# Test if input is a valid subnet mask (in quad dot format)
Test-IPv4Address -Mask -IP 255.255.0.0 # Returns True
Test-IPv4Address -Mask -IP 255.0.255.0 # Returns False
Test-IPv4Address -Mask -IP 32 # Returns False
# Test if input is a valid subnet mask (quad dot) or mask length
Test-IPv4Address -Mask -AllowLength -IP 255.0.255.0 # Returns False
Test-IPv4Address -Mask -AllowLength -IP 255.255.0.0 # Returns True
Test-IPv4Address -Mask -AllowLength -IP 32 # Returns True
Test-IPv4Address -Mask -AllowLength -IP /32 # Returns True
########################## Test-IPv4AddressInSameNet ###########################
# Test if two IP addresses is in the same subnet
Test-IPv4AddressInSameNet -IP 10.30.50.60 -IP2 10.30.50.61/24 # Returns True
Test-IPv4AddressInSameNet -IP 10.30.50.60/24 -IP2 10.30.50.61/255.255.255.0 # Returns True
Test-IPv4AddressInSameNet -IP 10.30.50.60/24 -IP2 10.30.50.61/29 # Returns False
# Allow mismatch in subnet mask, as long as hosts with the two IP addresses
# would be able to communicate directly (not routed)
Test-IPv4AddressInSameNet 10.30.50.60/24 10.30.50.61/29 -AllowMaskMismatch # Returns True
########################### Test-IPv4AddressInSubnet ###########################
# Test if IP address is in a subnet
Test-IPv4AddressInSubnet -Subnet 10.30.50.0/24 -IP 10.30.50.70 # Returns True
Test-IPv4AddressInSubnet -Subnet 10.30.50.0/24 -IP 10.30.50.70/24 # Returns True
Test-IPv4AddressInSubnet -Subnet 10.30.50.0/24 -IP 10.30.50.70/29 # Returns False
# Ignore mask on IP
Test-IPv4AddressInSubnet -Subnet 10.30.50.0/24 -IP 10.30.50.70/29 -AllowMaskMismatch # Returns True
Test-IPv4AddressInSubnet -Subnet 10.30.50.0/24 -IP 10.30.50.70/23 -AllowMaskMismatch # Returns True
Test-IPv4AddressInSubnet -Subnet 10.30.50.0/24 -IP 10.30.51.70/23 -AllowMaskMismatch # Returns False
############################### Test-IPv4Subnet ################################
# Test if input is a subnet
Test-IPv4Subnet -Subnet 10.20.30.0/24 # Returns True
Test-IPv4Subnet -Subnet 10.20.30.0/255.255.0.0 # Returns False
#******************************************************************************#
# IPv6 #
#******************************************************************************#
############################# Convert-IPv6Address ##############################
# Compact IPv6 address
Convert-IPv6Address 00ab:00:0:000:00:fff::1 # Returns ab::fff:0:1
Convert-IPv6Address 00ab:00:0:000:00:fff::1/64 # Returns ab::fff:0:1/64
# Get different info about an IPv6 address
Convert-IPv6Address -IP a:b:c::/64 -Info # Returns object...:
# IP : a:b:c::/64
# IPCompact : a:b:c::
# IPExpanded : 000a:000b:000c:0000:0000:0000:0000:0000
# IPIntArray : {10, 11, 12, 0...}
# IPHexArray : {a, b, c, 0...}
# IPHexArrayExpanded : {000a, 000b, 000c, 0000...}
# IPBinary : 0000000000001010 0000000000001011 0000000000001100 0000000000000000 0000000000000000 0000000000000000 0000000000000000 0000000000000000
# Cidr : a:b:c::/64
# CidrExpanded : 000a:000b:000c:0000:0000:0000:0000:0000/64
# Prefix : 64
# PrefixIntArray : {65535, 65535, 65535, 65535...}
# PrefixHexArray : {ffff, ffff, ffff, ffff...}
# PrefixHexArrayExpanded : {ffff, ffff, ffff, ffff...}
# PrefixHexString : ffff:ffff:ffff:ffff:0:0:0:0
# PrefixHexStringExpanded : ffff:ffff:ffff:ffff:0000:0000:0000:0000
# PrefixBinary : 1111111111111111 1111111111111111 1111111111111111 1111111111111111 0000000000000000 0000000000000000 0000000000000000 0000000000000000
############################### Get-IPv6Address ################################
# Get subnet
Get-IPv6Address -IP 007:6:5::77:88/64 -Subnet # Returns 7:6:5::/64
# Get subnet without prefix
Get-IPv6Address -IP 007:6:5::77:88/64 -Subnet -IPOnly # Returns 7:6:5::
# Get IP without prefix
Get-IPv6Address -IP 007:6:5::77:88/64 -IPOnly # Returns 7:6:5::77:88
# Get different info for 7:6:5::77:88/64
Get-IPv6Address -IP 007:6:5::77:88/64 -Info # Returns object...:
# IP : 7:6:5::77:88/64
# Subnet : 7:6:5::/64
# FirstIP : 7:6:5::/64
# SecondIP : 7:6:5::1/64
# PenultimateIP : 7:6:5::ffff:ffff:ffff:fffe/64
# LastIP : 7:6:5::ffff:ffff:ffff:ffff/64
# Prefix : 64
# Objects : @{IP=; Subnet=; FirstIP=; SecondIP=; PenultimateIP=; LastIP=}
################################ Get-IPv6Subnet ################################
# Get subnet in different formats from IP address (this funtion is just a wrapper for Get-IPv6Address)
Get-IPv6Subnet -IP 0007:006:05::077:0088/64 # Returns 7:6:5::/64
Get-IPv6Subnet -IP 0007:006:05::077:0088/64 -IPOnly # Returns 7:6:5::
############################### Test-IPv6Address ###############################
# Test if input is a valid IPv6 address (without prefix)
Test-IPv6Address -IP a:b::c # Returns True
Test-IPv6Address -IP a:b::c/64 # Returns False
Test-IPv6Address -IP a:b::x # Returns False
Test-IPv6Address -IP a:b::c/64 # Returns False
# Test if input is a valid IPv6 address - prefix is allowed, but not required
Test-IPv6Address -AllowPrefix -IP a:b::c/64 # Returns True
Test-IPv6Address -AllowPrefix -IP a:b::c # Returns True
Test-IPv6Address -AllowPrefix -IP a:b::x/64 # Returns False
# Test if input is a valid IPv6 address - prefix is required
Test-IPv6Address -RequirePrefix -IP a:b::c/64 # Returns True
Test-IPv6Address -RequirePrefix -IP a:b::c # Returns False
########################## Test-IPv4AddressInSameNet ###########################
# Test if two IP addresses is in the same subnet
Test-IPv6AddressInSameNet -IP a:2::/31 -IP2 a:3::/31 # Returns True
Test-IPv6AddressInSameNet -IP a:2::/32 -IP2 a:3::/32 # Returns False
Test-IPv6AddressInSameNet -IP a:2::/31 -IP2 a:3::/30 # Returns False
# Allow mismatch in prefix, as long as hosts with the two IP addresses
# would be able to communicate directly (not routed)
Test-IPv6AddressInSameNet -IP a:2::/31 -IP2 a:3::/32 -AllowPrefixMismatch # Returns False
Test-IPv6AddressInSameNet -IP a:2::/31 -IP2 a:3::/30 -AllowPrefixMismatch # Returns True
########################### Test-IPv6AddressInSubnet ###########################
# Test if IP address is in a subnet
Test-IPv6AddressInSubnet -Subnet a:2::/31 -IP a:3::/31 # Returns True
Test-IPv6AddressInSubnet -Subnet a:2::/32 -IP a:3::/32 # Returns False
Test-IPv6AddressInSubnet -Subnet a:2::/31 -IP a:3::/30 # Returns False
# Ignore mask on IP
Test-IPv6AddressInSubnet -Subnet a:2::/32 -IP a:3::/31 -AllowPrefixMismatch # Returns False
Test-IPv6AddressInSubnet -Subnet a:2::/31 -IP a:3::/32 -AllowPrefixMismatch # Returns True
############################### Test-IPv6Subnet ################################
# Test if input is a subnet
Test-IPv6Subnet -Subnet a:0:0:b::/64 # Returns True
Test-IPv6Subnet -Subnet a:0:0:0:b::/64 # Returns False