Need help in getting login information on a windows server using vbscript

61 views Asked by At

I need to get the information on who all logged into my servers thru RDP in the last n number of days. I found a script on a website and I modified it as per my requirements. I can iterate thru multiple servers and get the subfolders and write the results to a text file. The only problem is I am not able to apply date filter, I want to list the subfolders under the folder C:\Users for a specific date range. I am just amateur in vbscript, can someone help me here, below is the script:

Dim comparray(1)
Dim Item, objWMIService, colSubfolders, objFolder

Set objFSO=CreateObject("Scripting.FileSystemObject")

outFile="MyPath\Results.txt"

Set objFile = objFSO.CreateTextFile(outFile,True)

comparray(0)= "Server1"
comparray(1)= "Server2"

For Each Item in comparray

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & Item & "\root\cimv2")

Set colSubfolders = objWMIService.ExecQuery _
    ("Associators of {Win32_Directory.Name='c:\users'} " _
        & "Where AssocClass = Win32_Subdirectory " _
            & "ResultRole = PartComponent") 
For Each objFolder in colSubfolders
    'Wscript.Echo objFolder.Name & Item
    objFile.WriteLine objFolder.Name & ":" & Item
Next
Next

I am trying another script but with this script I am not able to iterate thru servers, it gives me results for only one server though I have mentioned two servers. I am not able to determine where should I put the block for the array of the servers, below is the second script:

Set objFSO=CreateObject("Scripting.FileSystemObject")

outFile="MyPath\Results.txt"

Set objFile = objFSO.CreateTextFile(outFile,True)

showfolderlist "C:\Users"

Sub ShowFolderList(folderspec)
    Dim Item, fs, f, f1, fc, s, t
    Dim comparray(1)
    comparray(0)= "server1"
    comparray(1)= "server2"
    For Each Item in comparray
    Set fs = CreateObject("Scripting.FileSystemObject")
    Set f = fs.GetFolder(folderspec)
    Set fc = f.SubFolders
    t = (DateAdd("d",-1,"03-25-2020"))
    For Each f1 in fc
    If f1.datelastmodified > t Then
        s = s & f1.name & " - " & f1.datelastmodified
    s = s &  vbCrLf 
    objFile.WriteLine Item & ":" & vbCrLf & s
        End If
    Next
    Next
End Sub
1

There are 1 answers

0
Iftekhar Ilm On BEST ANSWER

Finally I was able to use the LastModified property of Win32_Directory class and I am able to find the folders with specific date range.

If objFolder.LastModified > "20200319101621.666501-240" Then
    'Wscript.Echo objFolder.Name & Item
    objFile.WriteLine Item & "|" & objFolder.Name & "|" &         WMIDateStringToDate(objFolder.LastModified)
End If