Migrate a netadapter configuration in an atomic command

Recently I've been playing around on some lab environment with a newly installed operating system - Windows Server 2022 in this case. The test node had two network interfaces - onboard two Intel I350 1G network interfaces + one dual port Mellanox ConnectX-4.

The management of the OS should be done on a NIC team (built on top of a VMSwitch with SRIOV enabled). Unfortunately i didn't yet have drivers installed and therefore SRIOV has not been available on the I350 nics. Long story short: I created a ManagentOS VMNetadapter on the VMSwitch built on top of the mellanox adapter  to access the operating system and be able to load drivers and install them. At the end i had two VMSwitch: One for VMs, one for management.

But - a simple issue has been bothering: For my access in the first place i've created the management netadapter on the VM vmswitch and i wanted to move it - without using BMC access, as it's nasty to type in long powershell commands over and over again.

Problem: How to migrate the management interface easily

You can obviously create a set of powershell commands and run them. The problem is: If you need to enter them subsequently and you are cutting your connection in the middle, you won't be able to execute upcoming commands that will set up the connection again.

Solution: Create a function and run it

The idea for the solution is quite simple: Create a function definition for the commands and run the function afterwards - the execution of this funtion will survive your disconnect and proceed with the configuration.

function Move-Management {
    Get-NetAdapter *mgmt* | Get-NetIPAddress -PrefixOrigin Manual | Remove-NetIPAddress -Confirm:$false
    Get-VMNetworkAdapter -ManagementOS -Name *mgmt* | Remove-VMNetworkAdapter -Confirm:$false
    Start-Sleep 3
    Add-VMNetworkAdapter -ManagementOS -SwitchName MGMT -Name MGMT
    Start-Sleep 3
    Get-NetAdapter *MGMT* | New-NetIPAddress -PrefixLength 26 -DefaultGateway 10.10.250.1 -IPAddress 10.10.250.21
    Get-NetAdapter *MGMT* | Set-DnsClientServerAddress -ServerAddresses 10.10.250.2
}
define a function for the configuration

This function contains several commands:

  • Remove current ip configuration
  • Remove current VM netadapter from the temp VMSwitch
  • Create a new VM netadapter on the desired VMSwitch
  • Assign IP configuration on the created adapter

Having pasted this block before, you can now issue the reconfiguration as an "atomic" command:

Move-Management
start reconfiguration

Obviously you should write a quite reliable piece of code there - otherwise: