I’m heading up a work project that involves eliminating certain legacy subnets, with all the devices migrating to a replacement subnet. The network team handles the network changes (running/routing two subnets on the same segments) and DHCP scopes; I follow behind and update the servers that use static IPs. Ususally, these are Windows servers - some are virtualized while others run on Dell hardware. In theory, these Dell servers should all have iDRAC configured and working, but sometimes iDRAC doesn’t work, which leaves me in a high-risk scenario - do I update the IP address on a server when I have no way to revert it? (For example, I update the server IP, losing my connection to the console. If there’s a typo in the IP/subnet/gateway, or there’s a networking problem, there’s no way to reconnect to the server).
I decided that’s too much risk, so I developed a script to assign the new IP settings, wait 30 seconds, then re-apply the old IP settings. This way, I could validate that ping resolved the new IP address, giving me a modicum of reassurance that the network side was correct and nothing was mis-typed.
I wrote this to use WMI for the changes, not Powershell cmdlets, as I often encounter Windows 2008R2 servers in this project. That OS only has Powershell 2.0, which doesn’t include modern cmdlets only available in Powershell 5+, like Set-NetIPAddress.
# Get the network adapter configuration
$adapter = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.IPEnabled}
# Save current settings
$originalIP = $adapter.IPAddress[0]
$originalSubnet = $adapter.IPSubnet[0]
$originalGateway = $adapter.DefaultIPGateway[0]
Write-Host "Original IP: $originalIP"
Write-Host "Original Subnet: $originalSubnet"
Write-Host "Original Gateway: $originalGateway"
# Set new IP and gateway
$newIP = "192.168.17.251"
$newSubnet = "255.255.255.0"
$newGateway = "192.168.17.1"
$setIP = $adapter.EnableStatic($newIP, $newSubnet)
$setGW = $adapter.SetGateways($newGateway)
Write-Host "New IP settings applied. Waiting 30 seconds..."
Start-Sleep -Seconds 30
# Revert to original IP and gateway
$revertIP = $adapter.EnableStatic($originalIP, $originalSubnet)
$revertGW = $adapter.SetGateways($originalGateway)
This worked great… until I ran into a server that had a broken WMI instance. Suddenly, the WMI calls (that I used to bypass the lack of modern cmdlets) became a roadblock. I decided to modify the script so that the netsh CLI commands are used:
# Hardcode the current settings, since I can't query them with WMI.
#(I suppose that I could parse results from ipconfig, but that's too complex for this quick and dirty script)
$originalIP = "164.145.70.7"
$originalSubnet = "255.255.255.0"
$originalGateway = "164.145.70.1"
$interface = "Fwd team"
Write-Host "Original IP: $originalIP"
Write-Host "Original Subnet: $originalSubnet"
Write-Host "Original Gateway: $originalGateway"
# Set new IP and gateway
$newIP = "10.49.212.7"
$newSubnet = "255.255.255.0"
$newGateway = "10.49.212.1"
netsh interface ip set address name="$interface" static $newIP $newsubnet $newgateway 1
Write-Host "New IP settings applied. Waiting 30s..."
start-sleep -seconds 30
netsh interface ip set address name="$interface" static $originalIP $originalSubnet $originalGateway 1
write-host "finished"