Contents of Guy’s Scripting Ezine No 12 – Create files with FSO
This Ezine was only an introduction. See more complex FSO examples here.
I will let you into a secret, until I did a little research, I did not realize there were quite so many Microsoft programs that add users to Active Directory. Here is a list of the programs with my view of their personality.
- CSVDE – Solid versatile.
- LDIFDE – Fringe or special teams player.
- DS Add – New kid on the block.
- ADC agreements – The specialist. Here is a tool for migrating from Exchange 5.5 to Active Directory.
- Active Directory Users and Computers – A reliable workhorse, but a bit of a plodder if you have lots of accounts to create.
- VBScript – Wise old sage, very versatile.
I will let you into another secret, when ever I am rude about one of the above programs, someone always comes up with a killer use for that program. My conclusion is that each program has its own special use and it is a matter of "horses for courses".
For the next few weeks I would like to feature one of the above programs. This will be in addition to the regular VBScript section.
Guy Recommends: The Free IP Address Tracker (IPAT)
Calculating IP Address ranges is a black art, which many network managers solve by creating custom Excel spreadsheets. IPAT cracks this problem of allocating IP addresses in networks in two ways:
For Mr Organized there is a nifty subnet calculator, you enter the network address and the subnet mask, then IPAT works out the usable addresses and their ranges.
For Mr Lazy IPAT discovers and then displays the IP addresses of existing computers. Download the Free IP Address Tracker
Firstly, CSVDE is a built-in command found in Windows 2000, 2003 and XP; just go to the command prompt and type CSVDE /?. The key concept with CSVDE is that the first line of the text file (.csv) contains the fields. These fields are actually LDAP attributes, so from a learning point of view, CSVDE is ideal for investigating the LDAP properties of a user object.
For those of us who love spreadsheets, CSVDE is great because you can manipulate the data in Excel, then import the accounts into Active Directory. Comma separated variable (CSV) is a very convenient and versatile format for transferring account data. In fact, I also generate CSV files for exporting data from Event Logs so that I can analyse the data in Excel or Access.
If you are new to the built-in CSVDE command, I always recommend using the EXPORT mode first. My reasoning is that it is annoying if you make a mistake and import 1500 users all with one character where as you meant to import 150 users each with 10 attributes.
Example: CSVDE -f GuyAd.csv (-f tells CSVDE the file name to dump Active Directory.) Next open up Excel, go to the File Menu, Open, Files of Type – (change to all files) select your file e.g. GuyAd.csv.
The key to getting the most out of CSVDE is to master the filters. If you just dump all of Active Directory into a .CSV file you will be overwhelmed with LDAP data. By filtering you can extract just the attributes that you are interested in. When it comes to importing you can use the LDAP knowledge to control accounts. For example, to enable or disable accounts, and to make users members of global groups. The first filter switch for CSVDE is -r.
CSVDE -f GuyUser.csv -r “(&(objectClass=user))” (Be careful with the brackets and speech marks.)
Another way to filter is to use -l (Lower case L) the idea here is to filter just the fields (columns) you need.
Example: CSVDE -f GuyFilter.csv -l “DN, objectclass, cn, sn”
The killer disadvantage of using CSVDE to bulk import users is that you cannot use it to set passwords. Another irritation is that you can only create new users and not modify existing accounts.
A new year is a good time to take stock, so hear are some frequently asked questions about the role of VBScript in Windows 2000 /2003.
Q1. What is VBScript used for?
Ans. Logon scripts. Also for changing the registry, adding accounts to Active Directory and interrogating event logs.
Q2. Is VBScript used for anything else?
Ans. WMI (Windows Management Instrumentation) is becoming a huge area for creating scripts to read or change many properties of the operating system.
Q3. I am coming from DOS and UNIX how can I get a handle on VBScript?
Ans. The scripts are like batch files that run in a ‘Dos box’ (Unix Shell)
Q4. Is VBScript an Interpreted or Compiled language?
Ans. Interpreted, so beware of more runtime errors than you get with compiled programs.
Q5. Where is VBScript’s interpreter?
Ans. WSH (Windows Script Host). Useful to appreciate the role of WSH, because, VBScripts do not run on older clients like Windows 98 unless you get a Windows Script Host (WSH) add-on.
Q6. Where and what is the VBScript engine?
Ans. WSH runs VBScripts through two executables, WScript.exe (W = Windows) and CScript (C = command line). Now it makes sense where, and why you see WScript instructions in VBScripts. Example: Set net = CreateObject(“WScript.Network”)
The purpose of the script below is to investigate how VBScript can create and manipulate files. Here we will create a file and add some text. In future weeks we can see how we can read information from files and add it to Active Directory.
Copy the script below, save it with .vbs extension, for example Yourname.vbs. Double click YourName.vbs and check the folder referenced by line 7.
Note: There is a trap in the script.
Change the path on line 7. Find the line below in your script and to point a folder on your machine.
Set GuyFile = fso.CreateTextFile(“e:\ezine\newsletter13.txt”, True)
‘ Example VBScript
‘Create a text file
DIM fso, GuyFile
Set fso = CreateObject(“Scripting.FileSystemObject”)
Set GuyFile = fso.CreateTextFile(“e:\ezine\newsletter13.txt”, True)
GuyFile.WriteLine(“Ezine 13 Available Jan 11th”)
GuyFile.Write (“Next week will be about creating user accounts”)
GuyFile.Write (” More on same line”)
In the above example script, fso (FileSystemObject) creates an object which we can manipulate. There are several methods which we could apply to the fso object, but I have chosen the CreateTextFile method. Once we have created the file as a TextStream object, then we can start doing some useful work. To get text into the file, I have selected two more methods, .WriteLine and plain .Write. Whereas .WriteLine adds a carriage return and so makes a complete line, plain .Write, just adds text but no end of line marker.
Finally, good practice is to tidy up with a Close statement.
This Example was only an introduction. See more complex FSO examples here.
An Ezine of three parts. Firstly a reminder of the sheer variety of scripting methods available to create users. Secondly instructions on how to get started with CSVDE. Finally something a little different using the File System Object FSO to write text into files.
See more about scripting files with VBScript