Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Issue with an If statement

$
0
0

Hi Everyone, id love some help with the below script if anyone can shed some light. Im sure its something simple im not looking at. The script emails users in AD whos accounts are expiring within 14 days and sends doco on how to change their passwords. I then wanted to put an IF statement in there which emails me from 7 days with the logs.. Im sure its something basic in the syntax.. i can get it all to work untill the second IF statement.

#Add the Quest PowerShell snapin
Add-PsSnapIn Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue
Get-QADUser -SizeLimit 0 | Select-Object samAccountName,mail,PasswordStatus | Where-Object {$_.PasswordStatus -ne "Password never expires" -and $_.PasswordStatus -ne "Expired" -and $_.PasswordStatus -ne "User must change password at next logon." -and $_.mail -ne $null} |
ForEach-Object {
  $today = Get-Date
  $logdate = Get-Date -format yyyyMMdd
  $samaccountname = $_.samAccountName
  $mail = $_.mail
  $passwordstatus = $_.PasswordStatus
  $passwordexpiry = $passwordstatus.Replace("Expires at: ","")
  $passwordexpirydate = Get-Date $passwordexpiry
  $daystoexpiry = ($passwordexpirydate - $today).Days
  $smtpserver = "192.XXX.XXX.6"
  $attachment = "\\test\it$\PWD expiry reports\Updating the password on your Mobile and or Tablet device.docx"
  $emailFrom = "reports@test.com"
  $body = "Please change your password to prevent loss of access to Curwoods Systems. "
  $body += "Attached is the relevant documentation to change the password on your mobile and tablet device. If you are unable to change your password, please submit a  request or contact Help Desk on ext 3333 "
  if ($daystoexpiry -lt 3 ) {
    $emailTo = "$mail"
    $subject = "**PASSWORD CHANGE REMINDER** Your Network password will expire in $daystoexpiry day(s) please change your password."   
    Send-MailMessage -To $emailTo -From $emailFrom -Subject $subject -Body $body -Attachments $attachment -SmtpServer $smtpserver
    Write-Host "Email was sent to $mail on $today"
    Add-Content C:\Windows\SYSVOL\sysvol\test\scripts\Scripts\Password_expiry\Logs\maillog$logdate.txt  "Email was sent to $mail on $today"
  }
}
if ($daystoexpiry -lt 3 ) {
Send-MailMessage -To "ryan@test.com" -From "reports@test.com" -Subject "Password change log for $today" -Body "This is the log from $today" -Attachments "C:\Windows\SYSVOL\sysvol\test\scripts\Scripts\Password_expiry\Logs\maillog$logdate.txt" -SmtpServer $smtpserver   
}


Viewing all articles
Browse latest Browse all 6937

Trending Articles