indexpost archiveatom feed syndication feed icon

Flakey Network Managers and Powershell

2015-03-02

I don't claim much experience in (successfully) managing Windows systems. Any more, the only Windows machine I use is a work laptop. My mother however has a Windows desktop that I am occassionally tasked with "fixing" if things go wrong.

I haven't determined a root cause, it is infrequent enough thus far to be very low priority, but for whatever reason during the last few months the desktop has occasionally failed to resolve the local network connection on start-up. It's not a big deal, the simplest solution has been to navigate to the Network Settings and disabling and then re-enabling the Ethernet connection1.

I think it says something about the un-intuitiveness of the interface though that every time I or my mother have to go looking for that particular menu we both stumble around the Control Panel, leading me to look for a less hands-on solution.

All the heavy lifting and research was done for me in this post. The gist of which, for me, was the following.2

    $network_connection = Get-WmiObject -Class Win32_NetworkAdapter `
                          -filter "Name LIKE '%ethernet%'"
    $network_connection.disable()
    $network_connection.enable()
    Start-Sleep -s 2

The Win32_NetworkAdapter class is actually deprecated, but as I'm on a system with only Powershell 1.0 this works without any further trouble. The non-obvious part was making this particular script runnable with minimal effort. Anyone who has so much as looked at Powershell has surely seen the error:

    ... cannot be loaded because the execution of scripts is disabled on this
    system. Please see "get-help about_signing" for more details.

Which is, I think, a step in the right direction for security on Windows systems, but a pain nonetheless for what I was doing. I'm not really comfortable changing the ExecutionPolicy system wide as a matter of principle (let alone on a system I'm not going to be running myself), so instead I settled on a shortcut (on the Desktop, labeled "restart the internet") with Target set to:

    powershell -ExecutionPolicy ByPass -File C:\[dir]\network_restart.ps1

So I don't need to go the the trouble of figuring out how to sign a 4 line script myself, but I also don't need to worry about white-listing any scripts that might end up on the system.

The sleep is just there to give the system time to restart while keeping the cmd.exe window open, as a kind of indicator that things probably aren't quite done. All those user experience books might be sinking in! While the process takes place the cmd.exe window displays nothing useful:

    __GENUS          : 2
    __CLASS          : __PARAMETERS
    __SUPERCLASS     :
    __DYNASTY        : __PARAMETERS
    __RELPATH        :
    __PROPERTY_COUNT : 1
    __DERIVATION     : {}
    __SERVER         :
    __NAMESPACE      :
    __PATH           :
    ReturnValue      : 0

    __GENUS          : 2
    __CLASS          : __PARAMETERS
    __SUPERCLASS     :
    __DYNASTY        : __PARAMETERS
    __RELPATH        :
    __PROPERTY_COUNT : 1
    __DERIVATION     : {}
    __SERVER         :
    __NAMESPACE      :
    __PATH           :
    ReturnValue      : 0

But I think it's enough that it looks like it's working.


  1. Sadly relevant
  2. Powershell uses the backtick for line continuations, included here for formatting