Using PowerShell to remotely enable/disable a network interface….
I have recently run into an issue with a multi-homed Windows 2012 Server where one of the network interfaces simply stops responding. There are no errors in the Event Logs and no other obvious error reporting to indicate the network interface has failed other than the network icon in the system tray shows a warning -
It is always the same interface and I am still able to connect with Remote Desktop to the affected machine through another interface.
The simple fix is to connect with Remote Desktop, disable the affected interface, then re-enable it. Everything goes back to normal until some random event causes the interface to fail. To date I have not been able to track down what is causing the problem. I have performed the basics troubleshooting of updating the network interface drivers and even swapping out the network adapter itself (albeit with an identical, out-of-box interface). Thus far it has not been a critical enough to force more in depth troubleshooting. The machine is a relatively low priority machine and since I can still Remote Desktop to it and I have a simple fix, I have not spent a lot of time trying to figure it out. But I still wanted a quicker Band-Aid fix and decided to write or find a small PowerShell script that would remote enable/disable the affected interface so I would not have to do all of the click steps to go into Network Sharing, locate the interface, disable it, then enable it.
I didn’t want to re-create the wheel (unless I had to!) so I did some searching and quickly found the following script by Sitaram Pamarthi. I like this script because it lets you choose whether you want to run it against a local or a remote machine as well as provides some other convenient options. Eventually I will enable PowerShell Remoting to the machine in question, but for now I am happy with just doing a Remote Desktop to the machine then running the script.
Because of the flexibility, there are also some cautions that go along with it – It is entirely possible to disable the network interface that you are connecting to, thus disabling access to the machine from Remote Desktop forcing you to go to the machine, logon, and re-enable the Interface. So be careful of which interface you enable/disable.
-------------------------------------------
Code : Set-NetworkConnection.ps1
[cmdletbinding()]
param (
[parameter(ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
[string[]]$ComputerName = $env:computername,
[switch]$Enable,
[switch]$Disable,
[String]$ConnectionName = "Local Area Connection"
)
begin {
if(-not($enable -or $disable)) {
write-Error " You should select either of -Enable or -Disable switches. Exiting"
exit
}
}
process {
foreach ($Computer in $ComputerName) {
$Computer = $Computer.ToUpper()
if(Test-Connection -ComputerName $Computer -Count 1 -ea 0) {
$Networks = Get-WmiObject Win32_NetworkAdapter -ComputerName $Computer | ? {$_.NetConnectionID -match $ConnectionName}
foreach ($Network in $Networks) {
if($Enable) {
$retval = $Network.Enable()
if($retval.ReturnValue -eq 0) {
Write-host "Successfully enabled `"$($network.NetConnectionID)`" on $Computer"
} else {
write-host "Failed to enable `"$($network.NetConnectionID)`" on $Computer"
}
} elseif($Disable) {
$retval = $Network.Disable()
if($retval.returnValue -eq 0) {
Write-host "Successfully disabled `"$($network.NetConnectionID)`" on $Computer"
} else {
write-host "Failed to disable `"$($network.NetConnectionID)`" on $Computer"
}
}
}
}
}
}
end {}
Now, with the script in place, I can run the following to disable the network interface -
.\Set-NetworkConnection -ComputerName MyPCName -Disable -ConnectionName “Ethernet″
I can then run the following command to re-enable the interface -
.\Set-NetworkConnection -ComputerName MyPCName –Enable -ConnectionName “Ethernet″
-------------------------------------------
I will do a follow-up post on how to turn on and configure PowerShell Remoting so the script can be run from a local machine against a remote machine.
Many thanks to Sitaram Pamarthi for posting this!
-Cheers
Comments
- Anonymous
July 11, 2013
I am glad to know that my script helped you.