3

I use a VPN for my console, and I do this by starting a VPN connection on my laptop and sharing the VPN Internet adapters Internet with my Ethernet adapter.

The issue is that upon reboot, errors sometimes occur and I need to un-share the adapters Internet connection and then re-share it. This becomes progressively more annoying every time I need to do it with all the clicks and such that it takes to get there.

Does anyone know a command/PowerShell solution to automate this process?

2
  • By sharing, do you mean Internet Connection Sharing or bridging adapters? How many network adapters do you have? Which one is used for the VPN? It would also help to include a screenshot of the network adapters in Settings > Network & Internet > Status > Change adapter options.
    – harrymc
    Commented Feb 28, 2020 at 21:54
  • Internet connection sharing. I have 10 Network adapters, most are for different VPN connections.
    – Ecstasy
    Commented Feb 29, 2020 at 23:21

1 Answer 1

3
+50

PowerShell enable/disable ICS and allow other adapter access

There are a couple functions to help with this task using PowerShell version 3.0+ that are written and talked about in the Configure Internet Connection Sharing with PowerShell post that I confirmed works with Windows 10.

I'll shorten it up a bit and post just the parts of the PowerShell commands which you need to complete the task you describe, but you can read over the full post for more detail if needed.


Command Execution

After declaring the two functions, you simply just need to run one line of code to enable the ICS between the VPN and Ethernet adapter. When done, you can run another line of code to disable the ICS feature entirely from the VPN adapter.

The full code with the functions to run for it to work is below, but I want to clarify a few things in this section first to help ensure you understand how to get it to work for your task.

Powershell

## List adapter names 
(Get-NetAdapter).Name 

## List adapter ICS statuses for the two that'll use ICS
Get-MrInternetConnectionSharing -InternetInterfaceName "Ethernet", "VPN" 

## Enable ICS on the VPN adapter and allow Ethernet network to use it
Set-MrInternetConnectionSharing -InternetInterfaceName "VPN" -LocalInterfaceName "Ethernet" -Enabled $true

## Disable ICS entirely on the VPN adapter when done
Set-MrInternetConnectionSharing -InternetInterfaceName "VPN" -LocalInterfaceName "Ethernet" -Enabled $false

Notes

  • The -InternetInterfaceName parameter is passed the name of the network adapter which you want to enable ICS.

  • The -LocalInterfaceName parameter is passed the name of the network adapter which you want to allow to use the ICS enabled adapter's Internet.

  • Running the Set-MrInternetConnectionSharing -InternetInterfaceName "VPN" -LocalInterfaceName "Ethernet" -Enabled $true command will result in enter image description here


Full PowerShell Code

Note: I have commented out the disable in the full script so you'll want to remove or add comments or adjust the logic to know if you are enabling or disabling per execution.

Adjusting or expanding on this further, finding a way to fully automate an enable or disable using this solution based on some event, etc. should just be a trivial matter to figure out within your environment—this takes care of the hard part at least.

#Requires -Version 3.0
function Get-MrInternetConnectionSharing {

<#
.SYNOPSIS
    Retrieves the status of Internet connection sharing for the specified network adapter(s).

.DESCRIPTION
    Get-MrInternetConnectionSharing is an advanced function that retrieves the status of Internet connection sharing
    for the specified network adapter(s).

.PARAMETER InternetInterfaceName
    The name of the network adapter(s) to check the Internet connection sharing status for.

.EXAMPLE
    Get-MrInternetConnectionSharing -InternetInterfaceName Ethernet, 'Internal Virtual Switch'

.EXAMPLE
    'Ethernet', 'Internal Virtual Switch' | Get-MrInternetConnectionSharing

.EXAMPLE
    Get-NetAdapter | Get-MrInternetConnectionSharing

.INPUTS
    String

.OUTPUTS
    PSCustomObject

.NOTES
    Author:  Mike F Robbins
    Website: http://mikefrobbins.com
    Twitter: @mikefrobbins
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory,
                   ValueFromPipeline,
                   ValueFromPipelineByPropertyName)]
        [Alias('Name')]
        [string[]]$InternetInterfaceName
    )
    BEGIN {
        regsvr32.exe /s hnetcfg.dll
        $netShare = New-Object -ComObject HNetCfg.HNetShare
    }
    PROCESS {
        foreach ($Interface in $InternetInterfaceName){
            $publicConnection = $netShare.EnumEveryConnection |
            Where-Object {
                $netShare.NetConnectionProps.Invoke($_).Name -eq $Interface
            }
            try {
                $Results = $netShare.INetSharingConfigurationForINetConnection.Invoke($publicConnection)
            }
            catch {
                Write-Warning -Message "An unexpected error has occurred for network adapter: '$Interface'"
                Continue
            }
            [pscustomobject]@{
                Name = $Interface
                SharingEnabled = $Results.SharingEnabled
                SharingConnectionType = $Results.SharingConnectionType
                InternetFirewallEnabled = $Results.InternetFirewallEnabled
            }
        }
    }
}

#Requires -Version 3.0 -Modules NetAdapter
function Set-MrInternetConnectionSharing {

<#
.SYNOPSIS
    Configures Internet connection sharing for the specified network adapter(s).

.DESCRIPTION
    Set-MrInternetConnectionSharing is an advanced function that configures Internet connection sharing
    for the specified network adapter(s). The specified network adapter(s) must exist and must be enabled.
    To enable Internet connection sharing, Internet connection sharing cannot already be enabled on any
    network adapters.

.PARAMETER InternetInterfaceName
    The name of the network adapter to enable or disable Internet connection sharing for.

 .PARAMETER LocalInterfaceName
    The name of the network adapter to share the Internet connection with.

 .PARAMETER Enabled
    Boolean value to specify whether to enable or disable Internet connection sharing.

.EXAMPLE
    Set-MrInternetConnectionSharing -InternetInterfaceName Ethernet -LocalInterfaceName 'Internal Virtual Switch' -Enabled $true

.EXAMPLE
    'Ethernet' | Set-MrInternetConnectionSharing -LocalInterfaceName 'Internal Virtual Switch' -Enabled $false

.EXAMPLE
    Get-NetAdapter -Name Ethernet | Set-MrInternetConnectionSharing -LocalInterfaceName 'Internal Virtual Switch' -Enabled $true

.INPUTS
    String

.OUTPUTS
    PSCustomObject

.NOTES
    Author:  Mike F Robbins
    Website: http://mikefrobbins.com
    Twitter: @mikefrobbins
#>

    [CmdletBinding()]
    param (
        [Parameter(Mandatory,
                   ValueFromPipeline,
                   ValueFromPipelineByPropertyName)]
        [ValidateScript({
            If ((Get-NetAdapter -Name $_ -ErrorAction SilentlyContinue -OutVariable INetNIC) -and (($INetNIC).Status -ne 'Disabled' -or ($INetNIC).Status -ne 'Not Present')) {
                $True
            }
            else {
                Throw "$_ is either not a valid network adapter of it's currently disabled."
            }
        })]
        [Alias('Name')]
        [string]$InternetInterfaceName,
        [ValidateScript({
            If ((Get-NetAdapter -Name $_ -ErrorAction SilentlyContinue -OutVariable LocalNIC) -and (($LocalNIC).Status -ne 'Disabled' -or ($INetNIC).Status -ne 'Not Present')) {
                $True
            }
            else {
                Throw "$_ is either not a valid network adapter of it's currently disabled."
            }
        })]
        [string]$LocalInterfaceName,
        [Parameter(Mandatory)]
        [bool]$Enabled
    )
    BEGIN {
        if ((Get-NetAdapter | Get-MrInternetConnectionSharing).SharingEnabled -contains $true -and $Enabled) {
            Write-Warning -Message 'Unable to continue due to Internet connection sharing already being enabled for one or more network adapters.'
            Break
        }
        regsvr32.exe /s hnetcfg.dll
        $netShare = New-Object -ComObject HNetCfg.HNetShare
    }
    PROCESS {
        $publicConnection = $netShare.EnumEveryConnection |
        Where-Object {
            $netShare.NetConnectionProps.Invoke($_).Name -eq $InternetInterfaceName
        }
        $publicConfig = $netShare.INetSharingConfigurationForINetConnection.Invoke($publicConnection)
        if ($PSBoundParameters.LocalInterfaceName) {
            $privateConnection = $netShare.EnumEveryConnection |
            Where-Object {
                $netShare.NetConnectionProps.Invoke($_).Name -eq $LocalInterfaceName
            }
            $privateConfig = $netShare.INetSharingConfigurationForINetConnection.Invoke($privateConnection)
        }
        if ($Enabled) {
            $publicConfig.EnableSharing(0)
            if ($PSBoundParameters.LocalInterfaceName) {
                $privateConfig.EnableSharing(1)
            }
        }
        else {
            $publicConfig.DisableSharing()
            if ($PSBoundParameters.LocalInterfaceName) {
                $privateConfig.DisableSharing()
            }
        }
    }
}

#(Get-NetAdapter).Name
#Get-MrInternetConnectionSharing -InternetInterfaceName "Ethernet", "VPN" 
Set-MrInternetConnectionSharing -InternetInterfaceName "VPN" -LocalInterfaceName "Ethernet" -Enabled $true
#Set-MrInternetConnectionSharing -InternetInterfaceName "VPN" -LocalInterfaceName "Ethernet" -Enabled $false

Script Source


Supporting Resources

1
  • 1
    This answer deserves more credit, thanks for the powershell workaround, really works! Commented Nov 26, 2020 at 18:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .