Looking for help and also for advice, i can’t put my head around
Need send user notification:14, 7 and 1 day before ps expared
also how i can create .csv for each day email was sent
this is my code, its not working properly, but i think maybe you can explain me how to fix it
also sime time i received error on this line
$daystoexpire = (New-TimeSpan -Start "$today" -End "$expireson").Days
and error:
New-TimeSpan : Cannot bind parameter ‘End’. Cannot convert value “180.00:00:00” to type “System.DateTime”. Error: “String was not recognized as a valid DateTime.”
- … $daystoexpire = (New-TimeSpan -Start “$today” -End “$expireson”).Days
Code
$FourteenDayWarnDate = 14
$SevenDayWarnDate = 7
$OneDayWarnDate = 1
$Searchbase = "OU=...,DC=..."
$filter = {(Enabled -eq $True) -and (PasswordNeverExpires -eq $False) -and (PasswordExpired -eq $false)}
$adProperties = @("Name", 'PasswordNeverExpires', 'PasswordExpired', 'PasswordLastSet', 'EmailAddress')
$users =
Get-ADUser -Searchbase $Searchbase -Filter $filter -Properties $adProperties
foreach ($user in $users)
{
$Name = $user.Name
$emailaddress = $user.emailaddress
$passwordSetDate = $user.PasswordLastSet
$maxPasswordAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
$expireson = $passwordsetdate + $maxPasswordAge
$today = (get-date)
$daystoexpire = (New-TimeSpan -Start $today -End $Expireson).Days
$subject="..."
$body =@"
"@
if ($daystoexpire -lt $FourteenDayWarnDate)
{
#Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -body $body -bodyasHTML -priority High
Write-Host "$($user.Name): Email notification sent" -BackgroundColor DarkGreen
}
if ($daystoexpire -lt $SevenDayWarnDate)
{
#Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -body $body -bodyasHTML -priority High
Write-Host "$($user.Name): Email notification sent" -BackgroundColor DarkGreen
}
if ($daystoexpire -lt $OneDayWarnDate)
{
#Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -body $body -bodyasHTML -priority High
Write-Host "$($user.Name): Email notification sent" -BackgroundColor DarkGreen
}
else {}
}
Thanks