Skip to content

Instantly share code, notes, and snippets.

@igoravl
Created March 27, 2015 01:33
Show Gist options
  • Save igoravl/db02a8769fd6af3028f7 to your computer and use it in GitHub Desktop.
Save igoravl/db02a8769fd6af3028f7 to your computer and use it in GitHub Desktop.
NetConnectionSharing (PowerShell Module)
#Based on code from http://superuser.com/questions/470319/how-to-enable-internet-connection-sharing-using-command-line
Function Set-NetConnectionSharing
{
Param
(
[Parameter(Mandatory=$true)]
[string]
$InternetConnection,
[Parameter(Mandatory=$true)]
[string]
$LocalConnection,
[Parameter(Mandatory=$true)]
[switch]
$Enabled
)
Begin
{
$netShare = $null
try
{
# Create a NetSharingManager object
$netShare = New-Object -ComObject HNetCfg.HNetShare
}
catch
{
# Register the HNetCfg library (once)
regsvr32 /s hnetcfg.dll
# Create a NetSharingManager object
$netShare = New-Object -ComObject HNetCfg.HNetShare
}
}
Process
{
# Find connections
$publicConnection = $netShare.EnumEveryConnection |? { $netShare.NetConnectionProps.Invoke($_).Name -eq $InternetConnection }
$privateConnection = $netShare.EnumEveryConnection |? { $netShare.NetConnectionProps.Invoke($_).Name -eq $LocalConnection }
# Get sharing configuration
$publicConfig = $netShare.INetSharingConfigurationForINetConnection.Invoke($publicConnection)
$privateConfig = $netShare.INetSharingConfigurationForINetConnection.Invoke($privateConnection)
if ($Enabled.IsPresent)
{
$publicConfig.EnableSharing(0)
$privateConfig.EnableSharing(1)
}
else
{
$publicConfig.DisableSharing()
$privateConfig.DisableSharing()
}
}
}
@DATAPOWERELECTRICAL
Copy link

Hi thank you for this.

I noticed on Win10, i can not get it to start using this command
"start a command prompt window (Run as Administrator) and run this command:
powershell.exe -ExecutionPolicy Bypass -File "NetConnectionSharing.ps1"

I can run this in PS directly however after reboot, it loose the config.. :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment