0

I can easily disable a network device via something like:

$wmi = gwmi win32_networkadapter -filter "name LIKE '$target%'"
$wmi.disable()

or in newer Windows versions:

get-netadapter -InterfaceDescription "$target*" | Disable-NetAdapter -Confirm:$false

If a disabled device (e.g. a USB-to-Ethernet adapter) is physically unplugged, it will again identify as disabled when it is plugged back in.

I can re-enable using similar code, but ONLY if the device is physically connected.

  • WMI is able to return the adapter, but the "enable()" method gives an "Invalid method Parameter(s) " error.
  • Get-NetAdapter returns nothing if the device is not present.

How can I enable it when it is not physically present so that it will work when it is re-connected? IOW, where is the "disabled" status of a device stored, and can it be programmatically flipped?

Thanks!

  • You could register a listener that waits for devices to be detected (plugged in), then trigger the device to be enabled. – root Nov 2 '17 at 21:06
0

The reason why it needs to be connected is the fact that the '$target%'

changes based on what else is connected. It is not a static value. As suggested in the comments, the best method would be to have a listener and re-enable based on changes with the appropriate '$target%' value.

As far as where this is stored, it would be in Registry:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002BE10318}

is one location, where the identifier at the end is specific for each device. However:

Enabling a network adapter by looking for a single key to trigger in the registry doesn't work. By doing a file compare on exported .reg files before and after a network connection is enabled, you can see that many changes in the registry were implemented. Thus the listener is the best approach

  • I don't think the problem is the $target filtering. WMI queries without any filters don't return the disconnected device either. – Teknowledgist Nov 3 '17 at 12:32
  • If there isn't a single place (or two) in the registry, then I guess I have to accept the listener suggestion. I'm not thrilled with that because the listener may sit there for eternity (if the device never returns). By "listener", do you mean a scheduled task with an event trigger, or is there some other mechanism which which I'm not familiar? – Teknowledgist Nov 3 '17 at 12:39
0

You might be able to listen for the WMI InstanceCreationEvent and enable the device on creation. I don't have a similar device so I can't test this.

function Register-USB2ETH {

    $query = "SELECT * FROM __InstanceCreationEvent " +
         "WITHIN 5 " +
         "WHERE TargetInstance ISA 'Win32_NetworkAdapter' " +
         "AND TargetInstance.Name = $target "

    Register-WmiEvent -Query $query -SourceIdentifier USB2ETH -Action { (Get-WmiObject Win32_NetworkAdapter -filter "Name=$target").Enable() } | Out-Null
}

Remove with Unregister-Event -SourceIdentifier KingstonPlugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.