Score:-1

Change IP address from 192.x.y.z to 10.u.v.w for whole network with Powershell

br flag

Out Network moves from 192.x.y.z to 10.u.v.w and I don't want to readdress 250 Computers by hand. My idea is to use a powershell script (test for a single pc):

$oldIp='192.168.100.1'
$newIp='10.11.12.13'
$newGw='10.11.12.254'
$newLen='24'

$adminCred = Get-Credential -UserName ourdomain\myAdminAccount -Message 'Enter Password'

ForEach ($Adapter in (Get-WmiObject Win32_NetworkAdapter -Filter "NetEnabled='True'"  -ComputerName $oldIp )){
    
    $wmiQuery = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "Index = '$($Adapter.Index)'" -ComputerName $oldIp
      
    if( $wmiQuery.IPAddress -contains $oldIp ){        

        $pcName=[system.net.dns]::gethostentry($lavIp) |  Select-Object -ExpandProperty HostName

        $cimSess=New-CimSession -SkipTestConnection -Credential $adminCred -ComputerName $pcName

        New-NetIPAddress -CimSession $cimSess  -InterfaceIndex $Adapter.Index -IPAddress $newIp -PrefixLength $newLen -DefaultGateway $newGw
        
    }else{
        ## do not touch this nic
    }
}

If all NICs got it's new IP, remove the old IP with Remove-NetIPAddress.

Currently, the $cimSess does not work as expected, but my main question is: Am I on the right track, or is there a better/faster/easier/... way to do this? Or am I completely wrong and missed something?

Zac67 avatar
ru flag
DHCP is a thing.
Score:6
us flag

You should use DHCP to provision the IP addresses for your network. All computers then have DHCP enabled, and they get the correct IP address from the DHCP server.

vidarlo avatar
ar flag
This is the *answer*. Use DHCP. If you for some reason think you require static addresses, use DHCP reservations.
Thomas P avatar
br flag
Yes, I totally agree with you. But it is not my decision and DHCP is currently not an option. We are forced to do this by higher authorities.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.