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:
# 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'"
# update DNS
# 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.
1 thought on “Programmatically Setting Static IP’s in a Computer Lab”
Pingback: How to enable DHCP using PowerShell | BorisKagan.net