Netadapter交换机PowerShell脚本
当我运行这个脚本时,即使Netadapter交换机PowerShell脚本,powershell,Powershell,当我运行这个脚本时,即使$ethernet的值不是$null,脚本仍然会返回下面的值,因此它会在不应该返回的时候转到else块 Function AdapterSwitcher { While ($true) { $Ethernet = Get-NetAdapter | where-object {$_.Status -contains "Up"} | Where-Object {$_.PhysicalMediaType -eq "802.3" -and {$_.Name -notlike
$ethernet
的值不是$null
,脚本仍然会返回下面的值,因此它会在不应该返回的时候转到else块
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
有人能告诉我为什么吗?这没有逻辑意义我不确定While循环在做什么,但是操作符都关闭了。看看这里的区别
write-host "Disabling Network Adapter"
您似乎还试图启用已处于“启动”状态的Wifi适配器。如果它的状态为“启动”…它已启用。试试这个
$Ethernet = Get-NetAdapter | Where-Object {($_.Status -eq "Up") -and ($_.PhysicalMediaType -eq "802.3") -and ($_.Name -ne "PANGP" )}
if (!($Ethernet)) {
#...do something
}
这项工作:
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
}
}
如果情况需要更改,则您的
。如果($null-eq$Ethernet)
,则应为。如果$Ethernet
始终具有数据,则else{}
条件将始终为真。问题是您希望对每个网络适配器执行条件求值吗?您的代码只检查了一次$Ethernet
集合。请参阅的可能重复:恐怕您错了,我没有启用启动的Wifi适配器-PhysicalMedia类型是802.3,即Ethernet,此脚本强制计算机在停靠或取消停靠时进行切换,以便您不能同时打开以太网和Wifi。请看这一行…$WiFiNetadapter=get netadapter |其中对象{$\状态-包含“Up”}其中对象{$\物理媒体类型-eq“Native 802.11”}$WiFiNetadapter |启用NetAdapter-确认:$false-详细。。。。您肯定会得到一个已经启动的网络适配器,然后启用它。你在下面发布的有效函数从分配$WiFiNetadapter中删除了该条件。啊,是的,抱歉-检查了我发布的修订工作版本-修复了该问题@罗伊斯顿是的,我看见你修好了。我也没有错,我鼓励大家也看看我们运营商在获得$Ethernet方面的差异。Thanks@Royston嘿,小家伙,在我解决你的问题的时候,在你编辑的帖子之前,帮我找到答案,这没什么害处。不管怎样,很高兴它起作用了!
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