'Get-Date Error in AD Password Expire reminder
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
Solution 1:[1]
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
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | RahulKumarShaw-MT |