I have some Azure VM’s that I regulary Stop (deallocate) and Start using Azure Automation. The idea is to cut costs while at night or weekends, as these VM’s are not used then anyway. I recently had a problem with one of these Virtual Machines, I was unable to browse or connect to network resources, could not connect to the domain to get Group Policy updates and more. When looking into it, I found out that I had a lot of hidden Network Adapters in Device Manager. The cause of this is that every time a VM is shut down and deallocated, on next start it will provision a new network adapter. The old network adapter is kept hidden. The result of this over time as I automate shut down and start every day, is that I get a lot of these, as shown below: I found in some forums that the cause of the network browse problem I had with the server could be related to this for Azure VM’s. I don’t know the actual limit, or if it’s a fixed value, but the solution would be to uninstall these hidden network adapters. Although it is easy to right click and uninstall each network adapter, I wanted to create a PowerShell Script to be more efficient. There are no native PowerShell cmdlets or Commands that could help me with this, so after some research I ended with a combination of these two solutions:
- Device Management PowerShell from Technet Gallery: http://blogs.technet.com/b/wincat/archive/2012/09/06/device-management-powershell-cmdlets-sample-an-introduction.aspx
- Windows Device Console (DevCon.exe) http://msdn.microsoft.com/en-us/library/windows/hardware/ff544707(v=vs.85).aspx
I then ended up with the following PowerShell script. The script first get all hidden devices of type Microsoft Hyper-V Network Adapter and their InstanceId. Then for each device uninstall/remove with DevCon.exe. The Script:
Set-Location C:\_Source\DeviceManagement
Import-Module .\Release\DeviceManagement.psd1 -Verbose
# List Hidden Devices
Get-Device -ControlOptions DIGCF_ALLCLASSES | Sort-Object -Property Name | Where-Object {($_.IsPresent -eq $false) -and ($_.Name -like “Microsoft Hyper-V Network Adapter*”) } | ft Name, DriverVersion, DriverProvider, IsPresent, HasProblem, InstanceId -AutoSize
# Get Hidden Hyper-V Net Devices
$hiddenHypVNics = Get-Device -ControlOptions DIGCF_ALLCLASSES | Sort-Object -Property Name | Where-Object {($_.IsPresent -eq $false) -and ($_.Name -like “Microsoft Hyper-V Network Adapter*”) }
# Loop and remove with DevCon.exe
ForEach ($hiddenNic In $hiddenHypVNics) {
$deviceid = “@” + $hiddenNic.InstanceId
.\devcon.exe -r remove $deviceid
}
And after a while all hidden network adapter devices was uninstalled: In the end I booted the VM and after that everything was working on the network again!
this post saved my day, thanks!
Works great for me!
do note that you must use the correct DevCon.exe version, either x64 or x86 depending on the OS version.
Use wrongly and it will not work
where to get the devcon.exe for windows server 2012 x64
Regards,
Jothi
Hi, just follow the path for downloading WDK 8 (for Windows Server 2012) or WDK 8.1 (for Windows Server 2012 R2), devcon.exe is in the WDK.
Thanks a lot.
hey, did you find anyway of getting the hidden adapters for server 2008 R2?
On the other hand your solution is really cool!
Thanks for the feedback. I haven’t tested on Windows Server 2008 R2, you will need a Devcon.exe for that platform. Maybe this could help: http://social.technet.microsoft.com/wiki/contents/articles/182.how-to-obtain-the-current-version-of-device-console-utility-devcon-exe.aspx
http://www.gfi.com/blog/how-to-remove-hidden-network-adapters-from-virtual-machines/
for Windows 2008, use the method here to view the hidden devices
Excellent work
Pingback: Azure VM に RDP 接続が通らず、Ping も応答がない | 一身独立して、一国独立す。
Many thanks!!
I had not learned in a million years .
You saved the day
Thanks again
Glad I could help!
Hi,
I had problems getting the script to work on Windows 7 and Server 2008 R2. Then I found another script based on jscript. It does the trick for those systems.
https://github.com/kevinoid/remove-nonpresent-devices
Thanks for the help.
DJITS
Hi
I found that the limit for netvork adapters to be 143. Browsing stops working on 144.
Thanks Torgeir!
Thanks for this post! It saved me rebuilding our Exchange server in our lab!
I found a good module https://gallery.technet.microsoft.com/PowerShell-Device-60d73bb0#content which doesn’t have any nasty pre-reqs and, once downloaded and PowerShell execution policy set to RemoteSigned, then the following powershell removes the devices nicely
get-device -ShowNonpresentPresentDevices -Class GUID_DEVCLASS_NET | ? { $_.IsPresent -eq $False -and $_.Name -like ‘Microsoft Hyper-V Network Adapter*’ } | Uninstall-Device -Confirm:$false
Thanks, glad to help. Btw, recently static MAC address support should have been implemented, I haven’t had time to test yet. Ref. https://feedback.azure.com/forums/216843-virtual-machines/suggestions/5217933-static-mac-address