VBS code to switch off the network

Source: Internet
Author: User

commissioned by a friend, write a VBS code to implement the power off the network to forcibly shut down the function of the computer, he said for the students in the classroom use, class always have students want to leave the teacher's surveillance, for this, will unplug the network cable or disable network card, so, get a VBS The script detects the status of the NIC and forcibly shuts down immediately if the network is disconnected.

============================ Method One ==============================

Dim Objwmiservice,objshell

Set objWMIService = Getobject ("winmgmts:\\.\root\cimv2")

Set Objshell = CreateObject ("Wscript.Shell")

' enable real-time monitoring

Do While True

Dim Objnetworks,objnetwork

Set objnetworks = objWMIService.ExecQuery ("select * from Win32_NetworkAdapter where netconnectionid= ' Local Area Connection ' ")

For each objnetwork in Objnetworks

If Objnetwork.netconnectionstatus<>2 Then

objshell.run "Shutdown-s-f-t 30-c" & Chr & " the machine is shutting down due to computer network disconnection "& Chr

' MsgBox ' Network is disconnected "

Exit For

End If

Next

Set objnetworks=nothing

' Delay Ten seconds

Wscript.Sleep 1000*10

Loop

Note: Method one is to detect the status of the network card to do the corresponding operation.

================================ Method Two ===================================

strip= "192.168.1.1"

' real-time monitoring

Do While True

Set Objshell = CreateObject ("Wscript.Shell")

If not Isonline_1 (StrIP) Then

objshell.run "Shutdown-s-f-t 30-c" & Chr & " The machine is about to close "& Chr

Wscript.Quit

End If

Wscript.Sleep 1000*10

Loop

' ========= This segment function is for reference only and is not called in the main program ===========

' through dos the way of the window Ping , however, the screen will appear black dos window , The following function will pop up every time you run dos and Color window, will soon be found.

Function IsOnline (StrComputer)

IsOnline = False

strcommand = "%comspec%/C ping-n 2-w" & StrComputer & ""

Set objexecobject = objshell.exec (strcommand)

Do and not ObjExecObject.StdOut.AtEndOfStream

StrText = ObjExecObject.StdOut.ReadAll ()

If Instr (StrText, "Reply") > 0 Then

IsOnline = True

End If

Loop

End Function

' ==================== function End ==========================

' Pure VBS make Ping action so that it does not eject dos window, will not be found.

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

Note: Method Two is through Ping one of the specified IP address, used to detect the operating status of the network card, if the network card is unable to communicate or communication failure so that cannot be returned Ping results, based on this result, determine whether the forced shutdown operation. Thus, there is an "accidental harvest" that is, when the designated IP address is disconnected, all machines will automatically shut down the computer, therefore, this program can also be used for different purposes, think of it!!!

Summary: The above two methods are in order to detect the network card running state to carry out the corresponding operation, but the implementation method is completely different, you will have to choose according to the situation!!!

This article is from the "Network Learning and Communication" blog, please be sure to keep this source http://steave.blog.51cto.com/229503/1619057

VBS code to switch off the network

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.