vhaperbaugub
asked on
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.Fi leSystemOb ject")
Set objInput = objFSO.OpenTextFile(strInp ut, ForReading, False)
Set objOutput = objFSO.CreateTextFile(strO utput, True)
objOutput.WriteLine """ComputerName"",""GroupN ame"",""Us er Name"",""Type"",""Descript ion"",""RE MOVAL 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.AD sPath)
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
strOutput = "LOCAL_ADMIN_AUDIT.csv"
strInput = "servers.txt"
strStartOfUserToRemove = "VHABALPageM"
Const ForReading = 1
Set objFSO = CreateObject("Scripting.Fi
Set objInput = objFSO.OpenTextFile(strInp
Set objOutput = objFSO.CreateTextFile(strO
objOutput.WriteLine """ComputerName"",""GroupN
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,
'WScript.Echo "Removing " & objUser.Name & ": " & objUser.adsPath
objGroup.Remove(objUser.AD
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
ASKER
Getting this error:
Script: c:\RemoveUsers
Line: 1
Char: 1
Error: Type mismatch: 'Format'
Code: 800A000D
Source: Microsoft VBScript runtime error
Script: c:\RemoveUsers
Line: 1
Char: 1
Error: Type mismatch: 'Format'
Code: 800A000D
Source: Microsoft VBScript runtime error
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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
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
pls try
strOutput = "LOCAL_ADMIN_AUDIT" & Format(Now(), "_yyyymmdd_hhmm") & ".csv"
Regards