Migrate a netadapter configuration in an atomic command
You know the situation: You need to perform multiple commands that will disconnect you in the middle. Functions to the rescue!
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.
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:
Obviously you should write a quite reliable piece of code there - otherwise: