Configure Script to Show Date/Time in Output File

I need to have the script below modified to show the date/time included in the name of the" LOCAL_ADMIN_AUDIT.csv" file



strOutput = "LOCAL_ADMIN_AUDIT.csv"
strInput = "servers.txt"
strStartOfUserToRemove = "VHABALPageM"

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objInput = objFSO.OpenTextFile(strInput, ForReading, False)
Set objOutput = objFSO.CreateTextFile(strOutput, True)
objOutput.WriteLine """ComputerName"",""GroupName"",""User Name"",""Type"",""Description"",""REMOVAL STATUS"""

While Not objInput.AtEndOfStream
      strComputer = Trim(objInput.ReadLine)
      If strComputer <> "" Then
            If Ping(strComputer) = True Then
                  On Error Resume Next
                  Set colGroups = GetObject("WinNT://" & strComputer & "")
                  If Err.Number = 0 Then
                        On Error Goto 0
                        colGroups.Filter = Array("group")
                        For Each objGroup In colGroups
                              If LCase(objGroup.Name) = "administrators" Then
                                    For Each objUser In objGroup.Members
                                          strStatus = ""
                                          strUserPath = Mid(objUser.aDSPath, 9)
                                          arrUserBits = Split(strUserPath, "/")
                                          If UBound(arrUserBits) = 2 Then
                                                strUserPath = arrUserBits(1) & "/" & arrUserBits(2)
                                          ElseIf UBound(arrUserBits) = 1 Then
                                                strUserPath = arrUserBits(0) & "/" & arrUserBits(1)
                                          Else
                                                strUserPath = arrUserBits(0)
                                          End If
                                          If UBound(arrUserBits) = 0 Then
                                                ' This identifies an instance where it only shows a SID
                                                strStatus = "UNKNOWN USER ACCOUNT"
                                                objOutput.WriteLine """" & strComputer & """,""" & objGroup.Name & """,""" & strUserPath & """,""" & objUser.Class & """,""" & objUser.description & """,""" & strStatus & """"
                                          Else
                                                If objUser.class = "User" Then
                                                'WScript.Echo vbTab & "USER:" & objUser.Name
                                                If StrComp(Left(objUser.Name, Len(strStartOfUserToRemove)), strStartOfUserToRemove, vbTextCompare) = 0 Then
                                                      'WScript.Echo "Removing " & objUser.Name & ": " & objUser.adsPath
                                                            objGroup.Remove(objUser.ADsPath)
                                                            strStatus = "REMOVED"
                                                      End If
                                          ElseIf objUser.class = "Group" Then
                                                'WScript.Echo vbTab & "GROUP:" & objUser.Name
                                          End If
                                          strGroupName = objGroup.Name
                                          strUserClass = objUser.Class
                                                On Error Resume Next
                                                strUserDescription = objUser.Description
                                                If Err.Number <> 0 Then strUserDescription = ""
                                                Err.Clear
                                                On Error Goto 0
                                                objOutput.WriteLine """" & strComputer & """,""" & strGroupName & """,""" & strUserPath & """,""" & strUserClass & """,""" & strUserDescription & """,""" & strStatus & """"
                                          End If
                                    Next
                              End If
                        Next
                  Else
                        objOutput.WriteLine """" & strComputer & """,""WMI ERROR " & Err.Number & ": " & Err.Description & """"
                        Err.Clear
                        On Error Goto 0
                  End If
            Else
                  objOutput.WriteLine """" & strComputer & """,""OFFLINE"""
            End If
vhaperbaugubAsked:
Who is Participating?
 
Rgonzo1971Commented:
Sorry VBS not VBA

strOutput = "C:\newfilename" & "_" & Year(Date) & Right("0" & Month(Date), 2) & Right("0" & Day(Date), 2) & "_" & Right("0" & Hour(Time), 2) & Right("0" & Minute(Time), 2) & ".csv"
0
 
Rgonzo1971Commented:
Hi,

pls try

strOutput = "LOCAL_ADMIN_AUDIT" & Format(Now(), "_yyyymmdd_hhmm") & ".csv"

Regards
0
 
vhaperbaugubAuthor Commented:
Getting this error:

Script:     c:\RemoveUsers
Line:        1
Char:       1
Error:       Type mismatch: 'Format'
Code:       800A000D
Source:    Microsoft VBScript runtime error
0
 
vhaperbaugubAuthor Commented:
Thanks....its working now....one last thing....do you know what needs to be done to run this from Task Scheduler? I try to run it but it either doesnt run or I get errors.
0
 
Rgonzo1971Commented:
hi,

In Task Scheduler
Create task
Action tab / New / Start a program
Program/script: path to your batch script e.g. C:\test.bat
Add arguments (optional):
Start in (optional):
Click OK
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.