?
Solved

VBScript Query OU for Computer OS and Last User to Login

Posted on 2010-09-01
6
Medium Priority
?
1,013 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 33581726
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
ID: 33617762
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
ID: 33629657
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 1

Author Comment

by:JB4375
ID: 33629842
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
ID: 33632194
Hi JB, looks like it's been resolved in your related question.  Good to see.

Rob.
0
 
LVL 1

Author Comment

by:JB4375
ID: 33635921
Yessir!!
Thanks for checking back,
JB
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Had a business requirement to store the mobile number in an environmental variable. This is just a quick article on how this was done.
This process allows computer passwords to be managed and secured without using LAPS. This is an improvement on an existing process, enhanced to store password encrypted, instead of clear-text files within SQL
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…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Suggested Courses

800 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