Results 1 to 10 of 10

Thread: Need VB advice - adding last 3 logged in users to group(s) and custom users.

  1. #1

    Thread Starter
    Junior Member thudo's Avatar
    Join Date
    Sep 2008
    Location
    North Toronto, Canada
    Posts
    28

    Question Need VB advice - adding last 3 logged in users to group(s) and custom users.

    Essentially, I've created the following to add to our environment three users to a desktop: BKRADM, CTC_ITSPRT, MUSR_MQADMIN then make two groups:
    mqm and mqbrkrs, then put those three users into those two groups.

    My problem is:

    Based on the script below how do I..

    1) Add to groups mqm and mqbrkrs the last three logged in users to that local machine I run the vb script on and..
    2) Manually add in domain users where I want to add to, say, the mqm group a user CORP\tom.jones and CORP\bertha.matt (both users are in our network domain and not local users to the computer so its critical to have the CORP\ there also). I can add local users but no idea how to add CORP\ domain users.

    Code:
    'Set System to ignore errors and continue
    On Error Resume Next
    
    'Declare Variable for Shell Object
    Dim WshShell
    Set WshShell = WScript.CreateObject("Wscript.Shell")
    'Declare Variable for Network Object
    Dim WshNet
    Set WshNet = WScript.CreateObject("WScript.Network")
    'Declare Variable for Filesystem Object
    Dim WshFileSys
    Set WshFileSys = WScript.CreateObject("Scripting.FileSystemObject")
    'Set Constants for file access (read, write, append)
    Const ForReading = 1, ForWriting = 2, ForAppending = 8
    
    'Declare Variable for Network Object
    Dim net, local, objUser2, objUser3, objDomain, UserID, ObjNet
    Set net = WScript.CreateObject("WScript.Network")
    local = net.ComputerName
    objDomain = net.UserDomain
    objUser3 = net.UserName
    Set UserID = objDomain & "\" & objUser3
    'WScript.Echo UserID
    
    Set colAccounts = GetObject("WinNT://" & local & ",computer")
    Set objUser = colAccounts.Create("user", "BKRADM")
    objUser.SetPassword "P@ssw0rd"
    objUserFlags = objUser.Get("UserFlags")
    'Password will never expire!
    objPasswordExpirationFlag = objUserFlags OR ADS_UF_DONT_EXPIRE_PASSWD
    objUser.Put "userFlags", objPasswordExpirationFlag 
    objUser.SetInfo
    
    Set colAccounts = GetObject("WinNT://" & local & ",computer")
    Set objUser1 = colAccounts.Create("user", "CTC_ITSPRT")
    objUser1.SetPassword "P@ssw0rd"
    objUserFlags = objUser.Get("UserFlags")
    objUser1.SetInfo
    
    Const ADS_UF_DONT_EXPIRE_PASSWD = &h10000
    Set colAccounts = GetObject("WinNT://" & local & ",computer")
    Set objUser2 = colAccounts.Create("user", "MUSR_MQADMIN")
    objUser2.SetPassword "P@ssw0rd"
    objUserFlags = objUser.Get("UserFlags")
    'Password will never expire!
    objPasswordExpirationFlag = objUserFlags OR ADS_UF_DONT_EXPIRE_PASSWD
    objUser2.Put "userFlags", objPasswordExpirationFlag 
    objUser2.SetInfo
    
    Set colAccounts = GetObject("WinNT://" & local & "")
    Set objGroup = colAccounts.Create("group", "mqm")
    objGroup.SetInfo
    
    Set colAccounts = GetObject("WinNT://" & local & "")
    Set objGroup = colAccounts.Create("group", "mqbrkrs")
    objGroup.SetInfo
    
    Set ObjGroup = GetObject("WinNT://" & local & "/mqm,group")
    objGroup.Add(ObjUser2.ADsPath)
    
    Set UserID = objDomain & "/" & objUser3
    Set ObjNet = GetObject("WinNT://" & objDomain & "/" & objUser3 & ",user")
    Set ObjGroup = GetObject("WinNT://" & local & "/mqbrkrs,group")
    objGroup.Add(ObjUser2.ADsPath)
    objGroup.Add(ObjNet.ADsPath)
    objGroup.Add(ObjUser.ADsPath)
    objGroup.Add(ObjUser1.ADsPath)
    
    Set objUser = GetObject("WinNT://" & objDomain & "/" & objUser2)
    Set ObjGroup = GetObject("WinNT://" & local & "/mqm,group")
    objGroup.Add(ObjUser2.ADsPath)
    objGroup.Add(ObjNet.ADsPath)
    objGroup.Add(ObjUser.ADsPath)
    objGroup.Add(ObjUser1.ADsPath)
    Thanks VB forums community!

  2. #2

    Thread Starter
    Junior Member thudo's Avatar
    Join Date
    Sep 2008
    Location
    North Toronto, Canada
    Posts
    28

    Re: Need VB advice - adding last 3 logged in users to group(s) and custom users.

    Any one?

  3. #3

    Thread Starter
    Junior Member thudo's Avatar
    Join Date
    Sep 2008
    Location
    North Toronto, Canada
    Posts
    28

    Re: Need VB advice - adding last 3 logged in users to group(s) and custom users.

    Found out how to get it to work adding domain users to the specific groups. Script looks like:

    Code:
    Dim net, local, objUser, objDomain, UserID, ObjNet
    Set net = WScript.CreateObject("WScript.Network")
    local = net.ComputerName
    objUser = net.UserName
    Set objGroup = GetObject("WinNT://" & local & "/<group to add user to>")
    Set objUser = GetObject("WinNT://<our domain>/<user to add>")
    objGroup.Add(objUser.ADsPath)
    Now I need a script to add the last three logged in users to the PC to a group rather than fixed names as above. Anyone?

  4. #4

    Thread Starter
    Junior Member thudo's Avatar
    Join Date
    Sep 2008
    Location
    North Toronto, Canada
    Posts
    28

    Re: Need VB advice - adding last 3 logged in users to group(s) and custom users.

    Anyone have a simple script to pick up the last 3 logged in users to the PC then transfer them to a local security group I chose?

  5. #5

    Thread Starter
    Junior Member thudo's Avatar
    Join Date
    Sep 2008
    Location
    North Toronto, Canada
    Posts
    28

    Re: Need VB advice - adding last 3 logged in users to group(s) and custom users.

    Anyone?

  6. #6

    Thread Starter
    Junior Member thudo's Avatar
    Join Date
    Sep 2008
    Location
    North Toronto, Canada
    Posts
    28

    Re: Need VB advice - adding last 3 logged in users to group(s) and custom users.

    Noone from this great community can figure this one out? I can't seem to put this VB script together to dump the last 3 users logged into the box into specific groups.

  7. #7
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,537

    Re: Need VB advice - adding last 3 logged in users to group(s) and custom users.

    you seem to have everything you need.... I fail to see the problem.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8

    Thread Starter
    Junior Member thudo's Avatar
    Join Date
    Sep 2008
    Location
    North Toronto, Canada
    Posts
    28

    Re: Need VB advice - adding last 3 logged in users to group(s) and custom users.

    Well the problem is I cannot get the final script for calling the LastModified attrib in Document and Settings (where the user names are stored).I can't get the logic behind the script.

  9. #9

    Thread Starter
    Junior Member thudo's Avatar
    Join Date
    Sep 2008
    Location
    North Toronto, Canada
    Posts
    28

    Re: Need VB advice - adding last 3 logged in users to group(s) and custom users.

    With the help of a gracious online colleague I have this now:

    Code:
    Option Explicit
    Dim objFSO, arrProfileAge, colFolders, objFolder, intRecord, intDomainAccts
    Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
    Set arrProfileAge = CreateObject("System.Collections.ArrayList")
    Set colFolders = objFSO.GetFolder("C:\Documents and Settings").SubFolders
    ' Loop through the folders to get the DateLastModified
    For Each objFolder in colFolders
    Select Case UCase(objFolder.Name)
         Case "ADMINISTRATOR", "ALL USERS", "DEFAULT USER", "LOCALSERVICE", "NETWORKSERVICE"
             ' Do Nothing, exclude these profiles
         Case Else
             ' Get the difference in minutes between the modified date and Now and add to array
             arrProfileAge.Add DateDiff("N", objFolder.DateLastModified, Now)
    End Select
    Next
    
    arrProfileAge.Sort() 'Sort the array ASCENDING
    
    intRecord = 0
    intDomainAccts = 0
    ' Loop until all items checked or script found 5 domain accounts
    
    Do Until intRecord = arrProfileAge.Count Or intDomainAccts = 5
    'Loop through the folders again and find the folders that are the least minutes or latest modified
    For Each objFolder in colFolders
         If DateDiff("N", objFolder.DateLastModified, Now) = arrProfileAge(intRecord) Then
              'Check to see if the account is local or domain
              If Verify_Domain_User(objFolder.Name) = True Then
                 WScript.Echo objFolder.Name
                 'Add your code to add to a group
                 intDomainAccts = intDomainAccts + 1 'Increment because domain account found
              End If
         End If
    Next
    intRecord = intRecord + 1 'Increment to look at next item
    Loop
    
    Function Verify_Domain_User(user_name)
    'On Error Resume Next
    Dim objConnection, objCommand, objRecordSet
    Const ADS_SCOPE_SUBTREE = 2
    Set objConnection = CreateObject("ADODB.Connection")
    Set objCommand =   CreateObject("ADODB.Command")
    objConnection.Provider = "ADsDSOObject"
    objConnection.Open "Active Directory Provider"
    Set objCommand.ActiveConnection = objConnection
    objCommand.Properties("Page Size") = 1000
    objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
    objCommand.CommandText = "SELECT ADsPath FROM 'LDAP://dc=corp,dc=ad,dc=ctc' " & _
                              "WHERE objectCategory='user' " & _
                              "AND Name='" & user_name & "'"
    
    Set objRecordSet = objCommand.Execute
    If objRecordSet.EOF Then
         Verify_Domain_User = False
    Else
         Verify_Domain_User = True
    
    Set objUser = GetObject("WinNT://CORP/" & "C:\Documents and Settings")
    Set objGroup = GetObject("WinNT://./mqm")
    objGroup.Add(objUser.ADsPath)
    Set objGroup = GetObject("WinNT://./mqbrkrs")
    objGroup.Add(objUser.ADsPath)
    
    End If
    
    End Function
    My specifics are the full domain: corp.ad.ctc (case doesn't matter I gather?), user from CORP, the Document and Settings path (not sure if its supposed to be THUDO as this is meant to be generic to all machine names no matter the desktop), and the two groups.

    However, when I run above nothing populates in both those security groups.

  10. #10

    Thread Starter
    Junior Member thudo's Avatar
    Join Date
    Sep 2008
    Location
    North Toronto, Canada
    Posts
    28

    Re: Need VB advice - adding last 3 logged in users to group(s) and custom users.

    Update.. for the community here is my final script that works..

    Code:
    Option Explicit
    Dim objFSO, arrProfileAge, colFolders, objFolder, intRecord, intDomainAccts, objUser, objGroup
    Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
    Set arrProfileAge = CreateObject("System.Collections.ArrayList")
    Set colFolders = objFSO.GetFolder("C:\Documents and Settings").SubFolders
    ' Loop through the folders to get the DateLastModified
    For Each objFolder in colFolders
    Select Case UCase(objFolder.Name)
    Case "ADMINISTRATOR", "ALL USERS", "DEFAULT USER", "LOCALSERVICE", "NETWORKSERVICE"
         ' Do Nothing, exclude these profiles
    Case Else
         ' Get the difference in minutes between the modified date and Now and add to array
         arrProfileAge.Add DateDiff("N", objFolder.DateLastModified, Now)
    End Select
    Next
    arrProfileAge.Sort() 'Sort the array ASCENDING
    intRecord = 0
    intDomainAccts = 0
    ' Loop until all items checked or script found 5 domain accounts
    Do Until intRecord = arrProfileAge.Count Or intDomainAccts = 5
    'Loop through the folders again and find the folders that are the least minutes or latest modified
    For Each objFolder in colFolders
        If DateDiff("N", objFolder.DateLastModified, Now) = arrProfileAge(intRecord) Then
             'Check to see if the account is local or domain
             On Error Resume Next
             Set objUser = GetObject("WinNT://CORP/" & objFolder.Name)
             If Err.Number = 0 Then
                 Set objGroup = GetObject("WinNT://./mqm")
                 objGroup.Add(objUser.ADsPath)
                 Set objGroup = GetObject("WinNT://./mqbrkrs")
                 objGroup.Add(objUser.ADsPath)
                 intDomainAccts = intDomainAccts + 1 'Increment because domain account found
             End If
             On Error GoTo 0
        End If
    Next
    intRecord = intRecord + 1 'Increment to look at next item
    Loop
    . The above takes the last many users logged into the PC using C:\documents and settings as the LastModifiedDate guide then puts them into two local security groups. Our domain is called CORP.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width