Avatar of EfrenM
EfrenM
Flag for United States of America

asked on 

do not allow duplicates on vbs output

Below is a script to audit the computer when a user logs on, it makes the output fine, but it allows duplicates, does anyone know how to make it so it doesnt allow duplicates based on computer name and serial number?
Option Explicit	
 
	Call VendorInfo
 
 Sub VendorInfo
 
	Dim fso, objWMIService, objItem, oFile 
	Dim sFileLocation, strHeader, strComputer
	Dim strOutput, strResults
	Dim colItems
	
	Const wbemFlagReturnImmediately = &h10
	Const wbemFlagForwardOnly = &h20
	Const ForReading = 1
	Const ForWriting = 2
	Const ForAppending = 8
 
' Initialize the variables	
	sFileLocation = "c:\KKVendor.xls"	' Put your filename and network share here i.e. \\server\share\filename.csv
	strHeader = "UserName,ComputerName,Domain,IP Address,ID No.,Name,Vendor,Version,Date" & vbNewLine 
	strComputer = "."
 
' Create the objects
	Set fso = CreateObject("Scripting.FileSystemObject")
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
 
	
' Get Username & Computer & Domain
	Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem", "WQL", _
		wbemFlagReturnImmediately + wbemFlagForwardOnly)
		
		For Each objItem In colItems
			strOutput = strOutput & objItem.UserName & ","
			strOutput = strOutput & objItem.Name & ","
			strOutput = strOutput & objItem.Domain & "," 
		Next
 
 
'Get IP Address	
	Set colItems = objWMIService.ExecQuery _
		("Select IPAddress From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
		
		For Each objItem In colItems
			If objItem.IPaddress(0) <> "0.0.0.0" Then
				strOutput = strOutput & objItem.IPAddress(0) & ","
				Exit For	' We only want the first one it comes across
			End If
		Next
 
' Get ID No. Name, Vendor and Version
	Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", _
		wbemFlagReturnImmediately + wbemFlagForwardOnly)
		
		For Each objItem In colItems
			strOutput = strOutput & objItem.IdentifyingNumber & ","
			strOutput = strOutput & objItem.Name & ","
			strOutput = strOutput & objItem.Vendor & ","
			strOutput = strOutput & objItem.Version & "," 
                        strOutput = strOutput & Date() & vbnewline
 
 
		Next
 
' Write Results To File
	If fso.FileExists(sFileLocation) = False Then
		' Create and write to file with header
		Set oFile = fso.OpenTextFile(sFileLocation, ForWriting, True)
	
			If Err.Number <> 0 Then
				' You could write to the event log here
				Exit Sub
			End If
		
		strResults = oFile.Write(strHeader & strOutput)
	Else
		' File exists so append to it without header
		Set oFile = fso.OpenTextFile(sFileLocation, ForAppending, False)
 
			If Err.Number <> 0 Then
				' You could write to the event log here
				Exit Sub 
			End If
 
		strResults = oFile.Write(strOutput)
	End If 
 
 End Sub

Open in new window

Visual Basic ClassicVB Script

Avatar of undefined
Last Comment
Krys_K

8/22/2022 - Mon