Page 1 of 1

Reset ICS on Tap Adapter

Posted: Thu Jul 08, 2021 3:09 pm
by admininator
This seems to be a common issue: something goes fubar on ethernet adapter sharing internet with the tap adapter after rebooting windows. I looked around and found a couple of excellent powershell functions by Mike Robbins to assist in automating RESETTING ICS on the tap adapter.

A few general notes:
* Must run as administrator
* PS 3+ required
* Run from task scheduler after reboot

Powershell:

Code: Select all

<#
.SYNOPSIS
    RESET Internet Connection Status on OpenVPN Tap Connector on Windows after restart
	
.DESCRIPTION
    ICS sometimes fails after reboot, so resetting is required. RESET Internet Connection Status on OpenVPN Tap Connector on Windows after reboot.
	
.NOTES
	1) Stops OpenVPN service
	2) Disables ICS on ethernet adapter
	3) Enables ICS on ethernet adapter > tap adapter
	4) Starts OpenVPN service

	Run from Windows Task Scheduler 
	Task Settings: "Begin the task: At Startup", "Delay task for: 1 minute"

	MUST RUN AS ADMINISTRATOR!
	Requires -Version 3.0
	Requires -Version 3.0 -Modules NetAdapter  
	Functions from Mike Robbins: https://mikefrobbins.com/2017/10/19/configure-internet-connection-sharing-with-powershell/
#>


<###   USER VARIABLES   ###>
$TapAdapter   = 'OpenVPN TAP-Windows6'  # Adapter to which ICS is connected
$OVPNSvc      = 'OpenVPNService'        # OpenVPN service name
$GetICSStatus = $True                   # If run in console, will display status after making changes


<###   FUNCTIONS   ###>

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
            }
        }
    }
}

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()
            }
        }
    }
}

Stop-Service $OVPNSvc
Set-MrInternetConnectionSharing -InternetInterfaceName Ethernet -LocalInterfaceName $TapAdapter -Enabled $False
If ($GetICSStatus) {Get-MrInternetConnectionSharing -InternetInterfaceName Ethernet, $TapAdapter}

Set-MrInternetConnectionSharing -InternetInterfaceName Ethernet -LocalInterfaceName $TapAdapter -Enabled $True
If ($GetICSStatus) {Get-MrInternetConnectionSharing -InternetInterfaceName Ethernet, $TapAdapter}
Start-Service $OVPNSvc

I haven't tested it by actually rebooting yet, but all other tests worked. Also, it is probably a good idea to add this to client config directive:

Code: Select all

push "keepalive 10 120"

Re: Reset ICS on Tap Adapter

Posted: Thu Jul 08, 2021 5:28 pm
by TinCanTech
admininator wrote:
Thu Jul 08, 2021 3:09 pm
it is probably a good idea to add this to client config directive:

Code: Select all

push "keepalive 10 120"
Clients cannot use --push and --keepalive cannot be pushed.

Your script is probably rubbish as well.

Re: Reset ICS on Tap Adapter

Posted: Fri Jul 09, 2021 1:15 am
by admininator
TinCanTech wrote:
Thu Jul 08, 2021 5:28 pm
Clients cannot use --push and --keepalive cannot be pushed.
I'll keep that in mind.
Your script is probably rubbish as well.
We can't all be hanging out here waiting on you to be helpful. That's a pointless waste of time.

Re: Reset ICS on Tap Adapter

Posted: Fri Jul 09, 2021 1:31 am
by TinCanTech
admininator wrote:
Fri Jul 09, 2021 1:15 am
We can't all be hanging out here waiting on you to be helpful. That's a pointless waste of time.
I already helped you twice ..
TinCanTech wrote:
Thu Jul 08, 2021 5:28 pm
Clients cannot use --push and --keepalive cannot be pushed.
I am more helpful than you realise .. I force you to think for yourself.

Re: Reset ICS on Tap Adapter

Posted: Fri Jul 09, 2021 4:12 pm
by admininator
TinCanTech wrote:
Fri Jul 09, 2021 1:31 am
I am more helpful than you realise .. I force you to think for yourself.
I won't stop you from believing that.

Anyway, this thread was not asking for help, it was offering help to a seemingly common problem on windows. It happened to me and it also came up several times while researching a solution. So I thunked for myself reeel hard like, created a working solution and archived it here so others might benefit.

I didn't even beg for money.

Re: Reset ICS on Tap Adapter

Posted: Fri Jul 09, 2021 5:53 pm
by TinCanTech
You even admit that you have not thoroughly tested your script and you gave bad advice about openvpn config.

but hey .. you shared it, maybe some one will use it.

Re: Reset ICS on Tap Adapter

Posted: Sat Jul 10, 2021 12:04 am
by admininator
Worked on unplanned reboot today. Performed flawlessly, as expected.

Its public domain now. Even you can use it if someone using windows is foolish enough to hire you.

Re: Reset ICS on Tap Adapter

Posted: Sat Jul 10, 2021 12:35 am
by TinCanTech
Anybody who needs your script cannot afford me.

I tested your script and it fails under certain circumstances ..

Re: Reset ICS on Tap Adapter

Posted: Sat Jul 10, 2021 12:55 am
by TinCanTech
If you really want to share viable code then:
  • share it in the correct place
  • test it thoroughly
  • expect negative feedback

Re: Reset ICS on Tap Adapter

Posted: Sat Jul 10, 2021 12:54 pm
by admininator
TinCanTech wrote:
Sat Jul 10, 2021 12:55 am
If you really want to share viable code then:
  • share it in the correct place
  • test it thoroughly
  • expect negative feedback
Negative feedback is fine.
TinCanTech wrote:
Thu Jul 08, 2021 5:28 pm
Your script is probably rubbish as well.
This is childish and retarded.

Re: Reset ICS on Tap Adapter

Posted: Sat Jul 10, 2021 1:03 pm
by admininator
TinCanTech wrote:
Sat Jul 10, 2021 12:35 am
I tested your script and it fails under certain circumstances ..
I think you're lying. You didn't test anything. Just more of the same secret king garbage from you. Why would anyone hire an observable liar?

I honestly don't know how you're even allowed to post here. You do far more to turn people away from this product than any kind of support. I noticed some OVPN devs have posted recently. Probably to counteract your anti-usefulness.

Re: Reset ICS on Tap Adapter

Posted: Sat Jul 10, 2021 1:27 pm
by TinCanTech
Have you tested your script against a machine with more than one TAP-Adapter installed ..