diff --git a/PowerShell/Validate-NetSettings.ps1 b/PowerShell/Validate-NetSettings.ps1 new file mode 100644 index 0000000..0e8c5f3 --- /dev/null +++ b/PowerShell/Validate-NetSettings.ps1 @@ -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 -gateway -subnetMask -DNSServers +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 +}