Query AD

After writing the script that generates the lab usage report, I looked at the BuildArray procedure and decided to change it to be more generic. This would allow me to use it in different ways.

The QueryAD procedure is a very simple script that loops through a recordset of objects returned from the Active Directory. The procedure uses the strQuery variable to represent your LDAP query.

This procedure accepts one parameter:

strQuery

The strQuery parameter represents the Active Directory query you wish to perform and should look similar to the following:

strQuery = “SELECT ” & strPropertyList & ” FROM ‘” & strLDAPURL & “‘ WHERE objectClass = ‘” & strObjectClass & “‘”

I have shown the assignment using variables to represent the significant portions of the query, you could certainly manually build the query, but this format allows for much more flexibility.

Update Array

The UpdateArray procedure updates the arrLabs array. The purpose of this procedure is to generate a tallies that will be used to in a report to display lab usage. This procedure depends on the arrLabs array being declared globally as well as the GetProp function being available.

This procedure accepts three parameters:

  • strADSPath
  • intArrIndex
  • strLab

strADSPath

This is the LDAP URL of the object we are tallying.

intArrIndex

There are 8 labs that we are responsible for, so the intArrIndex is the first element in the array that corresponds to a particular lab.

strlab

This is a value that is stored in the first column of the array index.

Script Details

I just wrote the ScriptDetails function today, I wanted better information in the Application Log regarding what script executed, where was it located, and who executed it. This information is then passed back to the LogData procedure to be input into the Application Log.

The functions accepts one parameter:

  • strComputer

strComputer

This function takes advantage of the Wscript object to return the name of the script in memory, and its fully qualified path. Then it uses WMI to connect to the computer, it uses the value in strComputer to return the username of the user currently running this script.

Build Array

The BuildArray procedure connects to the provided domain and returns a list of requested objects. Then it loops through those objects checking for a specific value in the DistinguishedName property. The lab structure in our domain uses the room number of a given lab as the name of the OU.

I use the InStr function to locate the room number and then based on that information the UpdateArray procedure creates the tallies in the arrLabs array.

This procedure relies on a particular Active Directory OU structure to work properly so it may require some adjustment.

This procedure accepts two parameters:

  • strLDAP
  • strObject

strLDAP

This is the LDAP URL of the Active Directory domain you wish to connect to.

strObject

This is the Active Directory Object you wish the query to return.

Write to AD Objects

The WriteData procedure originally was used to update the Description property of computer objects in the Active Directory. The procedure can now update any single-valued property of a given object in the Active Directory.

The procedure accepts the following parameters:

  • strProperty
  • strValue
  • strADSPath

strProperty

This is a string variable that would contain any one of the many single-value properties available in Active Directory. I typically pass in “Description” as that’s the property I’m interested in.

strValue

This is a string variable that holds the data to be written to the Active Directory. The information can be statically passed in or created based on calls to other functions within the script.

strADSPath

This is a string variable that holds the DistinguishedName property of the object we are working with. This is typically passed in from another function that queries the Active Directory for object data.

How I use scripting

I work with two developers, in order to reduce the amount of teasing I decided to modularize all of my code. I have several functions and subroutines that I am fairly proud of and will document their use here. All of the scripts will be freely available in my public “Scripts” folder.

If you have any questions don’t hesitate to write, my contact information is available through my profile.

Get Serial

About 99.9% of the computers we manage are Dell computers. Each Dell computer has a unique service tag that can be used to lookup anything from warranty information to the correct drivers. The serial number is stored in the BIOS and can be changed using a Dell supplied utility.

This function, GetSerial, returns the serial number stored in the BIOS of the computer. It can be used in a number of different ways. One way is to tack it onto the end of a the warranty URL from dell and then you can create a nice clickable table of your computers with the ability to see where each is in the warranty status.

This function accepts one parameter:

  • strComputer

strComputer

This string variable is usually passed from a calling routine. That routine may be working through a .csv file or it may be pulling this data direct from Active Directory.

WMI Ping Script

Most of the computer management scripts run against multiple computers, I usually want to have the script do one task if the computer is available for management or another when it’s not. So I put together a very simple function, WMIPing, that takes advantage of the Err object.

The function accepts a single value:

  • strComputer

strComputer

This string variable is usually passed from a calling routine. That routine may be working through a .csv file or it may be pulling this data direct from Active Directory. I attempt a connection to the computer, if it fails I set the online flag to False, otherwise I set it to true.

Get MAC Address

In managing our IP space we rely heavily on the MAC address of a computer. This seems to be slightly backwards of how most people manage their IP space, but we consider the IP to be a property of the MAC, and the MAC itself represents the computer.

The GetMac, function grabs the MAC address of the network interface of the computer. In Windows there are multiple “interfaces” so if the interface doesn’t have an IP address in the proper range I don’t worry about it. We have noticed that on some machines, apparently randomly, the correct IP and MAC are reported back twice, so I exit the entire loop once I get a MAC address.

This function accepts one parameter:

  • strComputer

strComputer

This string variable is usually passed from a calling routine. That routine may be working through a .csv file or it may be pulling this data direct from Active Directory.

Build Report

The BuildReport procedure is a basic file creation script. It verifies that the folder that is passed in exists before processing anything. I could have made the script create the folder if it didn’t exist, but in our environment a given user may not be able to create a directory. This way it logs the error in the application log for later review.

This procedure accepts two parameters:

  • strFileName
  • strFilePath

strFileName

This is the filename and extension of the file you are creating.

strFilePath

This is the path to the file including drive letter assignment. If the folder doesn’t exist, the procedure logs an error and exits the routine.