【发布时间】: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"
谁能告诉我为什么?这不合逻辑
【问题讨论】:
-
您的
if条件需要更改。应该是if ($null -eq $Ethernet)。如果$Ethernet始终有数据,那么else {}条件将始终为真。问题是您要对每个网络适配器进行条件评估吗?您的代码只检查一次$Ethernet集合。
标签: powershell