在 Windows 10 上自动在网络适配器之间共享 Internet

在 Windows 10 上自动在网络适配器之间共享 Internet

我将 VPN 用于我的控制台,方法是在我的笔记本电脑上启动 VPN 连接并与我的以太网适配器共享 VPN 互联网适配器互联网。

问题是,重启后有时会出现错误,我需要取消共享适配器的互联网连接,然后重新共享。每次我需要点击很多次才能完成共享,这变得越来越烦人。

有谁知道可以自动化这个过程的命令/PowerShell解决方案吗?

答案1

PowerShell 启用/禁用 ICS 并允许其他适配器访问

有几个函数可以帮助使用 PowerShell 版本 3.0+ 完成此任务,这些函数在使用 PowerShell 配置 Internet 连接共享经我确认该帖子适用于 Windows 10。

我会稍微缩短它并仅发布完成您描述的任务所需的 PowerShell 命令的部分,但如果需要,您可以阅读完整的文章以了解更多详细信息。


命令执行

声明这两个函数后,您只需运行一行代码即可在 VPN 和以太网适配器之间启用 ICS。完成后,您可以运行另一行代码以从 VPN 适配器完全禁用 ICS 功能。

下面是运行它所需的完整代码和函数,但我想首先在本节中澄清一些事情,以帮助确保您了解如何让它完成您的任务。

电源外壳

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

笔记

  • -InternetInterfaceName参数传递的是您想要启用 ICS 的网络适配器的名称。

  • -LocalInterfaceName参数传递的是您想要允许使用 ICS 启用适配器的 Internet 的网络适配器的名称。

  • 运行该Set-MrInternetConnectionSharing -InternetInterfaceName "VPN" -LocalInterfaceName "Ethernet" -Enabled $true命令将导致 在此处输入图片描述


完整的 PowerShell 代码

笔记:我已经注释掉了禁用在完整的脚本中,您需要删除或添加注释或调整逻辑以了解是否在每次执行时启用或禁用。

进一步调整或扩展这一点,找到一种基于某些事件使用该解决方案完全自动启用或禁用的方法等,应该只是在您的环境中解决的一件小事 - 这至少解决了困难的部分。

#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

脚本源


支持资源

相关内容