VBS 斷網(wǎng)后自動關機30秒后
核心代碼
Dim Wsh,objWMIService,colMonitoredEvents Set Wsh = WScript.CreateObject("WScript.Shell") Set objWMIService = GetObject("winmgmts:\. ootwmi") Set colMonitoredEvents = objWMIService.ExecNotificationQuery("Select * from MSNdis_StatusMediaDisconnect") Do While True Set strLatestEvent = colMonitoredEvents.NextEvent Wsh.run "shutdown -s -t 30 -c "&chr(34)&"系統(tǒng)網(wǎng)絡斷開,機器即將關閉"&chr(34) Loop
只要運行它后,一旦網(wǎng)線被拔掉的話,馬上就進入自動關機倒計時,“-t 30”是倒計時的時間,你可以自己調整。
下面是其他的補充
受一個朋友委托,編寫一段vbs代碼實現(xiàn)斷網(wǎng)就強行關閉計算機的功能,他說為學生機房上課時使用,上課時總有學生想脫離老師的監(jiān)視,為此,會拔掉網(wǎng)線或者禁用網(wǎng)卡,所以,弄個vbs腳本檢測網(wǎng)卡狀態(tài),如果斷網(wǎng)馬上強行關機。
代碼一
Dim objWMIService,objShell Set objWMIService = Getobject("winmgmts:\\.\root\cimv2") Set objShell = CreateObject("WScript.Shell") '實現(xiàn)實時監(jiān)測 do while true Dim objNetworks,objNetwork Set objNetworks = objWMIService.execQuery("Select * From Win32_NetworkAdapter where NetConnectionID='本地連接'") For Each objNetwork In objNetworks if objNetwork.NetConnectionStatus<>2 then 'objShell.run "shutdown -s -f -t 30 -c " & chr(34) & "由于計算機網(wǎng)絡斷開,機器即將關閉" & chr(34) msgbox "網(wǎng)絡已斷開" exit for end if Next set objNetworks=nothing '延時10秒 WScript.sleep 1000*10 Loop
注:方法一是通過檢測網(wǎng)卡的狀態(tài)來進行相應的操作。
方法二
strIP="192.168.1.1" '實時監(jiān)測 do while true Set objShell = CreateObject("WScript.Shell") If Not IsOnline_1(strIP) Then ? objShell.run "shutdown -s -f -t 30 -c " & chr(34) & "機器即將關閉" & chr(34)? wscript.quit End If wscript.sleep 1000*10 loop
=========此段函數(shù)僅供參考,不會在主程序中調用===========
通過dos窗口的方式ping,但是,屏幕上會出現(xiàn)黑色dos窗口,每運行一次下面的函數(shù)都會彈出一次dos和色窗口,很快就會被人發(fā)現(xiàn)。
Function IsOnline(strComputer) ??? IsOnline = false ??? strCommand = "%comspec% /c ping -n 2 -w 500 " & strComputer & "" ??? Set objExecObject = objShell.Exec(strCommand) ??? Do While Not objExecObject.StdOut.AtEndOfStream ???????? strText = objExecObject.StdOut.ReadAll() ???????? If Instr(strText, "Reply") > 0 Then ?????????????????? IsOnline = true ???????? End If ??? Loop End Function
純vbs進行ping操作,這樣不會彈出dos窗口,不會被發(fā)現(xiàn)。
Function IsOnline_1(URLstr) ??? IsOnline_1 = false strComputer = "." Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") Set colPings = objWMIService.ExecQuery ("Select * From Win32_PingStatus where Address = '" & URLstr & "'") ??? For Each objPing in colPings ????????? if instr(objPing.ProtocolAddress,URLstr)>0 then ?????????????????? ????? IsOnline_1=true ?????????????????? ????? exit for ???????? ? ????end if ??? Next End Function
注:方法二是通過ping一個指定的IP地址,用于檢測網(wǎng)卡的運行狀態(tài),如果網(wǎng)卡無法通訊或者通訊失敗這樣就無法返回Ping的結果,根據(jù)這個結果判斷是否進行強制關機操作。由此,還有個“意外收獲”那就是,當斷掉指定的IP地址連接后,所有機器會自動關閉計算機,因此,此段程序還可以有別的用途,自己想吧!??!
總結:以上兩種方法都是為了檢測網(wǎng)卡的運行狀態(tài)才進行相應的操作的,只是實現(xiàn)方法完全不同,這你就要根據(jù)情況自行選擇了?。?!
相關文章
iis PHP安裝腳本 PHPInstall.vbs V3.1
PHP安裝腳本,您所要做的操作是:保存這個文件與要安裝的php文件夾放一起(不要放在C盤根目錄下)2009-07-07用vbs返回 Internet Explorer 的下載控件和 Applet 的列表
用vbs返回 Internet Explorer 的下載控件和 Applet 的列表...2007-04-04用vbs腳本獲取網(wǎng)卡MAC,CPUID,硬盤序列號的實現(xiàn)代碼
這篇文章主要介紹了用vbs腳本獲取網(wǎng)卡MAC,CPUID,硬盤序列號的實現(xiàn)代碼,需要的朋友可以參考下2019-04-04