Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Enabling Windows ICS from PowerShell

$
0
0

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


Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>