11 February 2012

Like any admin/tech support rep I don’t like to do tedious things over and over again.  I’ve been learning VBScript to automate a lot of things.  I’ve written a handful of scripts/scraps so far.  The one below is quite useful in gathering data about a list of remote computers.  It accepts input from a text file that is prompted for in the script.  It gathers the current logged on user, certain values in the registry that indicates it’s location (the values and where they are depend on where your company places them, if any), and any IP addresses that are assigned to any NICs in the machine.  It then outputs the data to a csv file, that the user can name in the beginning of the script.  It’s written so that the path to where the file is saved is hard coded and not selectable.  It uses the network roaming desktop path as the place to save it.

I’ll apologize in advance for the lack of “structure” in the code box below.  I’m just using a div to make it so the indentation doesn’t copy over 🙁  I’ll redo it later…

‘ This script accepts input from a line-separated text
‘ file. The text file should contain the computer names on
‘ each line that you want to run the script against.

‘ It will gather the current logged on user for each
‘ computer, various registry key values related to location
‘ and any IP address for it.’ Data is exported in CSV format.
‘ Script written by TheCuriousGeek: http://thecuriousgeek.org

On Error Resume Next
Const HKEY_LOCAL_MACHINE = &H80000002
Set objFSO = CreateObject(“Scripting.FileSystemObject”)

‘let user choose filename
saveFilename = InputBox(“Enter filename (without extension). It will save to your desktop”)
Set objSetCurUsr=GetObject(“winmgmts://.”).InstancesOf (“Win32_ComputerSystem”)
‘Get’s username of person running the script, used below for saving file to users roaming desktop path
For Each item In objSetCurUsr
currentUser = item.UserName
‘For domain users, the username is returned prefixed with the domain they are logged into, this line removes that. Replace “DOMAIN” with your actual domain name.
currentUser = replace(currentUser,”DOMAIN\”,””)
‘Create the file path to the users roaming desktop location. Then creates the file based on user input at start.
saveFilePath = “\\NETWORK PATH TO USER DESKTOP” & currentUser & “PATH CONT.” & saveFilename & “.csv”
Set objCreateFile = objFSO.CreateTextFile(saveFilePath)
Set saveFile = objFSO.OpenTextFile(saveFilePath, 8)

‘The below line writes the column headers in the csv file for each piece of data collected

‘ Opens an “Open file” dialogue to select the file containing the list of pc names
Set objFile = CreateObject(“UserAccounts.CommonDialog”)
openBox = objFile.ShowOpen
pcFileList = objFile.Filename

Set objFileReader = objFSO.OpenTextFile(pcFileList, 1)

‘Begin the loop that goes through each pc name in the file and collect the data for it
Do Until objFileReader.AtEndOfStream
strComputer = objFileReader.ReadLine
Set objWMIService = GetObject(“winmgmts:{impersonationLevel=impersonate}!\\” & strComputer & “\root\CIMV2”)
Dim objSet
Set objSet= objWMIService.InstancesOf (“Win32_ComputerSystem”)
For Each item In objSet
If IsNull(item.UserName) Then
loggedUser = “Nobody is currently logged in”
loggedUser = item.UserName
‘ Same reason as above
loggedUser = replace(loggedUser,”DOMAIN\”,””)
End If

‘Connect to remote PC’s registry
Set objRegistry=GetObject(“winmgmts:\\” & strComputer & “\root\default:StdRegProv”)
‘Set variable to the key path, this is the path minus the HKLM, HKUSERS, etc
‘Read each value in the key path. It begins with the variable that points to the registry section (HKLM, HKUSERS, etc).
‘It’s a variable that was set in the beginning of the script. The name in quotes in each below is the DWORD, String, etc
‘value that is in the registry. The last part of the line is the variable I’m saving the data to.
objRegistry.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,”SubnetDescription”,subnetDescription
objRegistry.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,”Department”,regDepartment
objRegistry.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,”Location”,regLocation
objRegistry.GetExpandedStringValue HKEY_LOCAL_MACHINE,strKeyPath,”Site”,regSite

Dim ipArray(5)
‘Connect to the Network info of the PC. Only pull back IpAddress
Set getIP = objWMIService.ExecQuery(“SELECT IpAddress FROM Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE”)
i = 0
For Each colItem In getIP
ipArray(i) = Join(colItem.IPAddress)
‘Because I have to set the length of the array, not all PC’s may have the same
‘number of IPs/active NICs. I dont want blank entries in my file so I check
‘for empty values and then overwrite that index in the array with the next.
If ipArray(i) = “” Then
i = i
i = i + 1
End If

j = 0
iprecord = “”
‘Now I iterate through the array I gathered above and save all the IP’s to a variable
‘Because some of the array values may be empty, I check for that and don’t add
‘blank comma separated spaces in the variable
For Each iprecord In ipArray
If iprecord = “” Then
If ipAddress = “” AND j=0 Then
ipAddress = iprecord
ipAddress = ipAddress & “,” & iprecord
End If
End If

‘Here I take all of the variables I set above and put them into a single variable,
‘separate by commas, and then write it to the file
saveData = strComputer & “,” & loggedUser & “,” & subnetDescription & “,” & regDepartment & “,” & regLocation & “,” & regSite & “,” & ipAddress
Set writeData = objFSO.OpenTextFile(saveFilePath, 8)
writeData.WriteLine saveData
ipAddress = “”


Wscript.Echo “Finished collecting data.”






































































Hopefully all or part of this script can help any admins out there do some data gathering!

There are no comments.

You must be logged in to post a comment.