简体   繁体   English

如果未检测到 wifi.network 如何退出 PC?

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

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.下面的代码片段确实有效,不幸的是,当我禁用我的热点时,它仍然显示为 available.network,直到我打开和关闭我的 PC 的 wifi。 Is there a way I can refresh that list or something in powershell?有什么方法可以刷新 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.我只是看到有人可能找到了一种使用 vbs 脚本来完成它的方法,但我并没有成功地让它工作,但我会把代码留在下面供任何人修改。

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

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 ;正如codaamok在评论中提到的那样,您可以使用Get-NetAdapater ,幸运的是,它有一个显示设备网络状态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.您希望Start-Sleep具有更长的延迟,这样它就不会连续调用Get-NetAdapter导致一些 memory 消耗。 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.至于代码: while循环有一个$true条件,它将无限期地运行直到循环被中断。 After the Start-Sleep ( explained above ), a call to Get-NetAdapter is made which is then saved to $adapter .Start-Sleep如上所述)之后,将调用Get-NetAdapter ,然后将其保存到$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.最后,使用if语句,我们只检查属性Status的值是否为“ Disconnected ”,如果是,则中断循环,或者只调用 logoff.exe。

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.要检查网络适配器,您可以查看我的项目VBA.MacAddress ,它具有检索各种信息的功能,例如设备描述、IP 地址、网关和供应商,当然还有 MAC 地址及其类型。

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.因此,您可以观察到具有网关的 NIC,因为它通常是活动 NIC。 If it looses connection, the link is lost.如果它失去连接,则链接丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 2014 SQL Server连接到本地网络上的其他PC。 VBA - 2014 SQL Server Connection to different PC on local network. VBA 允许网络上的其他人在我的本地 PC 上打开工作簿 - Allow others on network to open workbook on my local PC 从网络主机(LAN连接的PC)获取操作系统信息 - Get Operating System Info from a network host (LAN connected PC) 拆分 Access DB 后 VBA 非常慢(本地 PC,而不是网络) - VBA very slow after splitting Access DB (local PC, not network) 为什么 DSN(存储在网络上)只能在我的 PC 上工作,而不能在我们网络上的其他人上工作? - Why does the DSN (which is stored on the network) only work on my PC and not for others on our network? 如何从PC-1 ping PC-2上的MS Access - How to ping MS Access on PC-2 from PC-1 宏在 PC 上运行但不在 MacOS 上运行:下标超出范围错误 - Macro runs on PC but not on MacOS: Subscript out of range error 错误:程序太长 vba:仅在 11 台电脑中的一台上 - Error: procedure too long vba: only on one pc out of 11 按下按钮注销时如何关闭所有打开的表单/报告等? - How to close all open forms/reports etc. when a button is pressed to log out? 使用'WlanScan'刷新WiFi网络列表(将api语法从c#转换为vba ...或解决方法?) - Refresh WiFi network list with 'WlanScan' (convert api syntax from c# to vba… or a workaround?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM