I would like to count the number of logons of each user and would like to show the whole logons. I did that like that
$search = new-object DirectoryServices.DirectorySearcher([ADSI]"")
$search.filter = "(objectClass=user)"
$result = $search.FindAll()
#amount of User in AD
Write-Host Amount of user: $result.Count
#CountLogon
$logonCounter = 0
foreach($person in $result){
$logonCounter += $person.properties.logoncount
}
Write-host Number of Logons: $logonCounter
When I run this script I get a
Cannot convert the "System.CirectoryServices.ResultPropertyValueCollection" value of type "System.DirectoryServices.ResultPropertyValueCollection" to type System.Int32"
Try this. By the way, you may have to adjust the $result.PageSize value. In my testings it gave me just the first 1000 objects, so change it to bypass this limitation. To get the total amount of logins, pipe the results of the Measure-Object
cmdlet and specify the -Sum switch :
$logonCount = $result | foreach { $_.properties.logoncount } | measure -sum
$logonCount.sum