Use PowerShell to Add Multiple IPs to a Network Adapter
Fri, Aug 3, 2012This script uses WMI to assign a range of IPs to a network adapter.
Function chooseAdapter{
$adapters = get-wmiobject win32_networkadapterconfiguration -filter "IPEnabled='True'"
$menu = ""
$adapters | % {$menu += "["+$_.Index+"]"+$_.Description+"`r`n"}
$adapterIndex = Read-Host -Prompt ("Choose an adapter by typing the number next to the name of the adapter you'd like to change" + "`r`n" + $menu)
$chosenAdapter = $adapters | ? {$_.Index -eq $adapterIndex}
if ($chosenAdapter){
return $chosenAdapter
}
else{
chooseAdapter
}
}
Function setStatic($adapter,$ipArray,$subnet){
$subnetArray = @()
ForEach ($ip in $ipArray){
$subnetArray += $subnet
}
$adapter.EnableStatic($ipArray, $subnetArray)
$adapter.SetGateways($adapter.DefaultIPGateway)
$adapter.SetDNSServerSearchOrder($adapter.DNSServerSearchOrder)
$adapter.SetDynamicDNSRegistration(“FALSE”)
}
$adapter = chooseAdapter
$subnet = Read-Host -Prompt "Enter the subnet mask"
$first3 = Read-Host -Prompt "Enter the first three octets, ie: 192.168.1"
$ipMin = Read-Host -Prompt "Enter the minimum of the forth octet"
$ipMax = Read-Host -Prompt "Enter the maximum of the forth octet"
$ips = @()
While ($ipMin -lt $ipMax){
Write-Host Adding $first3 . $ipmin to array
$ips += ($first3+"."+[string]$ipMin)
[int]$ipMin += 1
}
setStatic $adapter $ips $subnet