changing ip address remotely causes disconnection

when i try to run this and change the ip address of a machine remotely, it disconnects before I can change the ip gateway. how can i get this working without getting disconnected?

$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'" -ComputerName sql2012
$wmi.EnableStatic("10.245.10.20", "255.255.0.0")
$wmi.SetGateways("10.245.0.1", 1)
$wmi.SetDNSServerSearchOrder("10.245.1.14")

June 4th, 2013 12:12am

That is correct.  As soon as you change the address the machine will become disconnected.  This is by design.  Setting gateway will also cause a disconnect to occur.

This can only be done locally. 

Free Windows Admin Tool Kit Click here and download it now
June 4th, 2013 12:28am

You're connecting to a remote machine at a specific IP address. You then change that remote machine's IP address.

Exercise: What happens to your connection to a remote machine if the remote machine's IP address changes? (It is not a trick question.)

Bill

June 4th, 2013 12:53am

I had to change 200 server IP addresses as part of a migration, it sucks, but this is how I did it.

$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'" -ComputerName sql2012

$CurrentNICIndex = $wmi.CurrentNICIndex # this might not work, i just added for the example

$wmi.EnableStatic($ip, $subnet) | Out-Null
[int]$Loops = 0
do {
    Start-Sleep -Seconds 10
    $Loops++
}
until ((Test-Connection -Cn $ip -BufferSize 16 -Count 1 -ea 0 -quiet) -or $Loops -ge 30) #Use the New IP to connect, gives it a bit to update

if ($Loops -lt 30) {
$NetworkAdapter = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -computername "$IP" -filter "Index = $CurrentNICIndex"  #Use the New IP to connect, remove the CurrentNicIndex if its causeing problems
$NetworkAdapter.SetGateways($Gateway) | Out-Null
$NetworkAdapter.SetDNSServerSearchOrder($DNSArr) | Out-Null
$NetworkAdapter.SetDynamicDNSRegistration("TRUE") | Out-Null

also if your changing subnets this wont work, unless the new network is contactable of course.

Free Windows Admin Tool Kit Click here and download it now
June 4th, 2013 4:15am

It can never work if the gateway has to be changed as is the case here.  If you are just changing the IP then it can be made to work.  IPs and gateways are paired.

As you posted - it is a subnet issue.

June 4th, 2013 4:18am

If you can get something to work locally, you might be able to (ab)use the task scheduler. Just a thought.
Free Windows Admin Tool Kit Click here and download it now
June 4th, 2013 5:16am

Setup WinRM, Invoke through PowerShell...Script will be sent via WinRM to server, execute, server will go offline for a few seconds, but then come back online. We setup our servers this way:

On the server your changing the IP on, set WinRM trusted hosts via command line or GPO:

winrm s winrm/config/client '@{TrustedHosts="IP or Hostname or comma,delim,list"}'

Don't forget to add firewall rule for Windows Remote Management. We lock this down by setting IP restrictions to our Servers that are sending the WinRM commands.

Enable Remoting:

Enable-PSRemoting -Force

Send the command to the server to set the info you want:

$IPInfo = New-Object PSObject -Property @{
    ComputerName = "sql2012"
    Interface = "InterfaceName"
    IP = "10.245.10.20"
    Netmask = "255.255.0.0"
    Gateway = "10.245.0.1"
    DNS1 = "10.245.1.14"
    DNS2 = $null
}

Invoke-Command -ComputerName $($IPInfo.ComputerName) -ArgumentList $IPInfo -ScriptBlock {
    param($IPInfo)
    netsh interface ipv4 set address name="$($IPInfo.Interface)" source=static addr=$($IPInfo.IP) mask=$($IPInfo.NetMask) gateway=$($IPInfo.Gateway)
    netsh interface ipv4 set dns name="$($IPInfo.Interface)" source=static addr=($IPInfo.DNS1) primary
    if ($IPInfo.DNS2) {
        netsh interface ipv4 add dns name="$($IPInfo.Interface)" addr=$($IPInfo.DNS2)
    }
}

#Wait until server comes back online
Do {
    $CheckWinRM = Invoke-Command -ComputerName $($IPInfo.ComputerName) -ErrorAction SilentlyContinue -ScriptBlock { ipconfig }
    Start-Sleep 3
}
Until ($CheckWinRM)
    

June 4th, 2013 8:44pm

Wes - that works but it requior4es a fully implemented WinRM in a multi-segmented network.  Most users asking the above question would not have implemented WinRM.

This is another excellent reason to implement network-wide WinRM.

Free Windows Admin Tool Kit Click here and download it now
June 4th, 2013 9:11pm

Wes - that works but it requior4es a fully implemented WinRM in a multi-segmented network.  Most users asking the above question would not have implemented WinRM.

This is another excellent reason to implement network-wide

June 4th, 2013 10:23pm

Yeah, I ended up running it twice, once to change the IP from one subnet, then a second time on another machine in the destination network, where the gateway didn't matter.
Free Windows Admin Tool Kit Click here and download it now
June 5th, 2013 4:43am

how do I implement network wide winrm?
June 5th, 2013 11:50pm

how do I implement network wide winrm?

Install and configure WMF 3.0 using Group Policy.
Free Windows Admin Tool Kit Click here and download it now
June 5th, 2013 11:51pm

Another thing to consider is that you should be using DHCP then none of this would be necessary.

Also Windows Pre-Windows 7 can only run MF 2.0 which sill work with WMF 3.0.

June 5th, 2013 11:56pm

network wide winrm needs wmf 3.0 installed on all machines? severs and pcs?
Free Windows Admin Tool Kit Click here and download it now
July 9th, 2013 2:34pm

network wide winrm needs wmf 3.0 installed on all machines? severs and pcs?

Is that a question, complaint, observation or other?

This thread is kind of dead now.

The simple answer is yes.  Network-wide means everywhere if you want to use it everywhere.

July 9th, 2013 2:44pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics