Solved

VBScript Query OU for Computer OS and Last User to Login

Posted on 2010-09-01
6
992 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
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
VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
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…

856 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