Jump to content

Need Vb Advice - Adding Last 3 Logged In Users To Group(s) And Custom Users.


Recommended Posts

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.

'Set System to ignore errors and continueOn Error Resume Next'Declare Variable for Shell ObjectDim WshShellSet WshShell = WScript.CreateObject("Wscript.Shell")'Declare Variable for Network ObjectDim WshNetSet WshNet = WScript.CreateObject("WScript.Network")'Declare Variable for Filesystem ObjectDim WshFileSysSet WshFileSys = WScript.CreateObject("Scripting.FileSystemObject")'Set Constants for file access (read, write, append)Const ForReading = 1, ForWriting = 2, ForAppending = 8'Declare Variable for Network ObjectDim net, local, objUser2, objUser3, objDomain, UserID, ObjNetSet net = WScript.CreateObject("WScript.Network")local = net.ComputerNameobjDomain = net.UserDomainobjUser3 = net.UserNameSet UserID = objDomain & "\" & objUser3'WScript.Echo UserIDSet 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_PASSWDobjUser.Put "userFlags", objPasswordExpirationFlag objUser.SetInfoSet colAccounts = GetObject("WinNT://" & local & ",computer")Set objUser1 = colAccounts.Create("user", "CTC_ITSPRT")objUser1.SetPassword "P@ssw0rd"objUserFlags = objUser.Get("UserFlags")objUser1.SetInfoConst ADS_UF_DONT_EXPIRE_PASSWD = &h10000Set 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_PASSWDobjUser2.Put "userFlags", objPasswordExpirationFlag objUser2.SetInfoSet colAccounts = GetObject("WinNT://" & local & "")Set objGroup = colAccounts.Create("group", "mqm")objGroup.SetInfoSet colAccounts = GetObject("WinNT://" & local & "")Set objGroup = colAccounts.Create("group", "mqbrkrs")objGroup.SetInfoSet ObjGroup = GetObject("WinNT://" & local & "/mqm,group")objGroup.Add(ObjUser2.ADsPath)Set UserID = objDomain & "/" & objUser3Set 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!

Link to post
Share on other sites

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

Dim net, local, objUser, objDomain, UserID, ObjNetSet net = WScript.CreateObject("WScript.Network")local = net.ComputerNameobjUser = net.UserNameSet 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?

Link to post
Share on other sites
  • 2 weeks later...
  • 2 weeks later...

Nobody from this great community re: The VB script simply needs to take the last 3 logged in users based on the date the script is run, and dump those 3 users to 2 specific groups. The 3 users must be designated in a domain when dumped (ie. CORP\test.user).

Link to post
Share on other sites
  • 1 month later...

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

Option ExplicitDim objFSO, arrProfileAge, colFolders, objFolder, intRecord, intDomainAccts, objUser, objGroupSet 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 DateLastModifiedFor Each objFolder in colFoldersSelect Case UCase(objFolder.Name)Case "ADMINISTRATOR", "ALL USERS", "DEFAULT USER", "LOCALSERVICE", "NETWORKSERVICE"	 ' Do Nothing, exclude these profilesCase Else	 ' Get the difference in minutes between the modified date and Now and add to array	 arrProfileAge.Add DateDiff("N", objFolder.DateLastModified, Now)End SelectNextarrProfileAge.Sort() 'Sort the array ASCENDINGintRecord = 0intDomainAccts = 0' Loop until all items checked or script found 5 domain accountsDo Until intRecord = arrProfileAge.Count Or intDomainAccts = 5'Loop through the folders again and find the folders that are the least minutes or latest modifiedFor 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 IfNextintRecord = intRecord + 1 'Increment to look at next itemLoop

. 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.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...