| |
jmaley
Posts: 5
Score: 0
Joined: 10/17/2005
Status: offline
|
'========================================================================== ' NAME: UserStatus ' Author: J. Maley ' Version: 3.0 ' COMMENT: The script takes the results of a UserStat file. Creates an Excel ' spreedsheet and then works through calulating when was the last ' time a user account was authenticated on a Domain. (NT) ' Note: A user account may never have logged onto a Domain but still be quite ' vaild. For example if you use an account to run your SQL server servcies. ' It may not have been ever physically logged on but you'll be out of a ' job if you delete it. ' ' Change the constant "YOUR_DOMAIN_NAME" to reflect your domain ' ' Requirement: EOLWTSCOM.DLL need to be registerd on the machine this script ' is running on to retrieve the TS Profile Path, this is handeled ' in the startup command file, at the end the dll is unregistered ' Download from FTP://FTP.GO-EOL.COM/PUBLIC/THINSSENTIALS/WTSCOM ' ' Userstat.exe from the Win2K Resource Kit ' ' Calling Command File : ' REM USRSTAT your_domain_name ' usrstat % 1>ll.dat ' regsvr32 /s eolwtscom.dll ' wscript userstatusv3.vbs ' regsvr32 /S /u eolwtscom.dll ' del ll.dat '========================================================================== Option Explicit On Error Resume Next 'Define Constants Const ForReading = 1 Const ForWriting = 2 Const ForAppending = 8 Const CompDteCol = 8 Const LeadingZero = FALSE Const Decimals = 0 Const NegNumInParen = FALSE Const GroupDigits = TRUE Const WTSUserConfigTerminalServerProfilePath = 15 Const tstdate = Date()-90 'Subtract 90 days from today's date Const strDom = "Your_Domain_Name" 'Declare Variables Dim objFSO, objLogFile, objXL, oRow, oColumn, strdate, tstdate Dim strDom, strUsr, strTSPP, strLine, intCountColumns, intCountRows Dim arrName, intCount, RecentDate, strLogonSrvr, objUserProfile Dim objUser, strUsrPrf, Network, intUserCount ,strMsg, oWTSCOM, oShell Dim RootDir, FileSystem, homeFolder, SubFolders, Folder, FolderSize, intTmp, TMP Dim strStatusBarText ' contains text to display in Excel's status bar ' confusingly, MS uses a Variant-alike data type in Excel ' so that setting the status bar with a value of False ' (i.e. not the string "False" but a boolean False) returns ' the status bar text to a default state. Apparently, ' using a null string doesn't do the same thing... Set network = Wscript.CreateObject("WScript.Network") Set objFSO = CreateObject("Scripting.FileSystemObject") Set oWTSCOM = CreateObject("EOLWTSCOM.WTSCOM") 'Open the input file, this is redirected output from 'the usrstat.exe program found in the Win2K resource kit Set objLogFile = objFSO.OpenTextFile("ll.dat", ForReading) 'All the inital Excel setup/formating Set objXl = Wscript.CreateObject("Excel.Application") objXL.Visible = TRUE objXL.WorkBooks.Add objXL.Columns.ColumnWidth = 24 objXL.Sheets("Sheet1").Name = "User ID Audit" strStatusBarText = "Starting" objXL.StatusBar = strStatusBarText Set_Headers() objXL.Rows(3).Select objXL.Selection.font.bold=true objXL.Selection.font.size=10 oRow = 4 intUserCount = oRow oColumn = CompDteCol '============================================================================ 'List All The User ID's in Column 1 and Full Names in Column 2 '============================================================================ strStatusBarText = "Retrieving UserID and User Name" objXL.StatusBar = strStatusBarText Do While Not objLogFile.AtEndOfStream strLine = Trim(objLogFile.ReadLine) If Left(strLine, 11) = "Users at \\" Then 'We only want to run through the first set of users intCount = intCount + 1 If intCount = 2 Then Exit Do End If ELSE arrName = Split(strline,"-",-1,1) objXL.Cells(oRow, 1).Value = Trim(arrName(0)) objXL.Cells(oRow, 2).Value = Trim(arrName(1)) strUsr = Trim(arrName(0)) intUserCount = intUserCount + 1 oRow = oRow + 1 End If Loop ' Close the input file. objLogFile.Close intCountRows = oRow oRow = 4 '========================================================================= 'Check to see if the Account is Disabled by passing the Domain & UserID 'to the function and recieving the Account status back as True or False. '========================================================================= Do While oRow < intCountRows 'Runs through each user line strUsr = objXL.ActiveSheet.Cells(oRow,1).Value strStatusBarText = "Retrieving Account Status for " & strUsr objXL.StatusBar = strStatusBarText Set objUser = GetObject("WinNT://" & strDom & "/" & strUsr & ",user") If objUser.AccountDisabled = True Then objXL.Cells(oRow, 3).Font.Bold=True objXL.Cells(oRow, 3).Value = "Disabled" Else objXL.Cells(oRow, 3).Font.Bold=False objXL.Cells(oRow, 3).Value = "Active" End If oRow = oRow + 1 Loop intCountRows = oRow oRow = 4 '========================================================================= 'ReOpen the File for Parsing '========================================================================= Set objLogFile = objFSO.OpenTextFile("ll.dat", ForReading) 'List Each Server and last Logon Times Do While Not objLogFile.AtEndOfStream strUsr = objXL.ActiveSheet.Cells(oRow,1).Value strStatusBarText = "Retrieving Last Logon Date for " & strUsr objXL.StatusBar = strStatusBarText strLine = Trim(objLogFile.ReadLine) If Left(strLine, 11) = "Users at \\" Then 'Locate Lines that represent a new server Thus a new column oColumn = oColumn + 1 oRow = 4 objXL.Cells(3, oColumn).Columns.ColumnWidth = 15 objXL.Cells(3, oColumn).Value = Mid(strLine, 10) ELSEIF Right(strLine, 5) = "Never" Then objXL.Cells(oRow, oColumn).Value = Right(strLine, 5) oRow = oRow + 1 ELSE 'We do not want the whole line so the below code breaks up and 'creates a date format that we can use. strLine = Right(strLine, 20) strLine = Mid(strLine, 5, 2) & " " & Left(strLine, 3) & " " & Right(strLine, 4) strline = CDate(strLine) objXL.Cells(oRow, oColumn).Value = strLine oRow = oRow + 1 End If Loop ' Close the input file. objLogFile.Close intCountColumns = oColumn intCountRows = oRow oRow = 4 '==================================================== 'Compare Each Date and list the Most Recent '==================================================== Do While oRow < intCountRows 'Runs through each user line strUsr = objXL.ActiveSheet.Cells(oRow,1).Value strStatusBarText = "Calculating Last Authentication Date for " & strUsr objXL.StatusBar = strStatusBarText RecentDate = Comparedate(oRow) If RecentDate <= tstdate OR RecentDate = "Never" Then objXL.Cells(oRow, CompDteCol).Font.Bold=True ELSE objXL.Cells(oRow, CompDteCol).Font.Bold=False End If objXL.Cells(oRow, CompDteCol).Value = RecentDate oRow = oRow + 1 Loop intCountRows = oRow oRow = 4 oColumn = CompDteCol intCount = 0 '==================================================== 'Retrieve the Domain User Profile '==================================================== Do While oRow < intCountRows strUsr = objXL.ActiveSheet.Cells(oRow,1).Value strStatusBarText = "Retrieving Domain User Profile for " & strUsr objXL.StatusBar = strStatusBarText Set objUser = GetObject("WinNT://" & strDom & "/" & strUsr & ",user") If objUser.Profile = "" Then strUsrPrf = "" Else strUsrPrf = objUser.Profile END If objXL.Cells(oRow, 4).Font.Bold = False objXL.Cells(oRow, 4).Value = strUsrPrf oRow = oRow + 1 Loop intCountRows = oRow oRow = 4 '==================================================== 'Retrieve the Terminal Server Profile Path '==================================================== Do While oRow < intCountRows On Error Resume Next strTSPP = "" strUsr = objXL.ActiveSheet.Cells(oRow,1).Value strStatusBarText = "Retrieving Terminal Server Profile Path for " & strUsr objXL.StatusBar = strStatusBarText strTSPP = oWTSCOM.WTSQueryUserConfig("\\USHATNTBDC1", strUsr, WTSUserConfigTerminalServerProfilePath) objXL.Cells(oRow, 5).Font.Bold = False objXL.Cells(oRow, 5).Value = strTSPP oRow = oRow + 1 Loop intCountRows = oRow oRow = 4 '==================================================== 'Retrieve Home Folder Location '==================================================== Do While oRow < intCountRows On Error Resume Next strUsr = objXL.ActiveSheet.Cells(oRow,1).Value strStatusBarText = "Retrieving Home Folder Location for " & strUsr objXL.StatusBar = strStatusBarText Set objUser = GetObject("WinNT://" & strDom & "/" & strUsr & ",user") objXL.Cells(oRow, 6).Font.Bold=False objXL.Cells(oRow, 6).Value = objUser.HomeDirectory oRow = oRow + 1 Loop intCountRows = oRow oRow = 4 '==================================================== 'Retrieve Home Folder Size '==================================================== Do While oRow < intCountRows On Error Resume Next strUsr = objXL.ActiveSheet.Cells(oRow,1).Value strStatusBarText = "Retrieving Home Folder Size for " & strUsr objXL.StatusBar = strStatusBarText Tmp = 0 intTmp = 0 Set objUser = GetObject("WinNT://" & strDom & "/" & strUsr & ",user") If objFSO.FolderExists(objUser.HomeDirectory) Then Set homeFolder = objFSO.GetFolder(objUser.HomeDirectory) If homeFolder.Size <> 0 AND ERR.Number = 0 Then intTmp = homeFolder.Size Tmp = FormatNumber(intTmp, Decimals, LeadingZero, NegNumInParen, GroupDigits) objXL.Cells(oRow,7).Value = Round((Tmp/1048576),0) Else objXL.Cells(oRow,7).Value = 0 End If End If If Err.Number <> 0 Then objXL.Cells(oRow,7).Value = "Unable To Access" Err.Clear End If Set homeFolder = Nothing oRow = oRow + 1 Loop '============================================================================= 'Script Ending Cleanup '============================================================================= With objXL.Cells(2,5) .Value = "End Time " & Time .Font.Size = 10 .font.bold=True End With With objXL.Cells(1,5) .Value = "Total User ID's " & IntCountRows-4 .Font.Size = 10 .font.bold=True End With strStatusBarText = "Finished" objXL.StatusBar = strStatusBarText Set objFSO = Nothing Set objXL = Nothing WScript.Echo "All Done!" Wscript.Quit '==================================================== 'Compare login dates between PDC and BDC '==================================================== Function CompareDate (Row) Dim Date1, Date2 Dim Column, I, str Column = CompDteCol + 1 Date1 = objXL.Cells(Row, Column).Value Column = Column + 1 For I = Column to intCountColumns Date2 = objXL.Cells(Row, Column).Value If Date1 = "Never" then 'Format Never to a Date we can compare I chose this date as it is 'unlikely that anyone has an NT4 Domain running before Date1 = "01/01/80" End If If Date2 = "Never" Then Date2 = "01/01/80" End If If CDate(Date1) <= CDate(Date2) Then 'Compare the date for the most recent Date1 = Date2 End If Column = Column + 1 Next If Date1 = "01/01/80" Then 'Turn Back to Never if the user has never logged onto the Domain CompareDate = "Never" Else CompareDate = Date1 End If End Function '==================================================== 'Configure Columns in Excel '==================================================== Function Set_Headers() With objXL.cells(1,1) .Value = "List of User Accounts and When They last logged on to the Domain." .font.size=12 .Font.ColorIndex = 1 .font.bold=True End With With objXL.Cells(2,1) .Value = "Start Date " & Date .Font.Size=10 .font.bold=True End With With objXL.Cells(2,2) .Value = "Start Time " & Time .Font.Size=10 .font.bold=True End With With objXL.Cells(2,3) .Value = "Dates shown in BOLD type are older than " & tstdate .Font.Size=10 .Font.Bold=True End With With objXL.Cells(3,1) .Columns.ColumnWidth = 20 .Value = "User ID" .Font.Size=10 .Font.Bold=True End With With objXL.Cells(3,2) .Columns.ColumnWidth = 30 .Value = "User Names" .Font.Size=10 .Font.Bold=True End With With objXL.Cells(3,3) .Columns.ColumnWidth = 9 .Value = "Status" .Font.Size=10 .Font.Bold=True End With With objXL.Cells(3,4) .Columns.ColumnWidth = 58 .Value = "Domain Profile Path" .Font.Size=10 .Font.Bold=True End With With objXL.Cells(3,5) .Columns.ColumnWidth = 59 .Value = "Terminal Server Profile Path" .Font.Size=10 .Font.Bold=True End With With objXL.Cells(3,6) .Columns.ColumnWidth = 28 .Value = "Home Folder Path" .Font.Size=10 .Font.Bold=True End With With objXL.Cells(3,7) .Columns.ColumnWidth = 20 .Value = "Home Folder Size(MB)" .Font.Size=10 .Font.Bold=True End With With objXL.Cells(3,CompDteCol) .Columns.ColumnWidth = 22 .Value = "Last Authentication Date" .Font.Size=10 .Font.Bold=True End With End Function '=====================================================
|
|