Thursday, July 24, 2008

Learning VBScript: structure and basic commands

Script Headers:
This is how you start your script; by telling the computer what to expect and how to run the script.

"Option Explicit": You're going to list each variable in the script before it is used. If you don't use this it'll be assumed that any statement that VBScript doesn't recognize is a variable. In order for it to work, though, it must be the first non-commented out line in the script. Option Explicit can also be thought of as a spell checker since it'll give an error if you misspell a variable name later in your code.

"Dim": Declares a variable. (Example: "Dim regComputerName")

"On Error Resume Next": Tells the computer that if it runs into an error it should keep going and try the next line of the script. (You may want to omit this -at least until the code has been tested as it may hide mistakes you'll want to know about.)

Reference Information:
This is the second section in your code; where you assign values to your variables, etc.

Example: (using "regComputerName" as the variable)
regComputerName = "HKLM\SYSTEM\CurrentControlSet\Control" &_
"\ComputerName\ComputerName\ComputerName"

HKLM:
The script knows that HKLM stands for HKEY_LOCAL_MACHINE, but be aware that it's case sensitive! Also, the &_ is just for continuing the code on a new line.

Avoid typos in registry keys:
To make sure you have the correct registry key, in regedit.exe you can go to the "Edit" menu and choose "Copy Key Name" to copy it rather than trying to type it and risk typos ;-).

Worker Information:
In this section your variables, etc are actually put to work doing something!

"Set": Command used to assign an
object reference to a variable. Example:
Set objShell = CreateObject("WScript.Shell")

"Create Object": As used in the example above, this part of an expression assigns a variable name to a reference.

"WScript.CreateObject": 99% of the time this is the same as using "Create Object"

Output:
How you display information.

"WScript.Echo": used to display text in a command window or pop up box depending on how it's run. When run using CScript, it'll write it in a command shell. When you use Wscript.exe it'll write to a Windows dialog box.
Example:
WScript.Echo ComputerName & " is computer name"

Symbol "&": The "&" symbol just puts two things together. Here those two things are the value of "Computer Name
and the text "is computer name".

General Info:

Comments:
Preface comments with a single quote " ' ".



No comments:

Post a Comment