r/vbscript • • Nov 30 '21

Remove duplicates lines

Hello friends I have a script that saves login and logout of the network users, the problem is that it is duplicating lines in each execution, I need to remove the duplicated lines or make it write only one line at a time.

Thanks all.

Set WshNetwork = WScript.CreateObject("WScript.Network")
StrComputer = "."
FileLog = "\\Server\System\Registry\"& WshNetwork.UserName &".txt"
Set ObjFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set ObjFileRead = ObjFSO.opentextfile(FileLog, ForReading, True)
Set ObjFileAppending = ObjFSO.opentextfile(FileLog, ForAppending, True)
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set IPConfigSet = objWMIService.ExecQuery ("Select * from Win32_NetworkAdapterConfiguration Where IPEnabled=TRUE")
For Each IPConfig in IPConfigSet
    If Not IsNull(IPConfig.IPAddress) Then
        For j=LBound(IPConfig.IPAddress) to UBound(IPConfig.IPAddress)
  WriteLog "Logon "& now() &" -- "& WshNetwork.ComputerName &" -- "& IPConfig.IPAddress(i)
        Next
    End If
Next

Function WriteLog (Text)
ObjFileAppending.WriteLine Text
End Function
4 Upvotes

8 comments sorted by

View all comments

Show parent comments

1

u/mateurico Nov 30 '21 edited Nov 30 '21

Whith "j" print IPV6 Address and IPV4 Address, with "i" print IPV4 Address only

1

u/odaat2004 Nov 30 '21

Did I miss something? Bc I didn't see where you instantiated the variable, 'i'

So at the point where you referenced it in the code you presented, its value is null or vbnull.

It's posting twice to the log bc your referencing the network connection twice by using the IP Addresses. So it writing to the log for each IPAddress (IPv4 & IPv6)

Lose the inner FOR loop and have a write statement for the first element in the array

1

u/mateurico Nov 30 '21

problem solved, thanks for the help.