Guy’s Ezine 18 – VBScript Carriage Returns

VBScript Carriage Returns


This Week’s Secret

When ever I get an email with complaints it really hurts me. When I recover from the criticism I want to learn from what the person is saying, so I ask myself, ‘Is this fair comment? Or is it just some nutter writing in?’ Well in the case of those who asked for friendlier printer pages in my ebooks, clearly, our poll results show that you represent what the majority want.

52% Said that printing was invaluable for ebooks.

33% Use printing now and again.

15% Rarely or never used printing.

I am truly grateful for you comments and will endeavour to make all future ebooks printer friendly.  A bigger company than mine would have appointed a marketing manager. They would have spent thousands of dollars to tell me what you told me in a minute, I thank all those readers for taking that minute to vote.

Carriage returns in scripts.

Scripts are nothing more than a series of instructions.
The VBScript engine needs to know when an instruction ends
or when the second half of the instruction continues on then next line.

Early in your VBScript career you may have thought an underscore _
At the end of a line was a dead insect that had crawled on to the page!  If you just copy and paste the code, then you may have dismissed these underscores as of no consequence, when in fact they are key to the flow of your script.

When you start writing your own scripts, then you really need to understand how to handle carriage returns. There are two key facts, firstly there has to be a limit of characters per line – often 80. Secondly there must be a way of telling the script engine that you have not yet reached the end of a line. Without a word-wrap instruction (_), VBScript would try and interpret every line as a separate statement. The result would be nonsense. If you try and place a normal carriage return at the end of the first line, the scripting engine thinks you now have two lines. The most likely outcome is a WSH error code 800A0401 or 409

The answer to the word-wrap problem lies in ending the first line with an underscore _
then continue the command on the next line.

Concatenate with ampersand

Concatenate and ampersand are not words you are likely to slip into everyday conversation, however they are vital for the smooth running of your scripts.  This weeks learning points are the use of _ (underscore) and & (ampersand) to control carriage returns in the main body of your script.  In the case of WSCript.Echo commands you can employ additional commands, VBCr (carriage return) and VBTab (to align multiple lines).

Example 1 – To find and display all computer names in Active Directory

The purpose of Example1, is to enumerate and then display all the computer objects in your domain. (Remember to change dc=cp to reflect your domain name).

Copy the VBScript below, then paste into notepad.  Crucial point, to get my script to work in your domain, change this line to reflect your Active Directory name:

& ‘LDAP:// DC=cp, DC=com’" _

Now you are ready to save the file with .vbs extension, for example, computers.vbs. Double click your computers.vbs file and see all machines in your Active Directory.

‘ Computers.vbs
‘ Example VBScript to display the Computer Objects in your  domain
‘ Guy Thomas February 2004.
‘ ******************************
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
"Select Name, operatingSystemVersion from ‘LDAP://dc=cp, dc=com’" _
& " where objectClass=’computer’ "
objCommand.Properties("Page Size") = 500
objCommand.Properties("Timeout") = 20
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
Do Until objRecordSet.EOF
Wscript.Echo "Computer: " & objRecordSet.Fields("Name").Value & VbCr _
& "Version: " & objRecordSet.Fields("operatingSystemVersion").Value

‘ End of example VBScript

Learning points

1) Making carriage returns in the script itself.

"Select Name, operatingSystemVersion from ‘LDAP://dc=cp, dc=com’" _
& " where objectClass=’computer’ "

The above two lines are responsible for extracting all the computer from your Active Directory.  Because they do not comfortably fit on one line, we need a carriage return – THAT VBSCRIPT UNDERSTANDS.  Were you to use the normal word processing ‘enter’, VBScript would interpret the above as two separate statements.  So, that is why we need and _ at the end of the first line.

If you are in doubt, try running the script with the command all on one line.

"Select Name, operatingSystemVersion from ‘LDAP://dc=cp, dc=com’"& " where objectClass=’computer’ "

2) Controlling WScript.Echo messages

WScript.Echo "Computer: " & objRecordSet.Fields("Name").Value& VbCr _
& "Version: " & objRecordSet.Fields("operatingSystemVersion").Value

In the above two lines, I have combined two ideas. Firstly, I wan to make the script believe this is just one instruction – not two. Secondly, I want the output to be in one box and not two.

As with the previous learning point, _ and & enable us to break the line for presentation purposes.  Turning our attention to the output –& VBCr makes a line break WITHIN THE MESSAGE BOX.



When you have a long statement, finish the first line with _
and thus join the two lines into on clear statement.

Be clear that VbCr is purely to make the output clearer.  Whilst the underscore is essential for the script to execute, VbCr is merely a cosmetic device to make the messages easier to read.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Orion performance monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Guy Challenges.

1) Remove & VbCr from line beginning WSCript.Echo.  See how the ComputerName and version run into one another and are all on the same line.

2) Introduce a filter which only display XP machines.  What you need to do is add an extra line, find then change:

"Select Name, operatingSystemVersion from ‘LDAP://dc=cp, dc=com’" _
& " where objectClass=’computer’ "


"Select Name, operatingSystemVersion from ‘LDAP://dc=cp, dc=com’" _
& " where objectClass=’computer’ " _
& " and operatingSystemVersion = ‘5.1 (2600)’"

Note the addition of the _ in the middle line, and the & on the third line.

If you have Windows 2000 machines, substitute ‘5.0 (2195)’ for ‘5.1 (2600)’

3) If you are getting too much output you could try different filtering by altering
‘LDAP://dc=cp, dc=com’ to ‘LDAP://cn=computers, dc=cp, dc=com’ 

What this does is display all the machines specifically in the Computers folder in Active Directory User’s and Computers.  If you prefer to specify an OU the command is OU=MyOrgUnit. Note cn=MyOrgUnit will not work.

Example 2 – Extract the names of all computers in your domain, and save to a text file in the C:\

I would like to thank C.K. for sending in the original script.

Note: Remember to find and change this line:

‘LDAP://DC=cp,DC=com’ "_

Note: The filename is Computernames.txt

‘ VBScript to extract Computer Objects and save to a text file in C:\
‘ Guy Thomas February 2004. With credit to CK.
‘ ******************************
Dim objOU
Dim objFileSystem
Dim objComputer
Dim objFileLocation
objFileLocation = "C:\computernames.txt"

Set objFileSystem = CreateObject("Scripting.FileSystemObject")
Set objComputer = objFileSystem.createTextFile("objFileLocation", TRUE)
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = "SELECT Name FROM " _
&" ‘LDAP://DC=cp,DC=com’ "_
& "WHERE objectClass=’computer’"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
Do Until objRecordSet.EOF
objComputer.WriteLine objRecordset.Fields(0).Value
Wscript.Echo "Computer names saved to this path: " & objFileLocation’
‘ End of example VBScript

Learning Point – attention to detail.

Consider the following statement:

objCommand.CommandText = "SELECT Name FROM ‘LDAP://DC=cp,DC=com’ "_

Here is how I dissected the line and got it to work:

objCommand.CommandText = "SELECT Name FROM " _
& ‘LDAP://DC=cp,DC=com’ "

Here is a tiny difference, lack of " " and it fails

objCommand.CommandText = "SELECT Name FROM  _
& ‘LDAP://DC=cp,DC=com’ "_

Const ADS_SCOPE_SUBTREE = 2 this tell the script to drill down two level from the root of Active Directory, in other works to search OUs and subOUs for any computer objects.

Summary of Carriage Returns

Punctuation makes or breaks writing.  Once you start placing judicious commas, the reader is able to make sense of your prose.  So it is with scripting, VBScript requires special characters to to tell the interpreter that it has reached the end of a line.   Take the time to understand _ (underscore) and especially: & (ampersand).

See more about VBScript

VBScripts  • Ezines  • WMI  • Logon Scripts  • Tool Kit

Ezine 26 SendKeys  • Ezine 34 Scriptomatic  • Ezine 47 .put  •Ezine 51 Sleep  • Ezine 52 OS

Ezine 77 Scriptomatic  • Ezine 84 AcctInfo  • Ezine 88 Progress bar  • Ezine 89 SendKeys

Ezine 97 Net Time  • Ezine 98 W32 Time  • Ezine 99 Time services  • Ezine 120 Sendkeys