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 {$_.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

When i run this script, even though the value of $ethernet is not $null, the script still returns the below so it goes to the else block when it shouldn't?

write-host "Disabling Network Adapter"

Could anybody tell me why? This doesn't make logical sense

Royston
  • 433
  • 2
  • 9
  • 25
  • Your `if` condition needs to change. It should be `if ($null -eq $Ethernet)`. If `$Ethernet` always has data, then the `else {}` condition will always be true. Is the problem that you want to do the conditional evaluation for every network adapter? Your code is only checking once for the `$Ethernet` collection. – AdminOfThings Jul 17 '19 at 15:02
  • Possible duplicate of [How to test for $null array in PowerShell](https://stackoverflow.com/questions/5111572/how-to-test-for-null-array-in-powershell) – iRon Jul 17 '19 at 15:20
  • See also: https://github.com/PowerShell/PSScriptAnalyzer/issues/1021 – iRon Jul 17 '19 at 15:21

2 Answers2

1

I am not sure what your While loop is doing, but your operators are all off. Look at the differences here...

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

    if (!($Ethernet)) {

        #...do something

    }

You also appear to be trying to enable a Wifi adapter that already has a Status of 'Up'. If it's status is 'Up'...it is already enabled. Try this...

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

    }

}
m0lochwalker
  • 422
  • 3
  • 11
  • I'm afraid you're wrong, i'm not enabling a Wifi adapter that's up - the PhysicalMedia type is 802.3 which is ethernet, this script forces a machine to switch between either when docking or undocking so you can't have both EtherNet AND Wifi on at the same time. – Royston Jul 17 '19 at 15:54
  • Look a this line...$WiFiNetadapter = get-netadapter | where-object {$_.Status -contains "Up"} | Where-Object {$_.PhysicalMediaType -eq "Native 802.11"} $WiFiNetadapter | Enable-NetAdapter -Confirm:$false -Verbose.... you are definitely getting a network adapter that is already up and then enabling it. Your function that works, which you posted below, removed that criteria from assigning $WiFiNetadapter. – m0lochwalker Jul 17 '19 at 15:55
  • Ah, yeah, sorry - checked revised WORKING version i posted - fixed that! – Royston Jul 17 '19 at 15:57
  • 1
    @Royston yeah, I saw you fixed it. I also was not wrong, and I encourage you to look at the differences in our operators for getting $Ethernet as well. Thanks – m0lochwalker Jul 17 '19 at 15:58
  • Sure thing - thanks for looking at the code with me! – Royston Jul 17 '19 at 15:59
  • 1
    @Royston Hey bub, no harm in spotting me the answer as I did fix your issue...and slightly before your edited post ;). Anyway glad it works! – m0lochwalker Jul 18 '19 at 03:22
0

This works:

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
Royston
  • 433
  • 2
  • 9
  • 25