[Powershell] Disable ICS sharing when USB adapter is already unplugged RRS feed

  • Question

  • Hello,

    I have question about internet connection sharing in Windows 7.

    The problem I need to solve is following:

    I have several devices which, when connected to computer, creates new network connection. I want to share my main Ethernet internet connection whenever new device is connected to PC with this device.

    I have following code for internet sharing:

    Write-Output "Finding RNDIS Network adapters..."
    $NICs = Get-WMIObject Win32_NetworkAdapterConfiguration -computername . | where{$_.Description -like "*RNDIS device*"}
    if ($NICs) {

    $MainAdapters = get-wmiobject win32_networkadapter -computername . | WHERE {$_.Name -LIKE '*Ethernet*' -AND $_.NetConnectionID -LIKE 'Local Area Connection*'}
    $RNDISAdapters = get-wmiobject win32_networkadapter -computername . | WHERE {$_.Name -LIKE '*RNDIS*'}

    ForEach ($MainAdapter in $MainAdapters) {
    $MainAdapterE = $MainAdapter.NetConnectionID

    ForEach ($RNDISAdapter in $RNDISAdapters) {
    $RNDISAdapterE = $RNDISAdapter.NetConnectionID

    $m = New-Object -ComObject HNetCfg.HNetShare

    $c = $m.EnumEveryConnection |? { $m.NetConnectionProps.Invoke($_).Name -eq $MainAdapterE }
    $c1 = $m.EnumEveryConnection |? { $m.NetConnectionProps.Invoke($_).Name -eq $RNDISAdapterE }
    $config = $m.INetSharingConfigurationForINetConnection.Invoke($c)
    $config1 = $m.INetSharingConfigurationForINetConnection.Invoke($c1)


    Remove-Variable m


    The problem is that, when I unplug old device and connect new device, the code does not enable sharing between $MainAdapterE and $RNDISAdapterE. Instead I am getting error in PS:

    Exception calling "EnableSharing" with "1" argument(s): "An event was unable to invoke any of the subscribers (Exceptio
    n from HRESULT: 0x80040201)"
    At C:\path\ethernet.ps1:94 char:25
    +         $config1.EnableSharing <<<< (1)
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : ComMethodTargetInvocation

    I understand that this is somehow tied to the fact that when I unplug old device, without un-sharing the connection, the sharing information is not cleared. This in turn could cause the error, when I try to share connection with newly connected device.

    When I disable sharing prior to remove old device with Powershell, then I am able to get the above code to work, when I plug in new device. 

    Please help me to solve this issue.

    • Moved by Bill_Stewart Tuesday, November 7, 2017 10:49 PM We cannot troubleshoot script for user from afar
    Wednesday, October 4, 2017 9:19 AM

All replies

  • Sharing is configured by device assuming the device has the correct drivers installed and supports ICS.

    If you use the GUI to configure each device it should be remembered the next time the device is plugged in.  I would suggest posting in the networking forum or the Windows 10 forum for help on how to manage ICS with USB networkable devices.

    In all cases the device must be recognized and configured before you can use that script.  This can only be done through physical interaction for security purposes.  Automation would allow anyone to connect to and hack your PC.


    Wednesday, October 4, 2017 11:06 AM
  • Thanks for answering.

    I have figured out how to configure USB Network unit. This snipped is executed after if ($NICs)

    Foreach($NIC in $NICs) { 

        $ip = "" 
        $subnet = ""
        $NIC.EnableStatic($ip, $subnet)
        $NIC.SetGateways("", 1)

    So the device is appropriately configured before code tries to share connection.

    The code works without problems when fresh unit is plugged in and there was not any active sharing enabled before that.

    Wednesday, October 4, 2017 11:25 AM
  • Then you have enabled sharing on all devices by default and the drivers are all installed for the device you are connecting.  You must have also set the default device behavior. If using some third party drivers and networking devices they may have globally enabled their devices which a driver can do but it is a security issue.

    If it is working then you are set but be careful. If you are running the scrip manually after attaching the device then that can work assuming you are an admin running elevated. 


    Wednesday, October 4, 2017 11:32 AM