Skip to content

Instantly share code, notes, and snippets.

@fuzzysteve
Created September 29, 2016 09:48
Show Gist options
  • Save fuzzysteve/5dacc6c34238ed142eda51b319b30da1 to your computer and use it in GitHub Desktop.
Save fuzzysteve/5dacc6c34238ed142eda51b319b30da1 to your computer and use it in GitHub Desktop.
Office 365 password expiry email
# Please Configure the following variables....
$smtpServer="###########SMTP SERVER GOES HERE###########"
$expireindays = 10
$from = "############EMAIL GOES HERE##############"
$logging = "Enabled" # Set to Disabled to Disable Logging
$logFile = "c:\scripts\logs\o365passwordreminder.csv" # ie. c:\mylog.csv
$date = Get-Date -format ddMMyyyy
#
###################################################################################################################
$pass = cat c:\scripts\secure\365securestring.txt | convertto-securestring
$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist "############Admin email to log into o365 goes here##############",$pass
# Connect to Office 365
connect-MsolService -Credential $mycred
# Get Users From MSOL where Passwords Expire
$users = get-msoluser | where { $_.PasswordNeverExpires -eq $false }
$domain = Get-MSOLDomain | where {$_.IsDefault -eq $true }
#$maxPasswordAge = ((Get-MsolPasswordPolicy -domain $domain.Name).ValidityPeriod).ToString()
$maxPasswordAge = "90"
# Check Logging Settings
if (($logging) -eq "Enabled")
{
# Test Log File Path
$logfilePath = (Test-Path $logFile)
if (($logFilePath) -ne "True")
{
# Create CSV File and Headers
New-Item $logfile -ItemType File
Add-Content $logfile "Date,Name,EmailAddress,DaystoExpire,ExpiresOn"
}
} # End Logging Check
# Get Users From AD who are Enabled, Passwords Expire and are Not Currently Expired
$fulllist="Passwords about to Expire"+"`n"
# Process Each User for Password Expiry
foreach ($user in $users)
{
$Name = $user.DisplayName
$emailaddress = $user.UserPrincipalName
$passwordSetDate = $user.LastPasswordChangeTimeStamp
$expireson = $passwordsetdate + $maxPasswordAge
$today = (get-date)
$daystoexpire = (New-TimeSpan -Start $today -End $expireson).Days
# Set Greeting based on Number of Days to Expiry.
# Check Number of Days to Expiry
$messageDays = $daystoexpire
if (($messageDays) -ge "1")
{
$messageDays = "in " + "$daystoexpire" + " days."
}
else
{
$messageDays = "today."
}
# Email Subject Set Here
$subject="Your email password will expire $messageDays"
# Email Body Set Here, Note You can use HTML, including Images.
$body ="
Dear $name,
<p> Your Email Password will expire $messageDays<br>
To change your password, go to portal.office.com and change it. <br>
If you don't know your current password, please contact IT Support for assistance.
<p>Thanks, <br>
</P>"
if (($daystoexpire -ge "0") -and ($daystoexpire -lt $expireindays))
{
$fulllist += $emailaddress + " , " + $messageDays + "`n"
Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -body $body -bodyasHTML -priority High
Add-Content $logfile "$emailaddress,emailed"
} # End Send Message
Add-Content $logfile "$date,$Name,$emailaddress,$daystoExpire,$expireson"
} # End User Processing
Send-Mailmessage -smtpServer $smtpServer -from $from -to $from -subject "Password Expiry" -body $fulllist
# End
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment