Skip to content

Commit c75bf20

Browse files
committed
Parameter validation improved
1 parent 05e99cd commit c75bf20

File tree

1 file changed

+43
-15
lines changed

1 file changed

+43
-15
lines changed

Scripts/IPv4NetworkScan.ps1

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,16 @@ Param(
8383
Position=1,
8484
Mandatory=$true,
8585
Helpmessage='Subnetmask like 255.255.255.0')]
86-
[ValidatePattern("^(254|252|248|240|224|192|128).0.0.0$|^255.(254|252|248|240|224|192|128|0).0.0$|^255.255.(254|252|248|240|224|192|128|0).0$|^255.255.255.(254|252|248|240|224|192|128|0)$")]
86+
[ValidateScript({
87+
if($_ -match "^(254|252|248|240|224|192|128).0.0.0$|^255.(254|252|248|240|224|192|128|0).0.0$|^255.255.(254|252|248|240|224|192|128|0).0$|^255.255.255.(254|252|248|240|224|192|128|0)$")
88+
{
89+
return $true
90+
}
91+
else
92+
{
93+
throw "Enter a valid subnetmask (like 255.255.255.0)!"
94+
}
95+
})]
8796
[String]$Mask,
8897

8998
[Parameter(
@@ -140,7 +149,7 @@ Begin{
140149
Write-Verbose -Message "Create backup of the IEEE Standards Registration Authority list..."
141150

142151
# Backup file, before download a new version
143-
if([System.IO.File]::Exists($CSV_MACVendorList_Path))
152+
if(Test-Path -Path $CSV_MACVendorList_Path -PathType Leaf)
144153
{
145154
Rename-Item -Path $CSV_MACVendorList_Path -NewName $CSV_MACVendorList_BackupPath
146155
}
@@ -153,7 +162,7 @@ Begin{
153162
Write-Verbose -Message "Remove backup of the IEEE Standards Registration Authority list..."
154163

155164
# Remove Backup, if no error
156-
if([System.IO.File]::Exists($CSV_MACVendorList_BackupPath))
165+
if(Test-Path -Path $CSV_MACVendorList_BackupPath -PathType Leaf)
157166
{
158167
Remove-Item -Path $CSV_MACVendorList_BackupPath
159168
}
@@ -162,12 +171,12 @@ Begin{
162171
Write-Verbose -Message "Cleanup downloaded file and restore backup..."
163172

164173
# On error: cleanup downloaded file and restore backup
165-
if([System.IO.File]::Exists($CSV_MACVendorList_Path))
174+
if(Test-Path -Path $CSV_MACVendorList_Path -PathType Leaf)
166175
{
167176
Remove-Item -Path $CSV_MACVendorList_Path -Force
168177
}
169178

170-
if([System.IO.File]::Exists($CSV_MACVendorList_BackupPath))
179+
if(Test-Path -Path $CSV_MACVendorList_BackupPath -PathType Leaf)
171180
{
172181
Rename-Item -Path $CSV_MACVendorList_BackupPath -NewName $CSV_MACVendorList_Path
173182
}
@@ -194,7 +203,16 @@ Begin{
194203
Position=0,
195204
Mandatory=$true,
196205
HelpMessage='Subnetmask like 255.255.255.0')]
197-
[ValidatePattern("^(254|252|248|240|224|192|128).0.0.0$|^255.(254|252|248|240|224|192|128|0).0.0$|^255.255.(254|252|248|240|224|192|128|0).0$|^255.255.255.(255|254|252|248|240|224|192|128|0)$")]
206+
[ValidateScript({
207+
if($_ -match "^(254|252|248|240|224|192|128).0.0.0$|^255.(254|252|248|240|224|192|128|0).0.0$|^255.255.(254|252|248|240|224|192|128|0).0$|^255.255.255.(255|254|252|248|240|224|192|128|0)$")
208+
{
209+
return $true
210+
}
211+
else
212+
{
213+
throw "Enter a valid subnetmask (like 255.255.255.0)!"
214+
}
215+
})]
198216
[String]$Mask
199217
)
200218

@@ -238,15 +256,14 @@ Begin{
238256
# Helper function to convert an IPv4-Address to Int64 and vise versa
239257
function Convert-IPv4Address
240258
{
241-
[CmdletBinding(DefaultParameterSetName='String')]
259+
[CmdletBinding(DefaultParameterSetName='IPv4Address')]
242260
param(
243261
[Parameter(
244-
ParameterSetName='String',
262+
ParameterSetName='IPv4Address',
245263
Position=0,
246264
Mandatory=$true,
247265
HelpMessage='IPv4-Address as string like "192.168.1.1"')]
248-
[ValidatePattern("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$")]
249-
[String]$IPv4Address,
266+
[IPaddress]$IPv4Address,
250267

251268
[Parameter(
252269
ParameterSetName='Int64',
@@ -264,8 +281,8 @@ Begin{
264281
switch($PSCmdlet.ParameterSetName)
265282
{
266283
# Convert IPv4-Address as string into Int64
267-
"String" {
268-
$Octets = $IPv4Address.split(".")
284+
"IPv4Address" {
285+
$Octets = $IPv4Address.ToString().Split(".")
269286
$Int64 = [long]([long]$Octets[0]*16777216 + [long]$Octets[1]*65536 + [long]$Octets[2]*256 + [long]$Octets[3])
270287
}
271288

@@ -310,7 +327,16 @@ Begin{
310327
Position=1,
311328
Mandatory=$true,
312329
Helpmessage='Subnetmask like 255.255.255.0')]
313-
[ValidatePattern("^(254|252|248|240|224|192|128).0.0.0$|^255.(254|252|248|240|224|192|128|0).0.0$|^255.255.(254|252|248|240|224|192|128|0).0$|^255.255.255.(254|252|248|240|224|192|128|0)$")]
330+
[ValidateScript({
331+
if($_ -match "^(254|252|248|240|224|192|128).0.0.0$|^255.(254|252|248|240|224|192|128|0).0.0$|^255.255.(254|252|248|240|224|192|128|0).0$|^255.255.255.(254|252|248|240|224|192|128|0)$")
332+
{
333+
return $true
334+
}
335+
else
336+
{
337+
throw "Enter a valid subnetmask (like 255.255.255.0)!"
338+
}
339+
})]
314340
[String]$Mask
315341
)
316342

@@ -420,12 +446,14 @@ Begin{
420446
}
421447

422448
Process{
449+
$CSV_MACVendorList_Available = Test-Path -Path $CSV_MACVendorList_Path -PathType Leaf
450+
423451
# Check for vendor list update
424452
if($UpdateList)
425453
{
426454
UpdateListFromIEEE
427455
}
428-
elseif(($EnableMACResolving) -and (-Not([System.IO.File]::Exists($CSV_MACVendorList_Path))))
456+
elseif(($EnableMACResolving) -and ($CSV_MACVendorList_Available -eq $false))
429457
{
430458
Write-Warning -Message "No CSV-File to assign vendor with MAC-Address found! Use the parameter ""-UpdateList"" to download the latest version from IEEE.org. This warning does not affect the scanning procedure."
431459
}
@@ -479,7 +507,7 @@ Process{
479507
}
480508

481509
# Check if it is possible to assign vendor to MAC --> import CSV-File
482-
if(($EnableMACResolving) -and ([System.IO.File]::Exists($CSV_MACVendorList_Path)))
510+
if($EnableMACResolving -and $CSV_MACVendorList_Available)
483511
{
484512
$AssignVendorToMAC = $true
485513

0 commit comments

Comments
 (0)