Script to get local user, description, last logon,Group Membership for dummys

Author Message
minakovic

  • Total Posts : 3
  • Scores: 0
  • Reward points : 0
  • Joined: 11/30/2006
  • Status: offline
Script to get local user, description, last logon,Group Membership for dummys Thursday, February 15, 2007 6:12 AM (permalink)
0
Option Explicit
'*******************************************************************************
'*                                                                                                                                                          *
'*   This Script list all the Users, their description, their Group membership                                        *
'* and the last time they logged on the computer, in a ~ delimited text file                                       *
'*       for easy import into a database or an Excel spreadsheet.                                                        *
'*                                                                                                                                                          *
'*   Tested on Windows XP, Windows 2000 Server and Windows 2003 (stand alone)                        *
'*******************************************************************************
' Copyright (c) 2007 Vicky Desjardins
' Version 2.1 - Montreal, February 15, 2007
'
' You have a royalty-free right to use, modify, reproduce, and
' distribute this script file in any way you find useful, provided that
' you agree that the copyright owner above has no warranty, obligations,
' or liability for such use.
Dim colCSItems
Dim dtmEventDate
Dim Groups
Dim Message
Dim objFSO
Dim objStream
Dim objConnection
Dim objCommand
Dim objRecordSet
Dim objComputer
Dim objCSItem
Dim objWMIService
Dim PrimaryDate
Dim strComputer
Dim strDateLastlog
Dim strGenerated
Dim strLine
Dim sComputer
Dim Utilisateur
Dim UserID
Dim OsVer
Dim objWMI
Dim objItem
Dim colItems
Dim Apprun
On error resume next
'*******************************************************************************
'  There is 4 different ways to retrieve User's information Please select one.                                    *
'                                                                                                                                                           *
'                1  Retrieve all information.                                                                                                 *
'                2  Retrieve all except last logon.                                                                                        *
'                3  Retrieve all except Group Membership.                                                                         *
'                4  Retrieve UserID, name and description only.                                                                *
'                                                                                                                                                           *
'PS: Getting the last logon time is very long to run since it has to query the                                     *
' entire Security log. Please be patient or use the scheduler.                                                            *
'*******************************************************************************
'===========================================================
Apprun = 1
'===========================================================
'*******************************************************************************
'*                                                                                                                                                          *
'*    Creating the text file where the results are going to be stored.                                                  *
'*    You can change the name or location of the file as you wish.                                                      *
'*                                                                                                                                                          *
'*******************************************************************************
Set objFSO = createobject("scripting.filesystemobject")
Set objStream = objFSO.CreateTextFile("C:\RetrieveUserSecurity.txt", True)
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
'******************************************************************************
'*                                                                                                                                                        *
'*                Test the OS to see if running windows 2000                                                                  *
'*                                                                                                                                                        *
'******************************************************************************
strComputer = "."
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMI.ExecQuery("SELECT * FROM Win32_OperatingSystem",,48)
 For Each objItem in colItems
      OsVer = objItem.Caption
 Next
If OsVer = "Microsoft Windows 2000 Server" then
     Set objComputer = CreateObject("WScript.Network")
     sComputer = objComputer.ComputerName
Else
     Set objComputer = CreateObject("Shell.LocalMachine")
     sComputer = objComputer.MachineName
End if

' ******************************************************************************
' *                                                                                                                                                        *
' *            Creating Titles Depending on the information requested                                                   *
' *                                                                                                                                                        *
' ******************************************************************************                                                                           
StrLine = sComputer
objStream.Writeline strLine
strLine= ""
Select Case Apprun
       Case "1"
            ' This Title line is for retrieving all the user informations
            StrLine = "UserID~Full Name~Description~Last Logon~Group Membership"
       Case "2"
            ' This Title line is for user informations without last logon time
            StrLine = "UserID~Full Name~Description~Group Membership"
       Case "3"
            ' This Title line is for user informations without Group Membeship
            StrLine = "UserID~Full Name~Description~Last Logon"
       Case "4"
            ' This Title line is for user informations only
            StrLine = "UserID~Full Name~Description"
End Select
objStream.Writeline strLine
strLine= ""
' ******************************************************************************
' *                                                                                                                                                        *
' *                 Main Module Query computer for users                                                                         *
' *                                                                                                                                                        *
' ******************************************************************************
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" & _
  "{impersonationLevel=impersonate}!\\" & strComputer & "\root\CIMV2")
  Set colCSItems = objWMIService.ExecQuery("SELECT * FROM Win32_UserAccount")
 For Each objCSItem In colCSItems
  Utilisateur = objCSItem.Name
  strLine = strLine & objCSItem.Name & "~"
  strLine = strLine & objCSItem.FullName & "~"
   strLine = strLine & objCSItem.Description & "~"
    PrimaryDate = 19700830074757
  UserID = sComputer & "\\" & Utilisateur
 
    Select Case Apprun
           Case "1"
               strDateLastlog = Lastlog(UserID)
               If PrimaryDate = strDateLastlog then
                  strLine = strLine & "Never Logged on "
                  strLine = strLine & "~"
               Else
                  dtmEventDate = strDateLastlog
                  strGenerated = WMIDateStringToDate(dtmEventDate)
                  strLine = strLine & "Last loggin : " & strGenerated
                  strLine = strLine & "~"
               End If
               Groups = GroupMembership (Utilisateur)
               strLine = strLine & Groups
           Case "2"
               Groups = GroupMembership (Utilisateur)
               strLine = strLine & Groups
           Case "3"
               strDateLastlog = Lastlog(UserID)
               If PrimaryDate = strDateLastlog then
                  strLine = strLine & "Never Logged on "
                  strLine = strLine & "~"
               Else
                  dtmEventDate = strDateLastlog
                  strGenerated = WMIDateStringToDate(dtmEventDate)
                  strLine = strLine & "Last loggin : " & strGenerated
                  strLine = strLine & "~"
               End If
           End Select
  strDateLastlog = ""
  Utilisateur =""
  objStream.Writeline strLine
  strLine= ""
 Next
'*******************************************************************************
'*                                                                                                                                                          *
'*                           Convert Date Function                                                                                           *
'*                                                                                                                                                          *
'*******************************************************************************
Function WMIDateStringToDate(dtmEventDate)
    WMIDateStringToDate = CDate(Mid(dtmEventDate, 5, 2) & "/" & _
        Mid(dtmEventDate, 7, 2) & "/" & Left(dtmEventDate, 4) _
            & " " & Mid (dtmEventDate, 9, 2) & ":" & _
                Mid(dtmEventDate, 11, 2) & ":" & Mid(dtmEventDate, _
                    13, 2))
End Function
'*******************************************************************************
'*                                                                                                                                                          *
'*                     Retrieve User Last logon Function                                                                               *
'*                                                                                                                                                          *
'*******************************************************************************
Function Lastlog (StrUserID)
  Dim strComputer
  Dim TestDate
  Dim StrQuery
  Dim objWMIService
  Dim collectionItems
  Dim objItem
  Dim PreDate
 
  strComputer = "."
 TestDate = 19700830074757
  StrQuery = "select * from win32_ntlogEvent Where LogFile='Security' "
  StrQuery = StrQuery &  "and eventCode = 528 and User =  '" & StrUserID  & "'"
  Set objWMIService = GetObject("winmgmts:" & _
  "{impersonationLevel=impersonate,(Security)}!\\" & strComputer & "\root\CIMV2")
  Set collectionItems = objWMIService.ExecQuery(StrQuery)
     For Each objItem In collectionItems
     PreDate = objItem.TimeGenerated
     If TestDate <= PreDate then
     TestDate = objItem.TimeGenerated
     End if
     Next
Lastlog = TestDate
End Function
'*******************************************************************************
'*                                                                                                                                                          *
'*                       Retrieve User's Group membership                                                                            *
'*                                                                                                                                                          *
'*******************************************************************************
Function GroupMembership (UserName)
  Dim StrGroups
  Dim strComputer
  Dim Virgule
  Dim colGroups
  Dim objGroup
  Dim objUser
 
  StrGroups = ""
  strComputer = "."
  Virgule = 0
  Set colGroups = GetObject("WinNT://" & strComputer & "")
  colGroups.Filter = Array("group")
  For Each objGroup In colGroups
    For Each objUser in objGroup.Members
        If objUser.name = Username Then
           if Virgule <> 0 then StrGroups = StrGroups & "~" End if
           StrGroups = StrGroups & objGroup.Name
           Virgule = Virgule + 1
        End If
    Next
  Next
GroupMembership = StrGroups
End Function
 
#1
    gracehnc

    • Total Posts : 2
    • Scores: 0
    • Reward points : 0
    • Joined: 9/5/2007
    • Status: offline
    RE: Script to get local user, description, last logon,Group Membership for dummys Friday, September 07, 2007 3:25 AM (permalink)
    0
    Where do I specified the domain controller that I want to check?
     
    #2
      Parabellum

      • Total Posts : 233
      • Scores: 0
      • Reward points : 0
      • Joined: 11/12/2006
      • Location: UK
      • Status: offline
      RE: Script to get local user, description, last logon,Group Membership for dummys Sunday, September 09, 2007 10:14 AM (permalink)
      0
      strComputer = "."

      allthough it did specify in the subject.. "Script to get LOCAL user..."
       
      #3
        skido

        • Total Posts : 2
        • Scores: 0
        • Reward points : 0
        • Joined: 1/11/2010
        • Status: offline
        Re: RE: Script to get local user, description, last logon,Group Membership for dummys Monday, January 11, 2010 4:54 AM (permalink)
        0
        hello, anyone can help me to use this script ?? I am not a coder.

        I have these info from my system

        objCommand.CommandText = _
            "SELECT Name,displayName,mail FROM 'LDAP://DC=ddd,DC=XXX,DC=XXX,DC=com' WHERE objectCategory='user' AND memberOf='CN="& readgroup &",OU=$E74212,OU=Groups,OU=XXX,DC=XXX,DC=XXX,DC=XXX,DC=com' order by Name"
        Set objRecordSet = objCommand.Execute

        If is needed any info more, please ask

        thank you , very much


         
        #4
          MikeT1955

          • Total Posts : 1
          • Scores: 0
          • Reward points : 0
          • Joined: 7/28/2010
          • Status: offline
          Re: RE: Script to get local user, description, last logon,Group Membership for dummys Wednesday, July 28, 2010 7:05 AM (permalink)
          0
          The above script is very good.  However, when used on a machine attached to an Active Directory domain, it appears to try and find all the AD users as well as the local users.  Is there a way to tell the script to just look for local users?

          Thanks for the help,

          Mike
           
          #5

            Online Bookmarks Sharing: Share/Bookmark

            Jump to:

            Current active users

            There are 0 members and 1 guests.

            Icon Legend and Permission

            • New Messages
            • No New Messages
            • Hot Topic w/ New Messages
            • Hot Topic w/o New Messages
            • Locked w/ New Messages
            • Locked w/o New Messages
            • Read Message
            • Post New Thread
            • Reply to message
            • Post New Poll
            • Submit Vote
            • Post reward post
            • Delete my own posts
            • Delete my own threads
            • Rate post

            2000-2012 ASPPlayground.NET Forum Version 3.9