The development and testing environments are in different CIDR blocks and IP address changes are often required. Manual modification is very troublesome. Later, a small vbs script was found to achieve automatic failover.
Suppose the IP segments are 192.168.30. * And 192.168.41. * respectively .*
Add two new files, turn30.vbs and turn41.vbs, respectively. The content is as follows:
Strcomputer = " . "
Set Obw.miservice = GetObject ( " Winmgmts :\\ " & Strcomputer & " \ Root \ cimv2 " )
Set Colnetadapters = Ob1_miservice. execquery _
( " Select * From win32_networkadapterconfiguration where ipenabled = true " )
Stripaddress = Array ( " 192.168.30.99 " )
Strsubnetmask = Array ( " 255.255.255.0 " )
Strgateway = Array ( " 192.168.30.254 " ) Strdns = Array ( " 192.168.30.254 " )
Strgatewaymetric = Array ( 1 ) Strdns2 = Array ( 1 )
For Each Objnetadapter in colnetadapters
Errenable = Objnetadapter. enablestatic (stripaddress, strsubnetmask)
Errgateways = Objnetadapter. setgateways (strgateway, strgatewaymetric) Errdns = objnetadapter. setdnsserversearchorder (strdns, strdns2)
If Errenable = 0 Then
Wscript. Echo " The IP address has been changed " & Stripaddress ( 0 )
Else
Wscript. Echo " The IP address cocould not be changed. "
End If
Next
Strcomputer = " . "
Set Obw.miservice = GetObject ( " Winmgmts :\\ " & Strcomputer & " \ Root \ cimv2 " )
Set Colnetadapters = Ob1_miservice. execquery _
( " Select * From win32_networkadapterconfiguration where ipenabled = true " )
Stripaddress = Array ( " 192.168.41.52 " )
Strsubnetmask = Array ( " 255.255.255.0 " )
Strgateway = Array ( " 192.168.41.1 " )
Strdns = Array ( " 192.168.41.254 " )
Strgatewaymetric = Array ( 1 )
Strdns2 = Array ( 1 )
For Each Objnetadapter in colnetadapters
Errenable = Objnetadapter. enablestatic (stripaddress, strsubnetmask)
Errgateways = Objnetadapter. setgateways (strgateway, strgatewaymetric) Errdns = objnetadapter. setdnsserversearchorder (strdns, strdns2)
If Errenable = 0 Then
Wscript. Echo " The IP address has been changed " & Stripaddress ( 0 )
Else
Wscript. Echo " The IP address cocould not be changed. "
End If
Next
Usage: Double-click turn30.vbs or turn41.vbs.
Reference website:
Http://www.microsoft.com/china/technet/community/scriptcenter/network/scrnet01.mspx