广告密码中的聚会错误到期提醒

发布于 2025-01-21 05:30:10 字数 1496 浏览 1 评论 0原文

这是代码的一部分,该代码如果用户的Active Directory密码即将到期。

它曾经起作用,但是现在我突然收到了这个错误!也许是因为当地的机器语言是德语的德语日期格式,但我不知道要修复它!我真的很感谢您的帮助。

代码:

#Calculate expirering passwords and store them in an object
$today = (get-date).date

[Threading.Thread]::CurrentThread.CurrentUICulture = 'en-US';

$ExpirePasswordList =
foreach ($ADUser in $AllADUsers) {
    $PasswordLastSet          = $ADUser.PasswordLastSet
    $PasswordExpireDate       = (get-date $PasswordLastSet).AddDays($MaxPasswordAge)
    $DaysBeforePasswordchange = (New-TimeSpan -start $today -end $PasswordExpireDate).days
    
    if ($DaysBeforePasswordchange -le $WarningLevel) {
        [pscustomobject]@{
            Givenname                = $ADUser.GivenName
            Surname                  = $ADUser.Surname
            MailAddress              = $ADUser.mail
            DaysBeforePasswordchange = $DaysBeforePasswordchange
            PasswordExpireDate       = $PasswordExpireDate 
        }
    }
}    

错误:

Get-Date : Cannot bind parameter 'Date' to the target. Exception setting "Date": "Cannot convert null to type "System.DateTime"."
At line:6 char:43
+     $PasswordExpireDate       = (get-date $PasswordLastSet).AddDays($ ...
+                                           ~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Get-Date], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.GetDateCommand

this is part of a code that reminds users if their active directory password is about to expire.

It used to work, but now I get this error out of a sudden! Maybe because the Local Machine Language is German with German Date Format, but I don't know hot to fix it! I would really appreciate the help.

The Code:

#Calculate expirering passwords and store them in an object
$today = (get-date).date

[Threading.Thread]::CurrentThread.CurrentUICulture = 'en-US';

$ExpirePasswordList =
foreach ($ADUser in $AllADUsers) {
    $PasswordLastSet          = $ADUser.PasswordLastSet
    $PasswordExpireDate       = (get-date $PasswordLastSet).AddDays($MaxPasswordAge)
    $DaysBeforePasswordchange = (New-TimeSpan -start $today -end $PasswordExpireDate).days
    
    if ($DaysBeforePasswordchange -le $WarningLevel) {
        [pscustomobject]@{
            Givenname                = $ADUser.GivenName
            Surname                  = $ADUser.Surname
            MailAddress              = $ADUser.mail
            DaysBeforePasswordchange = $DaysBeforePasswordchange
            PasswordExpireDate       = $PasswordExpireDate 
        }
    }
}    

The Error:

Get-Date : Cannot bind parameter 'Date' to the target. Exception setting "Date": "Cannot convert null to type "System.DateTime"."
At line:6 char:43
+     $PasswordExpireDate       = (get-date $PasswordLastSet).AddDays($ ...
+                                           ~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Get-Date], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.PowerShell.Commands.GetDateCommand

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

﹏半生如梦愿梦如真 2025-01-28 05:30:10

正如 theo 所建议的那样正确 $ passwordlastset 结果是null beacuse beacuse您未提及password> passwordlastset < /code>属性在其 - 属性 get -aduser的参数中,并插入变量$ alladusers

尝试以下PowerShell代码,它应该为您工作。

$AllADUsers=get-aduser -filter * -properties passwordlastset, passwordneverexpires | ft GivenName,Surname,mail,passwordlastset, Passwordneverexpires

$ExpirePasswordList =
foreach ($ADUser in $AllADUsers) {
    $PasswordLastSet          = $ADUser.passwordlastset
    $PasswordExpireDate       = (get-date $PasswordLastSet).AddDays($MaxPasswordAge)
    $DaysBeforePasswordchange = (New-TimeSpan -start $today -end $PasswordExpireDate).days
    
    if ($DaysBeforePasswordchange -le $WarningLevel) {
        [pscustomobject]@{
            Givenname                = $ADUser.GivenName
            Surname                  = $ADUser.Surname
            MailAddress              = $ADUser.mail
            DaysBeforePasswordchange = $DaysBeforePasswordchange
            PasswordExpireDate       = $PasswordExpireDate 
        }
    }
}    

参考: https://social.technet.microsoft.com/wiki/wiki/wiki/contents/32912.powershell-get-get-get-aDuser-to-Aduser-to-Aduser-to-aduser-最后一式和兴奋的信息

As Suggested by Theo is correct $PasswordLastSet outcome is null beacuse you didn't mentioned PasswordLastSet attribute in its -Properties parameter of Get-ADUser and taking into variable $AllADUsers.

Try with below powershell code it should work for you.

$AllADUsers=get-aduser -filter * -properties passwordlastset, passwordneverexpires | ft GivenName,Surname,mail,passwordlastset, Passwordneverexpires

$ExpirePasswordList =
foreach ($ADUser in $AllADUsers) {
    $PasswordLastSet          = $ADUser.passwordlastset
    $PasswordExpireDate       = (get-date $PasswordLastSet).AddDays($MaxPasswordAge)
    $DaysBeforePasswordchange = (New-TimeSpan -start $today -end $PasswordExpireDate).days
    
    if ($DaysBeforePasswordchange -le $WarningLevel) {
        [pscustomobject]@{
            Givenname                = $ADUser.GivenName
            Surname                  = $ADUser.Surname
            MailAddress              = $ADUser.mail
            DaysBeforePasswordchange = $DaysBeforePasswordchange
            PasswordExpireDate       = $PasswordExpireDate 
        }
    }
}    

Reference : https://social.technet.microsoft.com/wiki/contents/articles/32912.powershell-get-aduser-to-see-password-last-set-and-expiry-information-and-more.aspx

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