(Reproduced) The story of the script - the bug climbed, but it didn't take it.

xiaoxiao2021-03-06  71

Script story

For a list and other information about all "Script Story" columns, click here.

Transfer from: http://www.microsoft.com/china/technet/community/columns/scripts/sg0904.mspx This page

The bug climbed in, but it reluctantly launched the Script Debugger step-by-step execution code last set and deleted breakpoint processing variables run the script command script expert's scholastic secret

The bug climbed in, but it reluctantly

We have all heard how the dolphins are smart, and the gorilla can communicate with the sign language! We even read the article about the Tools for Birds on New Caledonia. (In truth, these birds just use a branch with a mouth, and try to dig out the bug in the tree; despite this, this is amazing, you can't let them use the table saw or electric drill. Stuff.)

This is good enough, isn't it? We are very happy to see that there is such a smart guy in the animal kingdom; it is good, I hope they will do something more colorful one day! There is nothing wrong with gentle or use tools. However, at least one other animals can not be able to fight with our human trial, that is, we can eliminate the entire species.

indeed so. What is the result of the game? Then use it, raise the two-claw surrender. What is the result? The little bird will fall and escape! Even if the Corge of New Caledonia can use the tool to dig a saucer, then there is a great thing; say, who is the African blue antelope to kill? Give you a wake up: This is true that the new Caledonian crow is dry.

Clarify. Of course, the script experts are like joking, we don't like what hangs such as genocide; in fact, we do not want any animals to be hurt. Of course, the neighboring home will not stop the dull dog. There is also the cat in the street all day, and a blue hooded is still wearing a blue head like an image. Cough, they are in this virtue.

Of course, at least one "animal" is poor than us, even though we have a strong embarrassment, you must be very strange, who is it? This is the computer insect. For the first time, the computer worm was in 1947, but until now, we have not completely destroyed; in fact, it is the opposite, these pests are more than one day. Moreover, the hazards produced by these pests are also more serious; it is estimated that the unscrupulous millennium insects have caused more than $ 300 billion in losses to the world.

Historical Events. It is said that the first computer insect is indeed a bug. In 1947, a Mark II Aiken relay calculator in the world's earliest computer has failed. After investigation, the programmer found that there is a moth clip in the middle of the relay, thus causing machine short circuit (it participated in "calculation"). People then took out the moth and made a specimen clip in the Mark II operation log, and it was also indicated in the log. This is "the first case of computer insects found." Letter do not believe that you, the Schomisen South Institute has also collected this small bug. (Obviously, because Mi Kai Langquiro and Da Vinci have been died for so many years, there is no thing for the museum to be available.

We know that the computer worm (perhaps the more precise statement is that the code error that makes the script cannot run normally) is a question that the system administrator is very concerned. In short, during the recent "Script Week" network broadcast series, we have received a lot of questions like the following:

"What tool 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 from?"

In fact, there is a place to download the script debugger, that is, Microsoft.com. Microsoft offers (free) script debuggers, but it seems to be nothing to know; this debugger interface has a little strange, but it does provide a lot of functions that are talented in a functional development environment (such as Visual Studio). If you are looking for tools that can help you debug scripts, you are a nice choice from Microsoft Script Debugger.

note. If Microsoft has a free script debugger, why don't we spend great effort? This can ask us. This is likely because Microsoft code has no error in exceptions, so we never want to have something such as debugging.

Please note that we are very likely ...

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

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

• For Windows NT 4.0,2000 and XP, Microsoft Windows Script Debugger (http://www.microsoft.com/downloads/details.aspx?FamilyID=2f465be0-94fd-4569-b3c4-dffdf19ccd99&DisplayLang=en). • For Windows 98 and Windows Me Microsoft Windows Script Debugger (http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=e606e71f-ba7f-471e-a57d-f2216d81ec3d).

Back to top

Start Script Debugger

Microsoft Script Debugger is a very good gadget, but as we noticed, it looks a bit weird. Let's talk about what problems you have to pay attention to. After downloading and installing Script Debugger, your first idea may be a startup debugger and load a script. Don't do this, this is not used. Indeed, the debugger will start and you can load the script, but there will be a little problem at this time; note that all debug commands are gray, you can't act if any action can be performed:

View larger image.

But really, if there is no other Microsoft software, I don't have to write this manuscript! Don't worry: Script debugger is actually easy to use, just when you start it and load the script, it will not work. You need to start the script from the command line and transfer parameters // x. For example, to load script my_script.vbs in Script Debugger, you should type the following command:

Cscript my_script.vbs // x

note. Why is SCRIPT Debugger? We don't know. You can try to ask the dolphins, it is said that they are very smart ... In fact, the answer to this question is likely to be related to the following facts: Script debugger is originally designed for debugging ASP and HTML. Another good news is that this debug is also suitable for separate VBScript and JScript files. It is undeniable that this sounds a bit ridiculous, but this method is effective. After using the // x parameter to start the script, you will find that all debug commands can now be used:

View larger image.

important. Before continuing to discuss, we should point out that the Script Debugger tool allows you to control how to run scripts to a large extent; however, it is not the perfect environment, which may affect other programs when running scripts. When using Script debugger, the script is actually running; If you debug scripts delete all user accounts in Active Directory, don't use debug sessions as a rehearsal or exercise; at the end of the session, it will delete all user accounts in Active Directory. The debugger is a troubleshooting tool, but it is not a sandbox or a virtual environment.

So what should I do after loading the script into the script debugger? Although there are several options to choose, we focus on the following tasks:

• Scycode execute code • Setup and delete breakpoints • Processing variables • Run the script command

Back to top

Step-by-step execution code

For example, Microsoft Word is an event-driven application. When you start Word, it does not perform any action; it is just quietly waiting for the incident, waiting for you to click the mouse button or press the key on the keyboard or perform some action. (Of course, if the wait time is too long, even after you perform some kind of operation, it will not make any response, but this has exceeded the scope discussed in this article.)

In contrast, the script is usually driven: after startup, they usually do not wait for the event, but run directly. After the script is started, it runs the first line of code, then (even without stopping "gasping") starts running the rest of the code line, the whole process is one. After running the code line, the script will automatically terminate.

As long as everything is normal, this is a good model. However, this model will have stopped for a while if the implementation is incompletely consistent with the plan. For example, assume that there is a script for complete the following tasks:

• Create a text file on your local computer. • Retrieve hardware information from several servers. • Write the retrieved information into the created text file. • Copy the text file from the local computer to the remote computer. • Remove the text file from your local computer.

You run the script, a blink of an eye, the script completes its task. You check the local computer, no text files. This is normal! After all, the script is to delete text files from the local computer. Now check the remote computer: there is no text file. Trouble! Obviously, there have been problems, but what problems have occurred, where is the problem? Even this is a relatively simple script, the script may also have an error in many places. How can I know where the problem is?

note. We assume that this script contains an ON Error Resume next to prevent script crash. But to remember, even if you delete the ON Error Resume next, you don't necessarily find the exact location that actually errors. Consider the following simple script: intMynumber = 2A = INTMYNUMBR

B = 3

C = B / a

If you run this script, there is an error in the fourth line, because the divisor is 0. However, the problem is not in the fourth line, and the problem is actually in line 2. You set the variable A to 0 instead of 2 in this line. This is due to spelling errors: you assign intNumbr rather than the value of INTNumber. Because INTNUMBR does not have a value, the A is assigned to 0, not the 2 you want to assign. It is undeniable that this is a very easy to find a mistake. But the key to the problem is that the error message displayed when the script crashes only tells you where the error is displayed; that is, the error in the code actually triggered an error. Error's root cause (if set to 0) may exist before hundreds of lines of code.

One way to handle such a problem is to use Script Debugger to "step by step" code. Step-by-step execution code is run by line. It is undeniable that if the script is very long, this may be a lengthy and boring job; however, we will introduce you to a solution. On the other hand, by performing code step by step, you can stop at each step and make sure the script works normally.

For example, we assume that the script first should first create a text file. When we run the script as it is, we don't know if we have created a text file in the first place. But by following step-by-step execution, we can easily verify this. We run the code rows that will create a text file and stop. Subsequently, we open the Windows Explorer and check if the text file exists. If the file exists, the rest of the script is processed step by step. If the file does not exist, we have found an error.

So how do you step by step in Script debugger? In fact this is very easy: load the script in the debugger, then press the F8 key. Each time you press the F8 key, the debugger will perform a row code, jump to the next line of code, then wait for you to press the F8 key again. (By the way, if you don't like to use the keyboard, you can also select STEP INTO from the Debug menu). Press the F8 button until the end of the script is reached. You can also start the rest of the script from the current row. To do this, press the F5 key or from the Debug menu to select Run.

caveat. Assume that the script you debug is retrieved from the event log and 5,000 events in these event logs. Be careful when performing code using the for Each loop; the script does not run the loop once, it will run 5,000 times, run once separately for each item in the collection. For such cases, you may only need to run cyclic or twice (just if you confirm if the loop is running is normal), then press the F5 key to run the rest of the script. Or, you may need to use breakpoints, we will introduce them later.

Back to top

last step

There is also the last strange thing, I have to remind you. You have already run a script and everything is normal. But be careful not to make a big mistake, right? So you want to run a script again, so you will be relieved. no problem. But you must exit Script debugger and reload the script, otherwise you may have problems. For some reason, you can only run the script once in Script Debugger; after completion, you will need to exit the debugger and restart. I know, I know. Don't be angry, we don't think this is a particularly good practice; in fact, we have begun to doubt, in fact, human extinction of African blue anteloers, is not as good as the software user interface design. But at least we have explained you to the weirdness of Script debugger; poor African blue antelope sheep is afraid that it will never see this day.

Back to top

Setting and deleting breakpoints

Assume that your script has 1,000 lines, and you execute the code in step by step (progressive execution). Everything seems to be normal, but a mistake is found in line 933. You stop the debugger and correct the script error. Now, you need to load scripts in the debugger and run again, but you don't want to re-execute the top 932 lines. Anyway, you are very sure that these codes have no mistakes. But do you have any options in addition to going over?

If you are the crow of New Caledonia, there is no choice. As a person, you have more smart than they, you can take shortcuts, that is, set a breakpoint on line 933. You may ask, what is a breakpoint? In terms of use, breakpoints are like "parking signs" inserted into the code. If you run a script in Script Debugger (by pressing the F5 key or from the Debug menu), the script will run until the breakpoint, and it will "stop". You can start step-by-step execution from this point, or you can select RUN to run the rest of the script.

In Script Debugger, you can recognize the breakpoint very easily. In fact, they are very similar to the situation in the figure below:

View larger image.

Note the contents of the content and similar parking sign with red highlights.

How do you set breakpoints? Simply place the cursor anywhere in the target row, then press the F9 key (or select Toggle Breakpoint from the Debug menu). To delete a breakpoint, press the F9 key again, or press the Ctrl-Shift-F9 combination button to delete all breakpoints in the script.

You may have already guessed, breakpoint is a good alternative to step by step. Are you sure that the first 932 line of the script is correct? No problem; then set up a breakpoint on page 933 and run the script. The script will quickly pass all these rows until you reach the breakpoint; then suddenly stop. At this point, you can start to perform the rest of the code.

Back to top

Treatment variable

As mentioned earlier, the variable is set to the error value or an unexpected value is an error that often appears in the script. Worse, such errors may be difficult to find, especially in longer scripts, where variable values ​​may change many times. How can I track the current value of the variable when running the script?

One way is to perform scripts in Script Debugger and regularly query the current value of the variable to the debugger. Is there a simpler way?

Let us use a very simple script to try it. The following script (we save to Test.vbs) is assigned to the variable A and assign a value of the variable B3. The script then performs some calculations and assigns these calculated cumulative results to the variable C. The script itself is similar to the following: a = 2

B = 3

C = a b

C = c * a

C = c ^ b

WScript.echo C

If you run this script, the returned answer should be 1000. Good, right? However, is 1000 to the end, is it what you should get? Who knows? Worse, how can you start to determine if this is the correct answer?

One thing you can do is load scripts in Script Debugger, step on code, and then query the debugger to the debugger to find the value of each variable. Do the following: The script is loaded in the Script Debugger and perform the first three lines of code in step by step. Highlighting the location should be c = c * a, the screen display should similar to the following:

View larger image.

So far, what is our script operation? We can start review from here. We know that A is equal to 2, b is equal to 3, we just executed the operation of equation C = A B. In other words, C = 2 3, this is to say that C should be equal to 5. We know this, but do our scripts know?

Ok, let's ask it. In Script Debugger, select Command Window from the View menu. You should now see a small window similar to this screen:

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

? C

In other words, type? C in the command window, then press Enter; you will immediately get the current value of the variable C.

Not bad right? Now, press the F8 key to perform the next line of code (c = c * a). We know that C is equal to 5, and A is equal to 2; therefore, after running this code, we expect C should be equal to 10 (5 * 2). So let's take a look at the current value of the variable C using the command window:

Good guy, we can't do this! Perform the next line of code (C = C ^ b). After this code line runs, C should be equal to 3 power of 1000 - 10 (C 's current value) (because B is equal to 3) is 1000. You guess?

Is there a better way?

Back to top

Run the script command

Then, is there a better way to query the script and get information on the operations of the script when you are running on the script? No, of course, there is no unless you can send a command to it at the script. But this is very ridiculous; you cannot send a command to it when the script is running. Is it really possible ...

Let us look at another simple small script that returns information about Internet Explorer attached components:

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 coliesttings

WScript.echo "Code Base:" & striesetting.codebase

WScript.echo "Program File:" & striesetting.programfile

WScript.echo "status:" & striesetting.status

NEXT

As written, this script is connected to the remote computer (ATL-WS-01) and then retrieves some information from the Microsoftie_Object class. Another old set, is it? We want to test this script by following the Script Debugger, so we load this script, press the F8 key to run the first line of code, which sets the value of the variable StrComputer to ATL-WS-01.

However, at this time we realize that there is a problem: We suddenly remember that the computer ATL-WS-01 is not online. Because we cannot connect to the computer, the script is destined to fail. You must think that we must exit Script debugger, change the script (pointing it to another computer), then re-run the script, right?

wrong. Is there a problem with the script? The problem is that there is a computer that we want to connect to the variable StrComputer. There is no problem, just the value of StrComputer is currently ATL-WS-01, and the computer is not online. But do you know? In fact, this is not a problem. Before we run the code line for connecting to the remote computer, we only need to change the value of the strComputer to a computer that is online (for example, we can change strComputer to "." To run scripts for local computer) . What should you guess? Yes, this time you are right, we are doing this in the command window:

Simply type the corresponding command in the command window and press Enter; if it is unaffir, the value of the StrComputer in the script will change to a dot (.). This is really great!

You can also enter a more complex command. For example, consider the following script, which is used to return to all services installed on your local computer:

StrComputer = "."

Set objwmiservice = getObject ("WinMgmts: //" & strComputer_

& "ROOT / CIMV2")

For Each Objitem in Colitems

Wscript.echo "name:" & objitem.name

NEXT

Very good, right? The lack of one line of code is lacking, and the line code actually retrieves information from the Win32_Service class. This script should be similar 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.name

NEXT

However, there is no relationship. Simply load the script in the Script Debugger and perform the first two lines of code in step by step. When you arrive at the missing third line of code, don't panic; type the missing code rows in the command window, then press Enter:

Simply execute the code from this point, the script will report the name of all services installed on the local computer, which is the same as the expected.

Back to top

Script expert's school secret

Now tell you a real wonderful content. The command window is designed to run a row of code each time; you type command, press Enter to execute the line code. This is really good. But if you miss three lines of code and forget to add a for Each loop. You will think: "There is no relationship, I don't have the lack of code," I am in some time. " But here, when you type a missing first line of code, then press ENTER, the following:

What is wrong? The problem is where you have created a for Each loop, but there is no NEXT statement (whose NEXT statement is not because you haven't been to type it). Because the command window only handles a single line code, you can't type the next statement; before you reach the line code, you have had an error. It seems that you are not lucky, right?

wrong again. In fact, VBScript allows you to type multi-line code in a row, provided you use a colon (:) to separate each line. For example, we can loop the entire FOR Each in a row code (as shown below):

For Each Objitem in Colitems: wscript.echo "name:" & objitem.name:next

Worth, you think we have gone in front: If you type the string in the command window and press Enter, you will do all three lines of code:

Will you use these things? May not. But I have to reiterate that you would like to see you related to scripting a few months ago?

Back to top

Conclude

As a person of human beings, I don't want to admit it, but I have to say that we may never get rid of the entanglement of PC. How is the California's vulture? Yes, we can kill the Californian vulture. But what about PC? Absolutely impossible; they are really too much.

On the other hand, it is not enough to explain that the bug race cannot be eradicated, and it is not enough to explain that we cannot track and eliminate the bugs, especially those who have laid in our script. If we can use tools such as Microsoft Script Debugger, what are we waiting for? In short, if those computer worms can use such tools to deal with us, the consequences can be unimaginable ... Try the Script Debugger, tell us your feelings.

转载请注明原文地址:https://www.9cbs.com/read-87445.html

New Post(0)