Netadapter切换器PowerShell脚本

时间:2019-07-17 13:48:18

标签: powershell

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"

有人可以告诉我为什么吗?这不合逻辑

2 个答案:

答案 0 :(得分:1)

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

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

    if (!($Ethernet)) {

        #...do something

    }

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

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

    }

}

答案 1 :(得分:0)

这有效:

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