【问题标题】:Netadapter Switcher PowerShell ScriptNetadapter 切换器 PowerShell 脚本
【发布时间】:2019-07-17 13:48:18
【问题描述】:
Function AdapterSwitcher {

While ($true) {

$Ethernet = Get-NetAdapter | where-object {$_.Status -contains "Up"} | Where-Object {$_.PhysicalMediaType -eq "802.3" -and {$_.Name -notlike "PANGP" }}
 if ($Ethernet -eq $null)
        {
        Write-Host "Ethernet Not Detected, Enabling WiFi"
            #When the value is null this means that there is no wired connection and the wireless must be enabled
            $WiFiNetadapter = get-netadapter | where-object {$_.Status -contains "Up"} | Where-Object {$_.PhysicalMediaType -eq "Native 802.11"} 
            $WiFiNetadapter | Enable-NetAdapter -Confirm:$false -Verbose


        }
        else
        {
        Write-Host "Disabling WiFi Network Adapter"
            #When the value is not null, the value consists of the object information about the Local Area Network Connection and
            #that the wireless connection needs to be disabled. 
            $WiFiNetadapter = get-netadapter | where-object {$_.Status -contains "Up"} | Where-Object {$_.PhysicalMediaType -eq "Native 802.11"} 
            $WiFiNetadapter | Disable-NetAdapter -Confirm:$false -Verbose }
            Start-Sleep -s 2
    }
        Start-Sleep -s 3
        }

            #Remove-Variable -Name WIRED -Force

AdapterSwitcher

当我运行这个脚本时,即使$ethernet 的值不是$null,脚本仍然会返回下面的内容,所以它不应该进入 else 块?

write-host "Disabling Network Adapter"

谁能告诉我为什么?这不合逻辑

【问题讨论】:

标签: powershell


【解决方案1】:

我不确定您的 While 循环在做什么,但您的运算符都已关闭。看看这里的区别...

    $Ethernet = Get-NetAdapter | Where-Object {($_.Status -eq "Up") -and ($_.PhysicalMediaType -eq "802.3") -and ($_.Name -ne "PANGP" )}

    if (!($Ethernet)) {

        #...do something

    }

您似乎还试图启用状态为“已启动”的 Wifi 适配器。如果它的状态是“Up”...它已经启用。试试这个...

function AdapterSwitcher {

        $Ethernet = Get-NetAdapter | Where-Object {($_.Status -eq "Up") -and ($_.PhysicalMediaType -eq "802.3") -and ($_.Name -ne "PANGP" )}

            if (!($Ethernet)) {

                $wifi = Get-NetAdapter | Where-Object {$_.PhysicalMediaType -like '*802.11*'}

                Enable-NetAdapter -Name $wifi.Name -Confirm:$false

            else {Disable-NetAdapter -Name $wifi.Name -Confirm:$false

    }

}

【讨论】:

  • 恐怕你错了,我没有启用已启动的 Wifi 适配器 - PhysicalMedia 类型是以太网的 802.3,此脚本强制机器在对接或脱离对接时切换所以你不能同时开启以太网和 Wifi。
  • 看看这一行...$WiFiNetadapter = get-netadapter | where-object {$_.Status -contains "Up"} | Where-Object {$_.PhysicalMediaType -eq "Native 802.11"} $WiFiNetadapter | Enable-NetAdapter -Confirm:$false -Verbose.... 你肯定会得到一个已经启动然后启用它的网络适配器。您在下面发布的有效功能从分配 $WiFiNetadapter 中删除了该条件。
  • 啊,是的,抱歉-检查了我发布的修订工作版本-修复了!
  • @Royston 是的,我看到你修好了。我也没有错,我鼓励你看看我们的运营商在获得 $Ethernet 方面的差异。谢谢
  • @Royston 嘿,小伙子,当我确实解决了您的问题时,发现我的答案并没有什么坏处……而且在您编辑的帖子之前一点点;)。无论如何很高兴它有效!
【解决方案2】:

这行得通:

Function AdapterSwitcher {

While ($true) {

    $Ethernet = Get-NetAdapter | where-object {$_.Status -contains "Up"} | Where-Object {$_.PhysicalMediaType -eq "802.3" -and {$_.Name -notlike "PANGP" }}
     if ($Ethernet -eq $null)
            {
            Write-Host "Ethernet Not Detected, Enabling WiFi"
                #When the value is null this means that there is no wired connection and the wireless must be enabled
                $WiFiNetadapter = get-netadapter | Where-Object {$_.PhysicalMediaType -eq "Native 802.11"} 
                $WiFiNetadapter | Enable-NetAdapter -Confirm:$false -Verbose


            }
            else
            {
            Write-Host "Disabling WiFi Network Adapter"
                #When the value is not null, the value consists of the object information about the Local Area Network Connection and
                #that the wireless connection needs to be disabled. 
                $WiFiNetadapter = get-netadapter | Where-Object {$_.PhysicalMediaType -eq "Native 802.11"} 
                $WiFiNetadapter | Disable-NetAdapter -Confirm:$false -Verbose }
                Start-Sleep -s 12
        }

            }

                #Remove-Variable -Name WIRED -Force

    AdapterSwitcher

【讨论】:

    猜你喜欢
    • 2021-11-06
    • 1970-01-01
    • 2019-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-15
    • 1970-01-01
    • 2012-05-25
    相关资源
    最近更新 更多