Script story-the worm crawls in, but it keeps running.

Source: Internet
Author: User
For more information about the list of all "script stories" columns, click here.

Content on this page

The worm crawls in, but does not leave.
Start script Debugger
Step-by-Step Code Execution
Last step
Set and delete breakpoints
Processing variable
Run the script command
Exclusive tips of script experts

The worm crawls in, but does not leave.

We have all heard about how the dolphins are smart, and how the gorilla can use the sign language to communicate! We even read articles about the availability of tools for birdies in the island of New Orleans. (To be honest, these birds only hold a tree branch with their mouths and try to dig out the insects in the tree. However, this is amazing, you can't always ask them to use things like a table saw or an electric drill .)

This is good enough, isn't it? We are very happy to see that there are so smart guys in the animal kingdom. Good job, I hope they will do something better one day! There is nothing wrong with signing language or using tools. However, at leastYesIn one aspect, other animals cannot compete with us, that is, we can eliminate all the species.

Indeed. What will happen when the pigeons fight? Let's talk about it again. What will happen if the ferry bird is fighting with others? The birds will surely escape! Even if the crows in the island of New Orleans can use tools to dig for bugs, what is amazing? In the end, who killed the African blue antelope? Let me remind you that it was not done by the raws of the island of New Orleans.

Clarify.Of course, script experts like to joke about it. We don't like to do anything like racism. In fact, we don't want any animals to be hurt.

Of course, it is another thing to say that the neighbor's dog keeps bark all day long. The cat sneaked around the street all day and wore a blue turban on his head as a decent one. Yes, they do.

Of course, there is at least one kind of "animal" that is not inferior to us in the fight for racism. Even though we used to fight hard, they have always been stubborn. You must be surprised, who would it be? This is computer worm. Computer Worms were first reported in 1947, but we haven't completely eliminated them until now. In fact, on the contrary, these pests are more than a day in a day. Moreover, the harm caused by these pests has also become increasingly serious. It is estimated that this notorious Millennium worm alone has caused more than $300 billion in losses to the world.

Historical story.It is said that the first computer worm is indeed a worm. In 1947, the Mark II Aiken relay calculator, one of the world's first computers, failed. After investigation, programmers found that a moth was caught in the middle of the relay, resulting in a machine short circuit (it involved in "computing "). People subsequently took the moth out and made it into the specimen in the Mark II running log. It also indicated in the log that this was the "first computer worm discovered by humans ". Believe it or not, this little worm is still treasured by the sensennan Research Institute. (Apparently, the museum has nothing to exhibit due to the death of Picasso and Da Vinci for so many years .)

We know that computer worms (or, more specifically, code errors that make scripts unable to run normally) are of great concern to system administrators. In short, we have received many questions similar to the following during the last "foot this week" network broadcast series:

"What tools can I use to debug code ?"

"Do you know which software can help me debug code and fix script problems ?"

"Where can I download the script debugger ?"

In fact,YesYou can download the script Debugger in one place, that is, Microsoft provides a (free) script debugger, but few people seem to know it. This debugger interface is a bit strange, but it does provide many fully functional development environments (such as Visual Studio) only available. If you are looking for a tool that can help you debug scripts, starting with Microsoft Script debugger is a good choice.

Note.If Microsoft has a free script debugger, why don't we make great effort to publicize it? This can help us. This is probably because Microsoft Code has no errors without exception, so we never thought about something like a debugger.

Please note that we are talking about the possibility ......

By the way, if you have purchased windows 2000YesScript debugger; it is included in Windows CD as the installation option. Windows XP or Windows Server 2003 does not provide this debugger, but you can download versions for these two operating systems and for Windows 98, Windows ME, and Windows NT 4.0.

If you are looking for a download location, try the following link:

Microsoft Windows Script Debugger for Windows NT 4.0, 2000, and XP
(Http:// Familyid = 2f%be0-94fd-4569-b3c4-dffdf19ccd99 & displaylang = EN ).

Microsoft Windows Script Debugger for Windows 98 and Windows ME
(Http:// Displaylang = ZH-CN & familyid = e606e71f-ba7f-471e-a57d-f2216d81ec3d ).

Back to Top

Start script Debugger

Microsoft Script debugger is a pretty good tool, but as we noticed, it looks a little strange. Let's talk about the issues you need to pay attention to first. After downloading and installing the script debugger, your first thought may be to start the debugger and load a script in it. Never do this. It does not work. Indeed, the debugger will start and youYesLoad the script, but there will be a problem at this time. Note that all Debugging commands are grayed out and you cannot actually perform any operation:

View the big chart.

But to be honest, ifOthersIf Microsoft software doesn't even have the most basic functions, I don't need to write so many articles! Don't worry: script debugger is actually easy to use, but it will not work when you manually start it and load the script. You need to start the script from the command line and pass the parameter // X. For example, to load the script my_script.vbs in the script debugger, you should enter a command similar to the following:

cscript my_script.vbs //x

Note.Why is the script debugger like this? We do not know. You can try to ask the dolphins. They are said to be very smart ......

In fact, the answer to this question is probably related to the fact that script debugger was initially designed for debugging ASP and HTML pages. The good news is that this debugger also applies to independent VBScript and JScript files.

It sounds crazy, but it does. After the script is started with the // X parameter, you will find that all Debugging commands can now be used:

View the big chart.

Important.Before continuing the discussion, we should point out that the script debugger tool allows you to control to a large extent how to run the script; however, it is not the perfect environment, other programs may be affected when running the script. When using script debugger, the script is actually running. In fact, if you want to view the Script output at work, you can use the alt-tab combination to switch back and forth between the debugger and the command window. If the debugged script deletes all user accounts in Active Directory, do not treat the debug session as a rehearsal or drill. When the session ends, it will delete all user accounts in Active Directory. This debugger is a troubleshooting tool, but it is not a sandbox or a virtual environment.

What should I do after I load the script to the script debugger? Although there are several options to choose from, we will focus on the following tasks:

Step-by-Step Code Execution

Set and delete breakpoints

Processing variable

Run the script command

Back to Top

Step-by-Step Code Execution

For example, Microsoft Word is an event-driven application. When you start word, it does not perform any operations; it only waits quietly for the event to occur, waiting for you to click the mouse button or press the key on the disk or executeSome operation. (Of course, if the wait time is too longAfterIt does not respond, but it is beyond the scope of this article .)

In comparison, scripts are usually process-driven: After startup, they usually run directly instead of waiting for the event to happen. After the script is started, it runs the first line of code and then (even without stopping) starts to run other lines of code. After the code line is run, the script is automatically terminated.

As long as everything works, this is a good model. However, if the execution is different from the plan, the model will be suspended for a period of time. For example, assume there is a script to complete the following tasks:

Create a text file on the local computer.

Retrieves hardware information from several servers.

Write the retrieved information to the created text file.

Copy the text file from a local computer to a remote computer.

Delete the text file from the local computer.

When you run the script, the script completes its tasks in a twinkling of an eye. Check that there are no text files on the local computer. This is normal! After all, scriptsThat isDelete a text file from a local computer. Now, check the remote computer: there are no text files. Trouble! Apparently, there is a problem, but what is the problem? What is the problem? Even if this is a relatively simple script, the script may have errors in many places. How can I know what the problem is?

Note.We assume that this script contains the on error resume next to prevent the script from crashing. But remember, even deleting on error resume next may not be able to find the exact location where the error actually occurred. Consider the following simple script:

intMyNumber = 2A = intMyNumbrB = 3C = B / A

If you run this script, an error occurs in row 4th because the divisor is 0. But actually the problem is not in the 4th rows, but in the 2nd rows, you set variable A to 0 instead of 2 in this row. This is caused by spelling errors: you assign intnumbr value to a instead of intnumber value. Because intnumbrNoSo assign a value to 0 instead of the 2 you want to allocate.

It is undeniable that this is an easy-to-find error. But the key to the problem is that the error message displayed when the script crashes only tells you which line the error is displayed; that is to say, the error in the Code actually causes an error in which line. The root cause of the error (for example, setting the variable to 0) may already exist before several hundred lines of code.

One way to deal with such problems is to use the script debugger to "execute" the Code step by step. Step-by-step execution of the code is to run the script line by line. It is undeniable that if the script is too long, it may be a lengthy and tedious task; however, we will introduce you to a solution later. On the other hand, by executing the code step by step, you can stop at each step and ensure that the script works properly.

For example, we assume that the script should first create a text file at runtime. When we run the script as is, we do not know whether a text file is created in the first place. However, by executing the code step by step, we can easily verify this point. We run the code line to create a text file and then stop it. Then, open windows Resource Manager and check whether the text file exists. If the file exists, the remaining parts of the script are executed step by step. If the file does not exist, an error is found.

So how can we execute code step by step in script debugger? It is actually very easy: load the script in the debugger and press F8. Each time you press the F8 key, the debugger will execute a line of code and jumpLowerOne line of code, and then wait for you to press F8 again. (By the way, if you do not like the keyboard, you can alsoDebugSelectStep(One-step execution )). Press F8 until the end of the script is reached. You can also run the rest of the script from the current line. Therefore, press F5 orDebugSelectRun(Run ).

Warning.Assume that the script you debug retrieves events from the event log and that there are 5,000 events in these event logs. Be careful when executing code that uses the for each loop step by step. The script runs this loop not only once, but 5,000 times for each item in the set. In this case, you may only need to run the loop once or twice (only to confirm whether the loop is running normally), and then press F5 to run the rest of the script. Alternatively, you may need to use breakpoints. We will introduce them later.

Back to Top

Last step

The last strange thing is that I have to remind you. You have run the script again and everything is normal. But be careful not to make a big mistake, right? Therefore, you can feel at ease by running the script again. No problem. However, you must exit the script debugger and re-load the script. Otherwise, problems may occur. For some reason, you can only run the script once in the script debugger. After the script is completed, You need to exit the debugger and restart it.

Yes. Don't get angry. We don't think this is a particularly good practice either. In fact, we are beginning to suspect that the user interface design of software is not so strange as the extinction of the African blue antelope. But at least we have already explained to you the strangeness of the script debugger. The poor African blue antelope will never see this day.

Back to Top

Set and delete breakpoints

Assume that your script has 1,000 lines and you have systematically executed the code step by step (by line ). Everything seemed to be normal, but an error was found in row 933rd. You stopped the debugger and corrected the script error. Now, you need to re-load the script in the debugger and run it again, but you do not want to re-execute the first 932 rows. In any case, you are very sure that these code lines have no errors. But what else do you have to choose?

If you are a crow in the island of New Orleans, you have no choice at all. As a person, you are much smarter than they are. You can take a shortcut to set a breakpoint on line 1. You may ask, what is a breakpoint? In terms of purpose, a breakpoint is like a "parking sign" inserted into the code ". If you run the script in script debugger (by pressing F5 orDebugSelectRun), The script will run until the breakpoint, and then it will "suddenly stop ". You can start step-by-step code execution from here, or you can chooseRunTo run the rest of the script.

In script debugger, you can easily identify breakpoints. As a matter of fact, they are very similar to the situation:

View the big chart.

Note:Highlighted in red and highlighted in red.

How can we set a breakpoint? Place the cursor anywhere in the target row and press the F9 key (orDebugSelectToggle breakpoint(Breakpoint switching )). To delete a breakpoint, press F9 again or press the Ctrl-Shift-F9 combination to delete all breakpoints in the script.

As you may have guessed, breakpoint is a good alternative to step-by-step code execution. Are you sure the first 932 rows of the script are correct? No problem; you only need to set a breakpoint on line 3 and run the script. The script will quickly pass through all these rows until it reaches the breakpoint and then suddenly stops. At this point, you can start to execute the rest of the code step by step.

Back to Top

Processing variable

As mentioned above, the variable is set to an error value or unexpected value, which is a common error in the script. Worse, such errors may be hard to find, especially in long scripts, where variable values may change many times. So how can we track the current value of a variable when running the script?

One way is to execute the script step by step in the script debugger and regularly query the current value of the variable from the debugger. Is there a simpler way?

Let's try it with a simple script. The following script (we save as test. vbs) assigns 2 to variable A and 3 to variable B. Then the script executes some calculations and assigns the cumulative results to variable C. The script itself is similar to the following:

A = 2B = 3C = A + BC = C * AC = C^BWscript.Echo C

If you run this script, 1000 is returned. Great, right? However, is 1000 yours?ShouldWhat is the answer? Who knows? What's worse, how do you determine whether this is the correct answer?

One thing you can do is to load the script in the script debugger, execute the code step by step, and then regularly query the debugger to understand the values of each variable. Perform the following operations: load the script in the script debugger and execute the first three lines of code step by step. The highlighted position should beC = C *The screen display should be similar to the following:

View the big chart.

So far, how is the script running? We can start the review here. We know that a is equal to 2 and B is equal to 3. We just executed the operation of equation c = a + B. In other words, c = 2 + 3, which means C should be equal to 5.WeKnow this, but ourScriptDo you know?

Okay. Let's ask. In the script debuggerViewSelectCommand window(Command window ). Now you should see a small window similar to the content on this screen:

We can interact with the script through the command window; we can ask a question to it, as we will see later, we can even use it to issue commands to the script. Use question mark first?As a command, query the current value of variable C:

? C

In other words, Type? C, And then pressEnterKey. You will immediately obtain the current value of variable C.

Good, right? Now, press F8 to execute the next line of code (C = C *). We know that C is equal to 5, while a is equal to 2. Therefore, after running this code line, we expect C to be equal to 10 (5*2 ). Therefore, let's look at the variable C'sCurrentValue:

Good guy, we can't do this for a day! Execute the next line of code (C = C ^ B). After the code is run in this line, C should be equal to the 3 power of 1000-10 (the current value of C) (because B is equal to 3), that is, 1000. What do you guess?

Is there a better way?

Back to Top

Run the script command

Therefore, compared with querying the script during script running and obtaining information about the operations executed by the scriptAre there anyWhat is a better solution? No, of course not, unless you can actually send it when the script is runningCommand. However, this is ridiculous; commands cannot be sent to a script during running. Is it true ......

Let's look at another simple little script that returns information about the additional components of Internet Explorer:

strComputer = "atl-ws-01"Set objWMIService = GetObject("winmgmts://" & strComputer _    & "/root/cimv2/Applications/MicrosoftIE")Set colIESettings = objWMIService.ExecQuery _    ("Select * from MicrosoftIE_Object")For Each strIESetting in colIESettings    Wscript.Echo "Code base: " & strIESetting.CodeBase    Wscript.Echo "Program file: " & strIESetting.ProgramFile    Wscript.Echo "Status: " & strIESetting.StatusNext

As you have written, this script connects to a remote computer (atl-ws-01) and then retrieves some information from the microsoftie_object class. It's the old one, right? We want to test this script by executing it step by step in script debugger, So we load this script and press F8 to run the first line of code that sets the value of the variable strcomputer to atl-ws-01.

But at this point we realized a problem: we suddenly remembered that the computer atl-ws-01 was not online. Because we cannot connect to the computer, the script is doomed to fail. You must assume that we have to exit the script debugger, change the script (direct it to another computer), and run the script again, right?

Error. Is there a problem with the script? The problem is that we want to connect to the computer represented by the variable strcomputer. This is fine, but the strcomputer value is currently atl-ws-01, and the computer is not online. But do you know? In factNoWhat is the problem. Before Running code lines for connecting to a remote computer, we only need to change the value of strcomputerOnlineA computer (for example, we can change strcomputer to "." To run scripts on a local computer ). Guess what to do? Yes, this time youYesIn the command window:

You only need to type the corresponding command in the Command window and pressEnterKey; sure enough,In the scriptThe strcomputer value is changed to dot (.). ThisTrueGreat!

You can also enter more complex commands. For example, consider the following script to return the names of all services installed on the local computer:

strComputer = "."Set objWMIService = GetObject("winmgmts://" & strComputer _    & "/root/cimv2")For Each objItem in colItems    Wscript.Echo "Name: " & objItem.NameNext

Great, right? One line of code is missing in the US. This line of code actually retrieves information from the win32_service class. This scriptShouldSimilar to the following:

strComputer = "."Set objWMIService = GetObject("winmgmts://" & strComputer _    & "/root/cimv2")Set colItems = objWMIService.ExecQuery _    ("Select * From Win32_Service")For Each objItem in colItems    Wscript.Echo "Name: " & objItem.NameNext

However, it does not matter. You only need to load the script in the script debugger and execute the first two lines of code step by step. Do not panic when the third line of code is missing. In the command window, type the missing line of code, and then pressEnterKey:

You only need to execute the code from this step, and the script will report the names of all services installed on the local computer, which is exactly the same as expected.

Back to Top

Exclusive tips of script experts

Now let me tell youTrueWonderful content. The command window is designed to run a line of code each time.EnterTo execute the code. This is indeed good. However, if you miss3.Line of code, and forget to add the for each loop. You will surely think: "It doesn't matter. I just need to enter the missing code line by line ." Yes, sometimes. However, when you type the missing first line of codeEnterKey, the following situation occurs:

What is the problem? The problem is that you have created a for each loop, but there is no next statement (the reason why there is no next statement is because you have not had time to type it ). Because the command window only processes a single line of code, you cannot type the next statement. An error has long been generated before you reach this line of code. It seems that you are not lucky, right?

Again. In fact, VBScript allows you to type multiple lines of code in one line, provided that you use the colon (:) to separate each line. For example, we can place the entire for each loop in a line of code (as shown below ):

For Each objItem in colItems:Wscript.Echo "Name: " & objItem.Name:Next

Yes, you want to go ahead: If you typeTheString and pressEnterTo execute all three lines of code:

Are you using these things? Probably not. But I want to reiterate that a few months ago, you will think that you will need to be related to the scriptTools?

Back to Top


As a member of humans, I do not want to admit it, but I have to say that we may never be able to get rid of computer worms. How is the California Condor? Yes, we can destroy the California Condor. But what about computer worms? Absolutely impossible; they are too many.

On the other hand, simply because the worm race cannot be eliminated is not enough to prove that we cannot track and eliminate individual bugs, especially those lurking inside our scripts. If we can use tools such as Microsoft Script debugger, what are we waiting? In short, if computer worms can use these tools to deal with us, the consequences would be disastrous ...... Try the script debugger and tell us how you feel.

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: 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.