Solved

VBScript Query OU for Computer OS and Last User to Login

Posted on 2010-09-01
6
984 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
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 Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

895 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

16 Experts available now in Live!

Get 1:1 Help Now