As a frequent user of virtualisation, particularly with regards to Windows Server, I have come across a problem that frequently troubles many: Hidden or Ghost Network Adapters.
Sometimes, after a VM is deallocated from the host and then reallocated, the Guest OS recognizes the NIC as a new device and installs a new driver for it.
These installations remain after any number of reboots. Even though they are marked as inactive some of them can remain active (probably through some corrupt registry setting) and can cause a bevy of errors including:
- “The IP address x.x.x.x you have entered for this network adapter is already assigned to another adapter.”
- Inability to use Fileshares.
- Problems authenticating with a Domain Controller.
- Failure for 2 VMs to communicate with each other.
This problem is particularly common for machines hosted in the cloud, for example on AWS or Azure.
There is a host of articles out there explaining how you can use the device manager to show these hidden adapters and remove them. However, this is a manual approach that can take a long time. In 9 Azure Machines I had 100 Ghost Network Adapters each, imagine the amount of time and frustration this would require. This approach also doesn’t work for Windows Server 2008 (R2).
PowerShell and devcon.exe solution
This led me to articles explaining how devcon.exe, part of the Windows SDK, could be used to find and uninstall these adapters.
Here is the script I used to automatically remove ghost network adapters using devcon.exe:
The script uses the friendly names and the patterns associated with Hyper-V Network Adapter names, depending on the OS version, to identify Ghost Network Adapters.
This approach still has some caveats though:
- It requires seperately downloading the SDK at least once (It has over 2 GB).
- Using this script requires that you ensure that devcon.exe has been added to the computer. In certain environments where Internet connectivity is not granted this can be tough.
- It is unclear whether devcon.exe from one Windows OS SDK is truly compatible with other OS Versions.
- Due to the licensing terms I cannot share the exe (which only has ~250KB) with you directly ie. provide a link.
In terms of DevOps and provisioning machines quickly and reliably with scheduled tasks that run at boot this is not an ideal solution.
Pure PowerShell solution
I therefore wrote a script that works completely without external applications or scripts. It relies heavily on PInvoke and the Windows Setup API, which, I believe, devcon.exe also uses.
As a bonus, in the implementation of
ForEach-Device you can see how to implement your own function that accepts a lambda type expression like Where-Object or ForEach-Object.
You must log in to post a comment.