使用 Powershell 将 DirectoryServices.ResultPropertyValueColleciton 转换为 Int
我想统计每个用户的登录次数并希望显示整个登录次数。我就是这么做的
$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
当我运行这个脚本时,我得到了
无法转换 “System.CirectoryServices.ResultPropertyValueCollection”类型的值 键入“System.DirectoryServices.ResultPropertyValueCollection” 系统.Int32"
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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在脚本中将
$logonCounter += $person.properties.logoncount
更改为$logonCounter += ($person.properties.logoncount)[0]
。Change
$logonCounter += $person.properties.logoncount
to$logonCounter += ($person.properties.logoncount)[0]
in your script.试试这个。顺便说一句,您可能需要调整 $result.PageSize 值。在我的测试中,它只给了我前 1000 个对象,因此对其进行更改以绕过此限制。要获取登录总数,请通过管道传输
Measure-Object
cmdlet 的结果并指定 -Sum 开关: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 :你可以尝试这个双重演员:
You can try this double cast: