Categories

Changing a network address

One task that comes up from time to time is changing the network address on a system. The subnet may be the same but we need to change the last octet of the address

We can find an address using Test-Connection

PS (1) > test-connection server02

Source        Destination     IPV4Address      IPV6Address  Bytes    Time(ms)
------        -----------     -----------      -----------  -----    --------
RSLAPTOP01    server02        192.168.1.3      {}           32       150

Creating the new address involves changing the last octet of the address

function new-ipaddress {            
param (            
 [string]$computer,            
             
 [ValidateRange(0,255)]            
 [int]$newoctet            
)            
 $ip = Test-Connection -ComputerName $computer -Count 1            
 $octets = $ip.IPV4Address.IPAddressToString -split "\."            
 $octets[3] = $newoctet.ToString()            
             
 $newaddress = $octets -join "."            
 $newaddress            
}


 



The new address can be used in Win32_NetworkAdapterConfiguration to change the address – but if you change the address you are connecting to you have to wait a long time for your PowerShell session to timeout.



Want to know how to speed up the process – read Chapter 13 of PowerShell and WMI (www.manning.com/siddaway2)

Leave a Reply