Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
257c209
commit 765ba1f
Showing
1 changed file
with
34 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#Test network settings for common errors. Ensures IP Address, Gateway, and DNS Servers are all valid IP addresses and that the Subnet Mask is a valid mask. Returns the Subnet Mask in CIDR notation if everything is successful. | ||
#Ensures tha the Gateway address is on the same subnet as the provided IP Address. | ||
#Usage: Validate-NetSettings -ipAddress <IP> -gateway <Gateway> -subnetMask <subnet mask in dotted notation> -DNSServers <Array of DNS Server IPs> | ||
param ( | ||
[Parameter(Mandatory=$true)] | ||
[string]$ipaddress, | ||
|
||
[Parameter(Mandatory=$true)] | ||
[string]$gateway, | ||
|
||
[Parameter(Mandatory=$true)] | ||
[string]$subnetMask, | ||
|
||
[Parameter(Mandatory=$true)] | ||
[string[]]$DNSServers | ||
) | ||
|
||
try { | ||
$ipSubnet = ([ipaddress](([ipaddress]$ipaddress).address -band ([ipaddress]$subnetMask).address)).ipaddresstostring | ||
$gatewaySubnet = ([ipaddress](([ipaddress]$Gateway).address -band ([ipaddress]$subnetMask).address)).ipaddresstostring | ||
if ($ipSubnet -ne $gatewaySubnet){ | ||
throw "IP Address $($ipAddress) with Subnet Mask $($subnetMask) cannot access Gateway $($gateway)" | ||
} | ||
$subnetMaskBinary = ($SubnetMask -split "\." | % {[convert]::tostring($_,2).padleft(8,"0")}) -join "" | ||
if ($subnetMaskBinary -notmatch "(^1+)0+$" -or $subnetMaskBinary.length -ne 32){throw "Invalid subnet mask: $(subnetmask)"} | ||
$subnetCIDR = $matches[1].length | ||
$DNSServers | % { | ||
[ipaddress]$_ | out-null | ||
} | ||
return $subnetCIDR | ||
} | ||
catch { | ||
return $_.Exception.Message | ||
} |