Windows 7 Powershell LAN/WLAN Switcher

Discussion in 'Programming and Scripting' started by Neemobeer, May 10, 2016.

  1. Neemobeer

    Neemobeer Windows Forum Team
    Staff Member

    Joined:
    Jul 4, 2015
    Messages:
    2,380
    Likes Received:
    360
    Many times people may have both their wireless NIC and wired NIC connected at the same time. This can create a bridge and cause your network performance to degrade. Here is a powershell script that could be setup as a scheduled task to run at start up. What it does it detect if the wired nic is connected and if so it will disable the wireless connections. The -Filter parameters may need to be tweaked to match your network adapter names though.

    Code:
    Function Get-WiredStatus
    {
        $nics = Get-WmiObject -Class Win32_NetworkAdapter -Filter "Name LIKE '%Ethernet%'"
        ForEach ($nic In $nics)
        {
            If ($nic.NetConnectionStatus -eq 2)
            {
                Disable-WirelessConnection
                break
            }
            Else
            {
                Enable-WirelessConnection
                break
            }
        }
    }
    
    Function Disable-WirelessConnection
    {
        $nics = Get-WmiObject -Class Win32_NetworkAdapter -Filter "Name LIKE '%Wireless%'"
        ForEach ($nic In $nics)
        {
            $nic.Disable() >> $null
        }
        $nics.Dispose()  
    }
    
    Function Enable-WirelessConnection
    {
        $nics = Get-WmiObject -Class Win32_NetworkAdapter -Filter "Name LIKE '%Wireless%'"
        ForEach ($nic In $nics)
        {
            $nic.Enable() >> $null
        }  
        $nics.Dispose()
    }
    
    While($true)
    {
        Get-WiredStatus
        Start-Sleep -Seconds 5
    }
     
  2. holdum333

    holdum333 Banned

    Joined:
    Mar 27, 2016
    Messages:
    1,244
    Likes Received:
    147
    Hi @Neemobeer Very good theory; but I must confess, way over this old country boys pay grade.
    I'm sure we have some members here that can use this switcher.;):worship:
     
  3. AceInfinity

    AceInfinity Senior Member
    Microsoft MVP

    Joined:
    Aug 12, 2011
    Messages:
    159
    Likes Received:
    11
    I don't think that the for loop is necessary if both code path's break from it in the first iteration of your first function. Not a bad idea though, but sometimes I like to have multiple networks
     
  4. Neemobeer

    Neemobeer Windows Forum Team
    Staff Member

    Joined:
    Jul 4, 2015
    Messages:
    2,380
    Likes Received:
    360
    The foreach loop is needed to iterate through all the adapters
     
  5. AceInfinity

    AceInfinity Senior Member
    Microsoft MVP

    Joined:
    Aug 12, 2011
    Messages:
    159
    Likes Received:
    11
    Code:
    ForEach ($nic In $nics)
    {
        If ($nic.NetConnectionStatus -eq 2)
        {
            Disable-WirelessConnection
            break  <----
        }
        Else
        {
            Enable-WirelessConnection
            break <----
        }
    }
    It won't iterate through all of them though because of the break statements. Look at your logic. The if statement tests for a specific condition and if true, breaks, otherwise if the first iteration evaluation is false, the else block still breaks from the foreach loop.

    Try this:
    Code:
    foreach ($num in 1,2,3,4,5) {
      write-output $num
      break
     }
     
  6. Neemobeer

    Neemobeer Windows Forum Team
    Staff Member

    Joined:
    Jul 4, 2015
    Messages:
    2,380
    Likes Received:
    360
    This is old code anyways it doesn't work like this anymore
     

Share This Page

Loading...