What I've created is a script which pulls the Dell Service code, Username, and Computername, from a computer, and compiles that information into a .csv file. This script will be implemented via Active Directory login scripts, so end users wont have to do a thing.
The problem I'm having though, is everytime a person logs on, it collects their information, and adds it to the list. This means my list could just be full of two people who log onto their computer over and over.
What I would like to happen is for the script to search the .csv file for the specific data collected, and if this data exists, to not enter it.
The code I have so far is this:
'Get Dell Service Tag Info set ProSet = GetObject("winmgmts:").InstancesOf("Win32_BIOS") Set ProSet1 = GetObject("winmgmts:").InstancesOf("Win32_SystemEnclosure") For each Pro in ProSet For each Pro1 in ProSet1 ServiceTag=Pro.SerialNumber exit for Next exit for Next 'get username and computername, could also be asked in a batch Set oShell = WScript.CreateObject("WScript.Shell") Set oShellEnv = oShell.Environment("Process") sComputerName = oShellEnv("ComputerName") sUsername = oShellEnv("username") dim filesys, filetxt, getname, path Set filesys = CreateObject("Scripting.FileSystemObject") Set filetxt = filesys.OpenTextFile("\\xx.xx.xx.xx\Gathering\DataLog.csv", 8, True, -2) path = filesys.GetAbsolutePathName("\\xx.xx.xx.xx\Gathering\DataLog.csv") getname = filesys.GetFileName(path) filetxt.WriteLine sUsername & ", " & sComputerName & ", " & ServiceTag filetxt.Close
This is the basic script, without the entry checker.
As for an entry checker, this is what I have tried but it doesn't seem to work:
Set objFSO = CreateObject("Scripting.Dictionary") Set objFile = objFSO.OpenTextFile ("\\xx.xx.xx.xx\Gathering\Dictionary.txt", 8, True) ' Make comparisons case insensitive. objList.CompareMode = vbTextCompare ' ... code to read user name and assign to variable strNameOfUser. If (objList.Exists(strNameOfUser) = False) Then ' Add this user to the dictionary object. objList(strNameOfUser) = True ' Log this unique user name. objFile.WriteLine strNameOfuser End If
Any help is appreciated! Thanks!Answer1:
(1) Your code is messed up: You store a Dictionary in objFSO and then try to invoke objFSO.OpentextFile()
(2) VBScript can't read <strong>and</strong> append to a file; so ForAppending (8) won't work; you'll have to read-open the file first, gather the info, close it, append-open and append new user info (if necessary)
(3) Using a dictionary is unnecessarily complex: to fill the dictionary you'll have to read the file from start to end, before you can ask the dictionary whether a specific user exists. While simply reading the file line by line, you can break the reading as soon as you find the user - then close - open & append - close - done.
Item (3) in code:
Dim goFS : Set goFS = CreateObject( "Scripting.FileSystemObject" ) Dim tsUsers : Set tsUsers = goFS.OpenTextFile(sFSpec, ForReading, True) Dim bFound : bFound = False Do Until tsUsers.AtEndOfStream If 1 = Instr(tsUsers.ReadLine(), sUser) Then bFound = True Exit Do End If Loop tsUsers.Close If Not bFound Then Set tsUsers = goFS.OpenTextFile(sFSpec, ForAppending, False) tsUsers.WriteLine sUser tsUsers.Close End If
Because I'm a pessimistic worrier: How do you plan to cope with more than one user logging in and write-access the file at the same time?