Click here to Skip to main content
15,880,543 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello,
I found the following code on this link https://4sysops.com/archives/track-user-logons-with-a-powershell-script/[^]
Code:
<#
Set-UserStatus.ps1
****************************************************************
* DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED *
* THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF    *
* YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, *
* DO NOT USE IT OUTSIDE OF A SECURE, TEST ENVIRONMENT.         *
****************************************************************
#>
 
Param(
[Parameter(Position=0)]
[ValidateSet("Logon","Logoff","Unknown")]
[string]$Status="Unknown"
)
 
#no spaces in the filter
[adsisearcher]$searcher="samaccountname=$env:username"
#find the current user
$find = $searcher.FindOne()
#get the user object
[adsi]$user = $find.Path
#define a string to indicate status
$note = "{0} {1} to {2}" -f (Get-Date),$status.ToUpper(),$env:computername
#update the Info user property
$user.Info=$note
#commit the change
$user.SetInfo()

I am testing it with my current AD account but does not work. I created the policy but my computer/user is not getting the policy neither. Can you guys give me a hand ? Thanks for your help.
Posted
Comments
Sergey Alexandrovich Kryukov 16-Oct-14 14:35pm    
Log on/off where? As a Windows user?
—SA
namerg 16-Oct-14 15:42pm    
Windows user....i think i figured it out, maybe you can help me with the following on the note variable: $note = $env:username.ToUpper(),$FullName.GivenName,$FullName.Surname,"{0} {1} to {2}" -f (Get-Date),$status.ToUpper(),$env:computername

I added the following:
$note | Select Username,FirsName,LastName,Date,Time,State, ComputerName | Export-Csv "C:\scripts\ActiveDirectory\clocktime.csv" -nti

But the contents of clocktime.csv has the following:
"Username","FirsName","LastName","Date","Time","State","ComputerName"
,,,,,,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900