Been a while sense my last post and i'm sorry for this. I need to get better at blogging.
However, here is version 2.0 of my Office 365 password expiration script.
Keep in mind this was written to be automated. You will need to create a "cached" user name and password. Please review one of the articles
http://social.technet.microsoft.com/wiki/contents/articles/4546.working-with-passwords-secure-strings-and-credentials-in-windows-powershell.aspx
or
http://blogs.technet.com/b/heyscriptingguy/archive/2013/03/26/decrypt-powershell-secure-string-password.aspx
on secure string passwords.
Any questions feel free to ask away!
<#
.NOTES
===========================================================================
Created on:
3/31/2015 10:28 AM
Created by:
josh.acton
Filename:
===========================================================================
.DESCRIPTION
version 2.0 of Office 365 password expiring.
#>
$date = get-date -Format MMddyyyy
#Make Office 365 Connection
$emailusername = "USER ACCOUNT" #Your User account
$encrypted = 'SECURE STRING HERE' | ConvertTo-SecureString #Place your secure string from text file here
Start-Transcript -path "C:\reports\Office365_Logs\O365PasswordExpire_US_$date.log" -force -NoClobber -append
$credential = New-Object System.Management.Automation.PsCredential($emailusername, $encrypted)
#$O365Cred = Get-Credential
$O365Session = New-PSSession –ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.outlook.com/powershell -Credential $credential -Authentication Basic -AllowRedirection
Import-PSSession $O365Session -AllowClobber
Connect-MsolService –Credential $credential
$date = get-date -Format MMddyyyy
#Start-Transcript -path "C:\reports\Office365_Logs\O365PasswordExpire_US_$date.log" -IncludeInvocationHeader
#Get system Date
$CSVDate = Get-Date -Format MMddyyyy
#Sets up the CSV File to email to service-desk for passwords that are expired
$csvstart = "User,Password_last_set,Password_expired_on"
Add-Content C:\reports\O365PWExpired\BluePWExpire_$CSVDate.csv $csvstart
#Sets days of advance notice
$AdvancedWarning = "14"
$AdvancedWarning2 = "7"
$Domains = (get-msoldomain).name
ForEach ($Domain in $Domains)
{
$logadd = "----------------------------Reviewing $Domain Now----------------------------"
add-content "C:\reports\Office365_Logs\O365PasswordExpire_US_$date.log" $logadd
Write-Host $logadd
$PWPolicy = Get-MsolPasswordPolicy -domain $Domain
$NotificationDays = $PWPolicy.NotificationDays
$ValidityPeriod = $PWPolicy.ValidityPeriod
If ($NotificationDays -eq $null -and $ValidityPeriod -eq $null)
{
$logadd = "No policy set, applying defaults"
add-content "C:\reports\Office365_Logs\O365PasswordExpire_US_$date.log" $logadd
Write-Host $logadd
$NotificationDays = "14"
$ValidityPeriod = "90"
}
else
{
$logadd = "Policy has be set - $notificationdays and $ValidityPeriod"
add-content "C:\reports\Office365_Logs\O365PasswordExpire_US_$date.log" $logadd
Write-Host $logadd
}
$Users = (Get-MsolUser | where{ $_.UserPrincipalName -like "*$Domain" }).UserPrincipalName
foreach ($User in $Users)
{
#write-host "working on user $user"
$PWLastSet = (Get-MsolUser -UserPrincipalName $User).lastpasswordchangetimestamp
If ($PWLastSet -ne $null)
{
$PWPeriod = ($PWLastSet).adddays($ValidityPeriod)
$PWExpire = ($PWPeriod - [DateTime]::Now).days
If ($PWExpire -le 0)
{
$logadd = "$User password expired on $PWPeriod"
add-content "C:\reports\Office365_Logs\O365PasswordExpire_US_$date.log" $logadd
Write-Host $logadd
$Content = "$User,$pwlastset,$PWPeriod"
Add-Content C:\reports\O365PWExpired\BluePWExpire_$CSVDate.csv $Content
}
Else
{
If ($PWExpire -eq $AdvancedWarning -or - $PWExpire -eq $AdvancedWarning)
{
#in the next line, add in your exceptions.
If ($User -eq "exception1" -or $user -eq "exception2" )
{ Write-Host User is predefined to be skipped, skipping }
else
{
write-host EXPIRING! $user password will expire on $pw180 that is $pwexpire days -ForegroundColor yellow -BackgroundColor Black
$logadd = "$user password will expire on $PWPeriod that is $pwexpire days"
add-content "C:\reports\Office365_Logs\O365PasswordExpire_US_$date.log" $logadd
$Subject = "Office 365 password for $user is going to expire!"
$Body = "$user password will expire on $PWPeriod"
Send-MailMessage -to "help@servicedesk.com" -from "email@servicedesk.com" -Subject $Subject -Body $body -SmtpServer 192.168.0.1
}
}
else
{
$logadd = "$user has $pwexpire day(s) left."
add-content "C:\reports\Office365_Logs\O365PasswordExpire_US_$date.log" $logadd
Write-Host $user has $pwexpire days left. -ForegroundColor Green
}
}
}
}
}
Send-MailMessage -to "sysadmin@servicedesk.com" -From "email@servicedesk.com" -Subject "Office 365 PW expiring has ran" -SmtpServer 192.168.0.1