Flexible and practical VBS introductory tutorial application

Time:2021-7-24

In the last article, we learned some basic knowledge of VBS programming. We should learn more about selection structure and loop structure.

Select structure

Select a structure, that is, judge the conditions, and then select a structure to execute different branches according to the judgment results. Common are if conditional statements and select   Case statement.

● if conditional statement
If conditional statements have many specific forms, such as if… Then, if… Then… Else, if… Then… Elseif. The following are explained by routines. Write a vbs script with the following contents in the notebook:

  dim a,b
A = InputBox (“enter the first number”, “enter”)
B = InputBox (“enter the second number”, “enter”)
  if a>b then
Msgbox (“the first number is greater than the second number”)
  End if

After running, the user is required to input two numbers. The value of the first number is assigned to a and the value of the second number is assigned to B. then the program automatically judges a and B. if the value of a is greater than the value of B, an information prompt box pops up and displays “the first number is greater than the second number”. Note: if the statement after then is not on the same line as then, add an end line after the judgment statement   If to end, otherwise the program will end. If it is on the same line, you don’t need to add end   If, for example, the above statement can be changed to:
  dim a,b
A = InputBox (“enter the first number”, “enter”)
B = InputBox (“enter the second number”, “enter”)
  if   a>b   then   Msgbox (“the first number is greater than the second number”)
In addition, multiple if statements can be used in another program. We can improve the above program as follows:
  dim a,b
A = InputBox (“enter the first number”, “enter”)
B = InputBox (“enter the second number”, “enter”)
  If a>b then
Msgbox (“the first number is greater than the second number”)
  End if
  If a=b then
Msgbox (“the first number equals the second number”)
  End if
  If a<b then
Msgbox (“the first number is less than the second number”)
  End if
We can also use other if statements if… Then… Else and if… Then… Else if to achieve the same effect, as follows:
  dim a,b
A = InputBox (“enter the first number”, “enter”)
B = InputBox (“enter the second number”, “enter”)
  If a>b then
Msgbox (“the first number is greater than the second number”)
  Else
  IF a=b then
Msgbox (“the first number equals the second number”)
  Else 
Msgbox (“the first number is less than the second number”)
  End if
  End if
  ――――――――――――――――――――――――――――――――――――――
  dim a,b
A = InputBox (“enter the first number”, “enter”)
B = InputBox (“enter the second number”, “enter”)
  If a>b then
Msgbox (“the first number is greater than the second number”)
  Elseif a=b then
Msgbox (“the first number equals the second number”)
  Else
Msgbox (“the first number is less than the second number”)
  End if
  
  ●Select   Case statement
  Select   Case statement is another representation of multi branch structure. It has the characteristics of intuitive representation, simple structure and not easy to cause confusion. Its syntax form is as follows:
  Select   Case   Variable or expression
  Case   Expression 1
Statement 1
  Case   Expression 2
Statement 2
  End Select

Now we use Select   Use the case statement to write a program that converts numbers into English and experience select   Advantages of case, let’s   go!!!

  dim a
A = InputBox (“enter a number”, “enter”)
  Select Case a
   Case 1 
     Msgbox (“the number you entered is one in English”)
   Case 2
     Msgbox (“the number you entered is two in English”)
   Case 3
     Msgbox (“the number you entered is tree in English”)
   Case 4
     Msgbox (“the number you entered is four in English”)
  End Select

From above, don’t you think   The case statement is very intuitive. If you use the if statement, it will be very chaotic and even make people dizzy. It should also be noted that no matter how many branches the conditional statement has, as long as one branch is executed, the other branches will not be executed.
  
Cyclic structure

● for loop statement
The for statement is used to control the loop structure with known loop times. The statement is as follows:
   For   Cyclic variable = initial value   To   final value  [ Step size]
Statement
  [Exit   for]  ‘ Exit loop statement
  Next
Now let’s write a program to calculate the sum of integers from 1 to 100 to deeply understand the usage of for. The content of the program is as follows:

  dim s
  s=0
  for i=1 to 100
  s=s+i
  next
  msgbox(s)

This program is also one of the classic programs. It first declares a variable s, and then assigns an initial value of 0, for to s   i=1   to   The 100 statement allows I to add 1 with each cycle, and the statements s = S + I in the loop body are accumulated with the increase of I until I = 100 to end the cycle. The next statement moves the loop body to the next loop.
  
● do… Loop loop statement
The for loop statement is used to control loops with known number of loops, while do… Loop is used to control loops with unknown number of loops and control loops according to set conditions. It has two forms, one is to judge before execution, and the other is to execute before judgment.
Judge before execute syntax form:
  Do  [ while/until   Conditions]
Statement
  [Exit   Do]  ‘ Exit loop
  Loop
  
Execute before Judge syntax form:
  Do
     sentence
  [Exit Do]
  Loop  [ while/until   Conditions]
Next, we use the do… Loop statement to rewrite the above program for calculating the sum of integers from 1 to 100, as follows:

Execute before Judge
  dim s
  s=0
  i=0
  do
  i=i+1
  s=s+i
  loop until i>=100
  msgbox(s)

Judgment before execution:
  dim s
  s=0
  i=0
  do while s<100
  i=i+1
  s=s+i
  loop
  msgbox(s)

These two procedures are very simple and easy to understand. I won’t repeat them here.
So far, the important syntax structure and sentences of VBS have been roughly studied. If you want to study more deeply, carefully and systematically, you’d better look for relevant books. The following will officially carry out the actual combat stage of VBS programming Internet cafe.

Actual combat chapter

Before “actual combat”, let’s understand what shell objects and WSH are. Shell object is Microsoft’s encapsulation of some common shell operation functions. Such as opening or browsing a folder, finding files, turning off the computer, etc., but also browsing the entire file system and obtaining various information of files or folders. Because these packages are based on OLE automation services, we can easily use them in VB, web and VBS. WSH is windows   Script   WSH, the abbreviation of host, is a script instruction of Windows platform. Its function is very powerful, and it uses JS and vbs script language to realize its excellent functions. In addition to modifying the registry, it can also access Excel files and communicate with the network. Of course, its greatest advantage is that it can communicate with the operating system, Modifying the registry is just the tip of the iceberg for it to communicate with the operating system. It has so many advantages and practicability that it is favored by many windows users.

As mentioned above, WSH can modify the system registry using VBS, but it must first create an object that can communicate with the operating system, and then use various methods of the object to operate the registry. The method and format of creating this object are as follows:

  Dim OperateReg
  Set OperateReg=WScript.CreateObject(“WScript.Shell”)
The first line above declares a variable, and the second line uses the declared variable to create a shell object operatereg that can communicate with the operating system.

Let’s use shell objects to close some services that will bring security risks to Internet cafes.

Turn off default sharing
  set ds=createobject(“wscript.shell”)
  ds.run “net stop server /y”

Shutdown WMI service
  set swmi=createobject(“wscript.shell”)
  swmi.run “net stop wmI /y”

Disable guess user
  set sg=createobject(“wscript.shell”)
  sg.run “net user guest /active:NO”

Turn off telnet service
  set st=createobject(“wscript.shell”)
  st.run “net stop telnet”
  
Using vbs   Realize boot bat delay and no black screen
If we boot up, we want to execute a batch file named bar.bat on the remote server \ \ server \ new\   Then create a batch under the root directory of Disk C of the client, and name it run.bat, as follows:
  ping   127.0.0.1  – n   10   ( 10 indicates the number of seconds you want to delay   (set it as needed)
  call \\server\new\bar.bat
  
Save and write a text in the same directory  
  set nb=createobject(“wscript.shell”)
  nb.run “run.bat /start”,0
Save it as a VBS file, then add this file to the startup item, restart bat, delay and no black screen.

The above are just some simple programming of VBS. If you can master VBS, you will find that its function is very powerful, and many complex functions can be realized through it. Now many network managers are happy to use VBS programming to manage the machines of Internet cafes. The three articles in this series are only a simple understanding and example application of VBS. If you want to learn more deeply, you have to rely on your usual efforts and accumulation. Finally, I wish you can learn VBS well as soon as possible.

Recommended Today

VBS obtains the operating system and its version number

VBS obtains the operating system and its version number ? 1 2 3 4 5 6 7 8 9 10 11 12 ‘************************************** ‘*by r05e ‘* operating system and its version number ‘************************************** strComputer = “.” Set objWMIService = GetObject(“winmgmts:” _  & “{impersonationLevel=impersonate}!\\” & strComputer & “\root\cimv2”) Set colOperatingSystems = objWMIService.ExecQuery _  (“Select * from […]