How to log out of PC if wifi network is not detected?

131 views Asked by At

I have an idea for a script I am trying to write. Essentially I want my computer to logoff if it does not detect the hotspot from my phone. If I were to walk away from my computer I'd want it to automatically log off if I got too far away. The code snippet down below does work, unfortunately when I disable my hotspot it still shows up as an available network until I turn my PC's wifi on and off. Is there a way I can refresh that list or something in powershell? Any other potential ideas to make this work?

try {
    $SSID = "Phone"
    $Network = (netsh wlan show networks mode=Bssid | ?{$_ -like "SSID*$SSID"}).split(':')[1].trim()
    if ($Network) { Write-Host "The SSID is detected" }
}
catch {
    shutdown -L
}

I did just see that someone potentially found a way to do it wuth a vbs script but I have not been succseful in making it work, but I'll leave the code down below for anyone to tinker with.

Sub ClickIt()
With CreateObject("WScript.Shell")
    .Run "%windir%\explorer.exe ms-availablenetworks:"
End With
End Sub
2

There are 2 answers

4
Abraham Zinala On

As codaamok mentions in the comments, you can use Get-NetAdapater which, lucky for us, has a Status property that shows the devices Network Status; so, if it's on it will show "connected", and when off it shows "disconnected".

while ($true) {
    Start-Sleep -Seconds 10
    $adapter = Get-NetAdapter -Name "Wi-Fi 2" 
        if ($adapter.Status -eq "Disconnected") {
            Write-Output -InputObject ($adapter.Name + " " + $adapter.Status)
            break #Or just invoke logoff.exe
            #logoff
        }
}

You want that Start-Sleep with a preferably longer delay so it doesn't continuously make a call to Get-NetAdapter leading to some memory consumption. Honestly, you may want this in a Scheduled Task instead which is the route I would take here.

As for the code: The while loop has a condition of $true that will make it run indefinitely until the loop is broken out of. After the Start-Sleep (explained above), a call to Get-NetAdapter is made which is then saved to $adapter. Finally, using an if statement, we just check to see if the property Status has a value of "Disconnected" and if so, break the loop, or just invoke logoff.exe.

0
Gustav On

To check for the network adapters, you can look up my project VBA.MacAddress which holds functions to retrieve all sort of information, like device descriptions, IP addresses, gateway, and vendors, and, of course, MAC addresses and their types.

The demo module shows some practical implementations, for example:

' Lists general information for each of the network adapters of the local computer.
'
' Example:
'   MAC address   IP Enabled    Has gateway   IP address       Description
'   4437E68218AB  True          True          192.168.100.26   Hyper-V Virtual Ethernet Adapter
'   00155D011500  True          False         169.254.80.80    Hyper-V Virtual Ethernet Adapter #2
'   00155D4DB442  True          False         192.168.96.211   Hyper-V Virtual Ethernet Adapter #3
'   4437E68218AB  False         False                          Intel(R) 82579LM Gigabit Network Connection
'   E0FB20524153  False         False                          WAN Miniport (IP)
'   E0FB20524153  False         False                          WAN Miniport (IPv6)
'   E45E20524153  False         False                          WAN Miniport (Network Monitor)
'
' 2019-09-21, Cactus Data ApS, Gustav Brock
'
Public Sub ListLocalMacAddressesInfo()

    Const IpAddressWidth    As Long = 17

    Dim MacAddresses()      As Variant
    Dim Index               As Long
    Dim NicInformation      As IpNicInformation
    Dim Octets()            As Byte
    
    ' Retrieve the MAC addresses.
    MacAddresses = GetMacAddresses()
    
    ' Print a header line.
    Debug.Print "MAC address", "IP Enabled", "Has gateway", "IP address       Description"
    ' Loop the adapters.
    For Index = LBound(MacAddresses, RowDimension) To UBound(MacAddresses, RowDimension)
        For NicInformation = IpNicInformation.[_First] To IpNicInformation.[_Last]
            Select Case NicInformation
                Case IpNicInformation.ipNicMacAddress
                    Octets() = MacAddresses(Index, NicInformation)
                    Debug.Print FormatMacAddress(Octets()), ;
                Case IpNicInformation.ipNicIpAddress
                    Debug.Print Left(MacAddresses(Index, NicInformation) & Space(IpAddressWidth), IpAddressWidth);
                Case Else
                    Debug.Print MacAddresses(Index, NicInformation), ;
            End Select
        Next
        Debug.Print
    Next

End Sub

Thus, you could observe the NIC having a gateway, as that would typically be the active NIC. If it looses connection, the link is lost.