If your organization has computer labs which require static IP addresses, here is a very easy method to deploy static IP’s to each lab. Most labs will have some sort of numbering scheme as part of the computer name. We can take advantage of this in order to transform the computer number into an IP address.
In my example, the computer number is at the end of the computer name. I am going to use PowerShell to calculate the new IP address for each computer, apply the new network configuration, register the new settings, and output a log file. Here is the script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# get computer number from its name $nameNum = $env:COMPUTERNAME -replace "CO-ROOM-200-","" # calculate the new IP [int]$nameNum += 100 # set DNS servers $dnsServers = "192.168.10.1","192.168.10.2" # apply new network settings $networkConfig = Get-WmiObject Win32_NetworkAdapterConfiguration -filter "ipenabled = 'true'" $networkConfig.EnableStatic("192.168.2.$nameNum", "255.255.255.0") $networkConfig.SetGateways("192.168.2.254", 1) $networkConfig.SetDNSServerSearchOrder($dnsServers) # update DNS ipconfig /registerdns # log file get-date | Out-File C:\Windows\COMPANY-staticIP.txt ipconfig /all | Out-File C:\Windows\COMPANY-staticIP.txt -Append |
In my example, I have a lab of computers named CO-ROOM-200-00, CO-ROOM-200-01, etc. The static IP’s I want to assign these computers are 192.168.2.100, 192.168.2.101, etc. So in my script I am taking that last 00 and 01 and adding 100 to it. Simple as that.
After that, we set the DNS servers. If you have more than one DNS server, you need to set them as an array, because the “SetDNSServerSearchOrder” property cannot accept two separate strings otherwise.
Finally, we update DNS and output a log file. This script can now be deployed using any method, such as SCCM, in order to set static IP addresses in bulk to an entire room of computers, if the computers are sequentially numbered.
And here is the script to undo these changes and to re-enable DHCP: How to enable DHCP using PowerShell.
Pingback: How to enable DHCP using PowerShell | BorisKagan.net