In this post, I will show you how to disable the local area connections that are not in connected state in a Windows Server/Desktop. It is quite common scenario that our servers will have at least one or two Network adapters in disconnected state if don’t use all available network ports on the server. There is no harm in having them in such condition, but a few monitoring softwares like HP SMH treats this condition as error state. Moreover, it is not great to have disconnected network adapters on the server as this can lead to some sort of confusion when someone is debugging a problem on the server.
The following powershell code helps you to disable the network adapters that are in disconnected state on remote/local computers. The code uses a WMI class called Win32_NetworkAdapter which stores the details of each network connection in the windows server along with its status(connected/disconnected, etc). The status code 7 for a network adapter indicates that it is in “media disconnected state”.
CODE
function Disable-DisconnectedAdapter{ [cmdletbinding()] param( [string[]]$ComputerName ) foreach($Computer in $ComputerName) { Write-Host "Working on $Computer" if(Test-Connection -ComputerName $Computer -Count 1 -ErrorAction 0) { try { $nics = Get-WmiObject -Class Win32_NetWorkAdapter -ComputerName $Computer -ErrorAction Stop | ? { $_.NetConnectionStatus -eq 7 } } catch { Write-Error "Failed to Query WMI class for network adapters on $Computer" Continue } foreach($nic in $nics) { try { $retval = $nic.disable() if($retval.returnvalue -eq 0) { "{0} network card disabled successfully" -f $nic.Name } else { "{0} network card disable failed" -f $nic.Name } } catch { "Error occurred while disabling {0}" -f $nic.Name continue } } } else { Write-Verbose "$Computer is offline" } } }
Test this code before you try in production environment.