Windows PowerShell 重新桥接网卡脚本

$NetShare = New-Object -ComObject HNetCfg.HNetShare
$wlan = $null
$ethernet = $null

foreach ($int in $NetShare.EnumEveryConnection) {
  $props = $NetShare.NetConnectionProps.Invoke($int)
  if ($props.Name -eq "以太网") {
    $wlan = $int;
  }
  if ($props.Name -eq "OpenVPN") {
    $ethernet = $int;
  }
}

$wlanConfig = $NetShare.INetSharingConfigurationForINetConnection.Invoke($wlan);
$ethernetConfig = $NetShare.INetSharingConfigurationForINetConnection.Invoke($ethernet);

$wlanConfig.DisableSharing();
$ethernetConfig.DisableSharing();

$wlanConfig.EnableSharing(0);
$ethernetConfig.EnableSharing(1);

Get-NetAdapter -Name 'OpenVPN' | Restart-NetAdapter

保存为ps1格式文件

CMD执行脚本命令

PowerShell.exe -ExecutionPolicy Unrestricted -File "C:\NetConnectionSharing.ps1"