Migrating IP settings from one NIC to another using Powershell


Here’s an example scenario where the following script may be particularly useful:

GridStore array where each node currently uses one 1 Gbps NIC. After adding 10 Gbps NIC to each node, we’d like to migrate the IP settings from the 1 Gbps NIC to the 10 Gbps NIC on each node. GridStore utilizes commodity rack mount servers and hardware and a robust software driver to present scalable, high performance, fully redundant vLUNs. More detailed posts on GridStore will follow.

This diagram shows network connectivity before adding the 10 Gbps NICs:

Before

 

After adding the 10 Gbps NICs:

After

Steps:

  1. You will need administrative credentials to the nodes from GridStore technical support
  2. From Server1, using GridControl snapin, stop all vLUNs:
    Stop-vLUNs
  3. RDP to each node.
    logon
  4. Currently nodes run Windows 7 Embedded and the RDP session will bring up a command prompt.
  5. Run Control to show Control Panel, double-click Network and Sharing Center, click Change Adapter Settings to view/confirm that you have 2 connected NICs:
    NICs
  6. Start Powershell ISE:
    start-ps-ise
  7. Copy/paste the following script and run it on each node:

    GS-2

# Script to move network configuration from one NIC to another on a GridStore node
# Sam Boutros
# 6/16/2014
# Works with Powershell 2.0
#
Set-Location “c:\support”
$Loc = Get-Location
$Date = Get-Date -format yyyymmdd_hhmmsstt
$logfile = $Loc.path + “\Move-GSNIC_” + $env:COMPUTERNAME + “_” + $Date + “.txt”
function log($string) {
Write-Host $string; $temp = “: ” + $string
$string = Get-Date -format “yyyy.mm.dd hh:mm:ss tt”; $string += $temp
$string | out-file -FilePath $logfile -append
}
#
log “Switching NIC configuration on $env:COMPUTERNAME”
$ConnectedNICs = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter ‘IPEnabled=”true”‘
If ($ConnectedNICs.Count -lt 2) {log “Error: Less than 2 connected NICs:”; log $ConnectedNICs}
else {
if ($ConnectedNICs.Count -gt 2) {log “Error: More than 2 connected NICs:”; log $ConnectedNICs}
else { # 2 connected NICs
Stop-Service GridStoreManagementService
# Storing NICs details in variables for later use
$NIC0Index = $ConnectedNICs[0].index; log “NIC 0 Index: $NIC0Index”
$NIC0Desc = $ConnectedNICs[0].description; log “NIC 0 Description: $NIC0Desc”
$NIC0IPv4 = $ConnectedNICs[0].IPAddress[0]; log “NIC 0 IPv4: $NIC0IPv4”
$NIC0Mask = $ConnectedNICs[0].IPSubnet[0]; log “NIC 0 Subnet Mask: $NIC0Mask”
$Nic0 = Get-WmiObject win32_networkadapter -filter “DeviceId = $NIC0Index”
$NIC0ConnID = $Nic0.NetConnectionID; log “NIC 0 NetConnectionID: $NIC0ConnID”
#
$NIC1Index = $ConnectedNICs[1].index; log “NIC 1 Index: $NIC1Index”
$NIC1Desc = $ConnectedNICs[1].description; log “NIC 1 Description: $NIC1Desc”
$NIC1IPv4 = $ConnectedNICs[1].IPAddress[0]; log “NIC 1 IPv4: $NIC1IPv4”
$NIC1Mask = $ConnectedNICs[1].IPSubnet[0]; log “NIC 1 Subnet Mask: $NIC1Mask”
$Nic1 = Get-WmiObject win32_networkadapter -filter “DeviceId = $NIC1Index”
$NIC1ConnID = $Nic1.NetConnectionID; log “NIC 1 NetConnectionID: $NIC1ConnID”
# Identify Target NIC and Source NIC
if ($ConnectedNICs[0].IPAddress[0] -match “169.254”) {$TargetNIC = 0} else {$TargetNIC = 1}
$SourceNIC = 1 – $TargetNIC; log “Source NIC: NIC $SourceNIC”
$SourceIP = $ConnectedNICs[$SourceNIC].IPAddress[0]
$SourceMask = $ConnectedNICs[$SourceNIC].IPSubnet[0]
log “Source IP: $SourceIP”
# Setting IP address for Source NIC to DHCP
log “Changing IP address of source NIC to DHCP”
if ($ConnectedNICs[$SourceNIC].EnableDHCP().ReturnValue -eq 0) {
log “==> IP setting change was successful”} else {log “==> IP setting change failed”}
$ConnectedNICs[$SourceNIC].SetDNSServerSearchOrder()
# Need to disable and re-enable the Source NIC for the settings to take effect (!?)
$Nic0.Disable(); Start-Sleep -s 2
$Nic0.Enable(); Start-Sleep -s 2
# Setting IP address for Target NIC to the same values previousely held by the Source NIC
$TargetIP = $ConnectedNICs[$TargetNIC].IPAddress[0]
log “Target IP: $TargetIP”
log “Changing IP address of Target NIC to $SourceIP with subnet mask $SourceMask”
if ($ConnectedNICs[$TargetNIC].EnableStatic($SourceIP,$SourceMask).ReturnValue -eq 0) {
log “==> IP address change was successful”} else {log “==> IP address change failed”}
Remove-Item -Path “HKLM:\SOFTWARE\Wow6432Node\Gridstore\NetworkAdapter”
Start-Sleep -s 2
Start-Service GridStoreManagementService
}
}
Invoke-Expression “$env:windir\system32\Notepad.exe $logfile”

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s