Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Enumerating the lastLogonTimestamp for LOCAL User Accounts

Status
Not open for further replies.

NiceYak

Programmer
Apr 24, 2008
6
US
I've found a million examples of how to enumerate domain user accounts to pull their last logon time but I'm trying to figure out how I could do the same for all LOCAL user accounts on servers that do not belong to a domain. I thought it would be as simple as enumerating the local users by doing a query something like this: "...ExecQuery("Select * from Win32_UserAccount Where LocalAccount = True")", but I can't track down any properties or methods that would provide the last logon time. Either I'm a dork or this is more difficult than it should be...or both.
 
You are pretty close. The results of your WMI query can be used to get the information, but you have to bind to the account using the WinNT provider and query the LastLogin property of the user. If the property happens to be null or empty, then the script may error out.

Hopefully this snip will help you...

Code:
For Each objAccount in colAccounts
	WScript.Echo "User Name: " & objAccount.Name

	Set objUser = GetObject("WinNT://" & strComputer & "/" & objAccount.Name)

	On Error Resume Next
	sLogin = objUser.LastLogin
	If Err = 0 Then
		WScript.Echo "Last Login: " & sLogin
	Else
		WScript.Echo "Last Login: Never"
	End If
	On Error GoTo 0
Next

Good Luck!

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Really raw...no real error handling

Code:
Option Explicit

Dim objNetwork : Set objNetwork = CreateObject("WScript.Network")
Dim objGroup : Set objGroup = GetObject("WinNT://.")
objGroup.Filter = Array("user")
Dim objUser
For Each objUser In objGroup
	On Error Resume Next
		WScript.Echo objUser.name & vbTab & GetObject(objUser.adsPath).LastLogin
	On Error GoTo 0
Next

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
I agree that there's more than one way to skin a cat, DM, but I don't understand the error handling comment...

PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Well the example I posted does not show the fact that there are accounts for which LastLogin is not populated and will only show those that do have a value...your example uses If Err.Number = 0 Then... which would show those that have a last login and those that do not.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Gotch... Thanks!


PSC

Governments and corporations need people like you and me. We are samurai. The keyboard cowboys. And all those other people out there who have no idea what's going on are the cattle. Mooo! --Mr. The Plague, from the movie "Hackers
 
Excellent! Thanks for your input...I hit all around the right way to do it but couldn't quite get it. You guys rock.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top