Solved

VBScript Query OU for Computer OS and Last User to Login

Posted on 2010-09-01
6
1,001 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 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
Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
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

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!

Question has a verified solution.

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

This article runs through the process of deploying a single EXE application selectively to a group of user.
This article shows the method of using the Resultant Set of Policy Tool to locate Group Policy that applies a particular setting.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

734 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