locked
Disable ICS sharing when USB adapter is already unplugged RRS feed

  • 質問

  • Crossposted from Scripting forum.

    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)

    $config.EnableSharing(0)
    $config1.EnableSharing(1)

    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.
    2017年10月4日 12:13

すべての返信

  • Hi zenofexx ,

    I didn't found any related information about it in Microsoft official documents. Also, I will continue to research and text in the future.
    If we have any updates about this issue, we will keep you posted as soon as possible. Your kind understanding is appreciated.

    Best Regards,

    Candy


    Please remember to mark the replies as answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    2017年10月5日 14:06
  • I have the same problem, did you find a solution?
    2017年12月12日 12:04
  • This is exactly the problem I am having right now.

    If I manually go into the Network Adapter settings page (Control Panel) and disable sharing, then it cleans up the connection and I can then enable sharing on the new device.   But, I'm trying to find a way to handle this programmatically.   I've compared registry settings but nothing has jumped out at me yet as being of any use.  

    Is there any chance a Microsoft Employee could possibly help with this problem?   It's really a show-stopper for me at this time.

    2020年7月27日 23:27