使用 Powershell 将 DirectoryServices.ResultPropertyValueColleciton 转换为 Int

发布于 2024-12-16 01:10:55 字数 619 浏览 2 评论 0原文

我想统计每个用户的登录次数并希望显示整个登录次数。我就是这么做的

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吃不饱 2024-12-23 01:10:55

在脚本中将 $logonCounter += $person.properties.logoncount 更改为 $logonCounter += ($person.properties.logoncount)[0]

Change $logonCounter += $person.properties.logoncount to $logonCounter += ($person.properties.logoncount)[0] in your script.

温柔戏命师 2024-12-23 01:10:55

试试这个。顺便说一句,您可能需要调整 $result.PageSize 值。在我的测试中,它只给了我前 1000 个对象,因此对其进行更改以绕过此限制。要获取登录总数,请通过管道传输 Measure-Object cmdlet 的结果并指定 -Sum 开关:

$logonCount = $result | foreach {  $_.properties.logoncount } | measure -sum
$logonCount.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 :

$logonCount = $result | foreach {  $_.properties.logoncount } | measure -sum
$logonCount.sum
幻想少年梦 2024-12-23 01:10:55

你可以尝试这个双重演员:

$logonCounter +=  [int][string]$person.properties.logoncount

You can try this double cast:

$logonCounter +=  [int][string]$person.properties.logoncount
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文