[Net.ServicePointManager]::SecurityProtocol = `
[Net.SecurityProtocolType]::Tls12 ;
#365 User
$emailusername = "[email protected]"
#password saved from previous ( https://pariswells.com/blog/research/how-to-save-secure-string-in-powershell )
$encrypted = Get-Content C:\scripts\ScheduledTask\securePassword.txt | ConvertTo-SecureString
$credential = New-Object System.Management.Automation.PsCredential($emailusername, $encrypted)
$inactive = Search-ADAccount -UsersOnly -AccountInactive -TimeSpan "42" | ?{$_.enabled -eq $True}
$inactive = $inactive.name | out-string
$passwordnextexpire = get-aduser -filter * -properties Name, PasswordNeverExpires | where {$_.passwordNeverExpires -eq "true" }
$passwordnextexpire = $passwordnextexpire.name | out-string
Send-MailMessage -From $emailusername -To [email protected] -Subject "Inactive User and Password Never Expire" -Body "6 weeksInactive Users `n------------------------`n $inactive`n`n Password Never Expire `n------------------------`n $passwordnextexpire" -SmtpServer smtp.office365.com -Credential $credential -Usessl -Port 587