none
Enabling Windows ICS from PowerShell RRS feed

  • Question

  • I am trying to use Windows ICS to share the computer's internet connection with a separate adapter in PowerShell. This is what I have so far:

    # Register the HNetCfg library (once)
    regsvr32 hnetcfg.dll
    
    $MainAdapter = Get-NetAdapter | Where-Object {$_.MediaConnectionState -eq 'Connected' -and $_.PhysicalMediaType -ne 'Unspecified'} | Sort-Object LinkSpeed -Descending
    
    function EnableICS([string]$ID)
    {
        # Create a NetSharingManager object
        $m = New-Object -ComObject HNetCfg.HNetShare
    
        # List connections
        $m.EnumEveryConnection |% { $m.NetConnectionProps.Invoke($_).Guid }
    
        # Find connection
        $c = $m.EnumEveryConnection |? { $m.NetConnectionProps.Invoke($_).Guid -eq $ID }
        
        # Get sharing configuration
        $config = $m.INetSharingConfigurationForINetConnection.Invoke($c)
        
        # See if sharing is enabled
        Write-Output $config.SharingEnabled
    
        # See the role of connection in sharing
        # 0 - public, 1 - private
        # Only meaningful if SharingEnabled is True
        Write-Output $config.SharingType
    
        # Enable sharing (0 - public, 1 - private)
        $config.EnableSharing(0)
        
        # Disable sharing
        #$config.DisableSharing()
    }
    
    EnableICS($MainAdapter.InterfaceGuid)

    It works, but only on systems with two network adapters. ICS needs an adapter as an input, and another as an output.  In the ICS GUI, when there are more than two adapters, it asks for a "Home network connection" that it uses as the output. But when I run this code, it doesn't specify the other adapter, so it doesn't do anything. How can I specify that other adapter in my script?


    Wasabi Fan

    Saturday, August 23, 2014 3:45 AM

Answers

  • Never mind, I figured it out. I had to enable sharing for each of the 2 adapters. The first one is just as it is in the example above, but the second needed to be set as private (a "1" instead of a "0"). Thanks anyway!


    Wasabi Fan

    • Marked as answer by Wasabi Fan Saturday, August 23, 2014 4:28 AM
    Saturday, August 23, 2014 4:28 AM

All replies

  • The active adapter is the input.  TH one you are sharing is the output.


    ¯\_(ツ)_/¯

    Saturday, August 23, 2014 4:09 AM
  • "active adapter"? I want to share one adapter's connection with a different adapter. In the GUI, I would select the first adapter, open it's properties, enable sharing in that dialog, and select the second adapter from the list that it gives me. In my script, I currently only specify one, so because it can't just guess for the second it doesn't do anything. Example:

    In this example, the device connected to the "EV3" adapter will be able to access the internet through the "Ethernet 4" adapter. That second box is what I am looking to fill out (because right now it just says "Select a private network connection" after I run my script).


    Wasabi Fan

    Saturday, August 23, 2014 4:18 AM
  • You share the Internet adapter with all of theother adapters/connections.  It is stock ICS.  Select the active Internet Adapter and share it.   All other active links will be able to see it.

    Assume:  Adapter "A" is Wi-Fi to Internet.  Adapter B is Ethernet to PC2.  Sharing Adapter A allows PC2 to browse Internet.

    ICS has been around for a decade or more.  Here is how it works: http://support.microsoft.com/kb/234815


    ¯\_(ツ)_/¯

    Saturday, August 23, 2014 4:24 AM
  • Never mind, I figured it out. I had to enable sharing for each of the 2 adapters. The first one is just as it is in the example above, but the second needed to be set as private (a "1" instead of a "0"). Thanks anyway!


    Wasabi Fan

    • Marked as answer by Wasabi Fan Saturday, August 23, 2014 4:28 AM
    Saturday, August 23, 2014 4:28 AM
  • You didn't say that you had already changed one adapter to public.  Only one can be "shared" or public all others must be private.  You can share A connection with everybody.  You cannot share multiple connections.  I am pretty sure that if you set it wrong and reboot Windows will choose one and reset the others.  It may not choose the one you want.

    ¯\_(ツ)_/¯

    Saturday, August 23, 2014 4:43 AM
  • Hello!

    Right now I'm dealing with exactly same problem but I've got strange issue -

    $m.EnumEveryConnection

    returns absolutely nothing!

    Can you please help me? Any suggestion, any idea

    Tuesday, September 2, 2014 12:40 PM
  • Hello!

    Right now I'm dealing with exactly same problem but I've got strange issue -

    $m.EnumEveryConnection

    returns absolutely nothing!

    Can you please help me? Any suggestion, any idea


    Can you please start your own question.  This one is closed.

    ¯\_(ツ)_/¯

    Tuesday, September 2, 2014 12:44 PM
  • I am trying to use Windows ICS to share the computer's internet connection with a separate adapter in PowerShell. This is what I have so far:

    # Register the HNetCfg library (once)
    regsvr32 hnetcfg.dll
    
    $MainAdapter = Get-NetAdapter | Where-Object {$_.MediaConnectionState -eq 'Connected' -and $_.PhysicalMediaType -ne 'Unspecified'} | Sort-Object LinkSpeed -Descending
    
    function EnableICS([string]$ID)
    {
        # Create a NetSharingManager object
        $m = New-Object -ComObject HNetCfg.HNetShare
    
        # List connections
        $m.EnumEveryConnection |% { $m.NetConnectionProps.Invoke($_).Guid }
    
        # Find connection
        $c = $m.EnumEveryConnection |? { $m.NetConnectionProps.Invoke($_).Guid -eq $ID }
        
        # Get sharing configuration
        $config = $m.INetSharingConfigurationForINetConnection.Invoke($c)
        
        # See if sharing is enabled
        Write-Output $config.SharingEnabled
    
        # See the role of connection in sharing
        # 0 - public, 1 - private
        # Only meaningful if SharingEnabled is True
        Write-Output $config.SharingType
    
        # Enable sharing (0 - public, 1 - private)
        $config.EnableSharing(0)
        
        # Disable sharing
        #$config.DisableSharing()
    }
    
    EnableICS($MainAdapter.InterfaceGuid)

    It works, but only on systems with two network adapters. ICS needs an adapter as an input, and another as an output.  In the ICS GUI, when there are more than two adapters, it asks for a "Home network connection" that it uses as the output. But when I run this code, it doesn't specify the other adapter, so it doesn't do anything. How can I specify that other adapter in my script?


    Wasabi Fan

    I've tried $config.DisableSharing() to disable sharing but it does not work. any suggestions?
    Monday, March 2, 2015 9:02 AM
  • This thread has been closed for more than 6 months.  Please start you own topic with your question. You can link back to this thread as a reference.

    ¯\_(ツ)_/¯

    Monday, March 2, 2015 4:24 PM