We help IT Professionals succeed at work.
Get Started

do not allow duplicates on vbs output

EfrenM
EfrenM asked
on
370 Views
Last Modified: 2013-11-26
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

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 4 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE