Solved

VBScript Query OU for Computer OS and Last User to Login

Posted on 2010-09-01
6
982 Views
Last Modified: 2012-05-10
I have script that currently queries a specific OU for the computer name, Operating System, Owner, and the date it was added.

I also need to find out the the user ID of the last person to login. I just haven't been able to find a listing for the attribute. (Assuming the method I've implemented will work in this case.)

If  it's not possible to get the last logged in user, a list of user profiles that exist on the computer might work as well. Subroutine listed below.

Thanks in advance!!
Function DoRecursive(strObjectDN)

  

'Set oneLevelOU= GetObject("LDAP://" & strObjectDN)

Set objOU = GetObject("LDAP://" & strObjectDN)

objOU.Filter = Array("Computer")



Set objUser = GetObject("LDAP://" & strObjectDN)	

Set objFSO = CreateObject("Scripting.FileSystemObject")



For Each objComputer In objOU

Set objNtSecurityDescriptor = objComputer.Get("ntSecurityDescriptor")	

 

    Excel.Cells(counter,1).Value = objComputer.CN

    Excel.Cells(counter,2).Value = objComputer.OperatingSystem

    Excel.Cells(counter,3).Value = objNtSecurityDescriptor.owner

    Excel.Cells(counter,4).Value = objComputer.whenCreated

    'Excel.Cells(counter,5).Value = objComputer.LastUserToLogin **need this attribute **

    counter =counter +1    



Next

End Function

Open in new window

0
Comment
Question by:JB4375
  • 4
  • 2
6 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
Comment Utility
Hi, there is no AD attribute for that (unless a logon script were to write the username to, say, the Description, at logon), but you can use WMI to retrieve the last user to logon.

Regards,

Rob.
Function DoRecursive(strObjectDN)

  

'Set oneLevelOU= GetObject("LDAP://" & strObjectDN)

Set objOU = GetObject("LDAP://" & strObjectDN)

objOU.Filter = Array("Computer")



Set objUser = GetObject("LDAP://" & strObjectDN)	

Set objFSO = CreateObject("Scripting.FileSystemObject")



For Each objComputer In objOU

Set objNtSecurityDescriptor = objComputer.Get("ntSecurityDescriptor")	

 

    Excel.Cells(counter,1).Value = objComputer.CN

    Excel.Cells(counter,2).Value = objComputer.OperatingSystem

    Excel.Cells(counter,3).Value = objNtSecurityDescriptor.owner

    Excel.Cells(counter,4).Value = objComputer.whenCreated

    Excel.Cells(counter,5).Value = GetLastUserToLogin

    counter =counter +1    



Next

End Function



Function GetLastUserToLogin(strComputer)

	If Ping(strComputer) = True Then

		strUserName = ""

		On Error Resume Next

		Set objWMIService = GetObject("winmgmts:" _

		    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

		Set colComputer = objWMIService.ExecQuery _

		    ("Select UserName from Win32_ComputerSystem")

		For Each objComputer in colComputer

			strUserName = objComputer.UserName

		Next

		If Err.Number <> 0 Then strUserName = "WMI ERROR"

		Err.Clear

		On Error GoTo 0

	Else

		strUserName = "OFFLINE"

	End If

	GetLastUserToLogin = strUserName

End Function



Function Ping(strComputer)

	Dim objShell, boolCode

	Set objShell = CreateObject("WScript.Shell")

	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)

	If boolCode = 0 Then

		Ping = True

	Else

		Ping = False

	End If

End Function

Open in new window

0
 
LVL 1

Author Closing Comment

by:JB4375
Comment Utility
Rob,
Sorry for the delayed response it was a holiday weekend. Thanks for the help on this!!

JB
0
 
LVL 1

Author Comment

by:JB4375
Comment Utility
Rob,
Just one issue: I'm getting VBScript Runtime error: Object doesn not support this property or method: 'objComputer.GetLastUserToLogin.
I've declared ObjWMIService, objComputer, colComputer, strComputer as well.
 
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Author Comment

by:JB4375
Comment Utility
Scratch that..... realized my mistake.
Changed line 17 from objComputer.Get LastUserToLogin to:
Excel.Cells(counter,5).Value = GetLastUserToLogin

Now the error is:  Wrong number of arguments or invalid property assignment: 'GetLastUserToLogin'.
0
 
LVL 65

Expert Comment

by:RobSampson
Comment Utility
Hi JB, looks like it's been resolved in your related question.  Good to see.

Rob.
0
 
LVL 1

Author Comment

by:JB4375
Comment Utility
Yessir!!
Thanks for checking back,
JB
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

I know all systems administrator at some time or another has had to create a script to copy file from a server share to a desktop. Well now there is an easy way to do this in Group Policy. Using Group policy preferences is not hard. The first thing …
Mapping Drives using Group policy preferences Are you still using old scripts to map your network drives if so this article will show you how to get away for old scripts and move toward Group Policy Preference for mapping them. First things f…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now