0

我正忙于创建一个查询,该查询将识别密码即将到期的用户。

但是,我在查询中遇到了一个小错误;它似乎返回了正确的结果,但抛出了 5 次错误,这似乎与 5 个用户帐户相对应。

我得到的错误是;

You cannot call a method on a null-valued expression.
At C:\common\SCRIPTS\PASSWORD_EXPIRY_CUSTOM.ps1:9 char:116
+ ... ires | where { $_.PasswordExpires -gt $null -and `
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

我可以在它卡住的帐户中看到的唯一区别是它们被设置为在下次登录时更改密码,因此 PasswordExpires 属性实际上是 $null,但即使将其过滤掉似乎也无法修复错误。

我正在运行的代码:

$date = Get-Date

$FirstWarning = $date.AddDays(2).ToShortDateString()
$SecondWarning = $date.AddDays(7).ToShortDateString()
$LastWarning = $date.AddDays(15).ToShortDateString()



$Users = Get-QADUser -SizeLimit 0 -SearchRoot 'mydomain.com/users' -IncludedProperties PasswordExpires | 
             Where { 
                 $_.PasswordExpires -gt $null -and `
                ($_.PasswordExpires).ToShortDateString() -eq "$FirstWarning" -or `
                ($_.PasswordExpires).ToShortDateString() -eq "$SecondWarning" -or `
                ($_.PasswordExpires).ToShortDateString() -eq "$LastWarning" 
             }

有人可以帮我吗?我要拔头发了;即使这可能是一个简单的解决方案。

4

1 回答 1

1

为什么 sizeLimit = 0?

“ -SizeLimit 要返回的最大项目数(默认=1000)”

这可能会导致 $null 被传送到您的处理部分,然后会暴露一个错误。我认为它需要括号来包围“或”子句。我也会用“-ne $null”替换“-gt $null”。

$Users = Get-QADUser -SizeLimit 0 -SearchRoot 'mydomain.com/users' -IncludedProperties PasswordExpires | where { $_.PasswordExpires -ne $null -and `
( `
  ($_.PasswordExpires).ToShortDateString() -eq "$FirstWarning" -or `
  ($_.PasswordExpires).ToShortDateString() -eq "$SecondWarning" -or `
  ($_.PasswordExpires).ToShortDateString() -eq "$LastWarning" `
) `
}
于 2014-04-15T02:08:33.453 回答