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”)

‘********************************************************
‘CREATE EXPORT FILE
‘********************************************************
‘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\”,””)
Next
‘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)
objCreateFile.Close
Set saveFile = objFSO.OpenTextFile(saveFilePath, 8)

‘The below line writes the column headers in the csv file for each piece of data collected
saveFile.WriteLine “PC_NAME,CURRENT_USER,SUBNET_LOCATION,DEPARTMENT,LOCATION,SITE,IP_ADDRESS”
saveFile.Close

‘********************************************************
‘GET PC NAMES FROM FILE
‘********************************************************
‘ 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”)
‘********************************************************
‘GET CURRENT LOGGED ON USER OF REMOTE PC
‘********************************************************
Dim objSet
Set objSet= objWMIService.InstancesOf (“Win32_ComputerSystem”)
For Each item In objSet
If IsNull(item.UserName) Then
loggedUser = “Nobody is currently logged in”
Else
loggedUser = item.UserName
‘ Same reason as above
loggedUser = replace(loggedUser,”DOMAIN\”,””)
End If
Next

‘********************************************************
‘GET SUBNET DESCRIPTION FIELD
‘********************************************************
‘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
strKeyPath = “REGISTRY\PATH\TO\KEY”
‘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

‘********************************************************
‘GET IP ADDRESSES
‘********************************************************
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
Else
i = i + 1
End If
Next

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
j=0
Else
If ipAddress = “” AND j=0 Then
ipAddress = iprecord
j=1
Else
ipAddress = ipAddress & “,” & iprecord
End If
End If
Next

‘********************************************************
‘WRITE LINES TO FILE
‘********************************************************
‘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
writeData.Close
ipAddress = “”

Loop
objFileReader.Close

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.

Links

RSS 2.0 Feed

Support

Brave Rewards
This site supports Brave Rewards. Please consider tipping or adding it to your monthly contributions if you find anything helpful!

For other ways: Support

Support this blog! If you have found it helpfu you can send me crypto. Any amount is appreciated!
ETH: 0xBEaF72807Cb5f4a8CCE23A5E7949041f62e8F8f0 | BTC: 3HTK5VJWr3vnftxbrcWAszLkRTrx9s5KzZ | SHIB: 0xdb209f96dD1BdcC12f03FdDfFAD0602276fb29BE
Brave Users you can send me BAT using the browser.