Script per email avviso scadenza password per Exchange


Uno degli script più utilizzati per Exchange (io l'ho testato sul 2010) è quello che ricorda agli utenti che la propria password sta per scadere.
Nonostante questo però le chiamate degli utenti per i reset della propria password saranno sempre tante, proprio perchè sono UTENTI.

Questo lo script (con i riferimenti in testa), che ovviamente DOVRETE modificare in base alle vostre esigenze.


#################################################################################################################
#
# Version 1.1 May 2014
# Robert Pearman (WSSMB MVP)
# TitleRequired.com
# Script to Automated Email Reminders when Users Passwords due to Expire.
#
# Requires: Windows PowerShell Module for Active Directory
#
# For assistance and ideas, visit the TechNet Gallery Q&A Page. http://gallery.technet.microsoft.com/Password-Expiry-Email-177c3e27/view/Discussions#content
#
##################################################################################################################
# Please Configure the following variables....
$smtpServer="XXX.XXX.XXX.XXX"
$expireindays = 7
$from = "XXXXXXNOME ACCOUNTXXXXXX <xxxxxxxxxxx@xxxxxxx.it>"
$logging = "Enabled" # Set to Disabled to Disable Logging
$logFile = "c:\Password.csv" # ie. c:\mylog.csv
$testing = "Disabled" # Set to Disabled to Email Users
$testRecipient = "xxxxxxx@xxxxxxx.it"
$date = Get-Date -format ddMMyyyy
#
###################################################################################################################

# 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
Import-Module ActiveDirectory
$users = get-aduser -filter * -properties Name, PasswordNeverExpires, PasswordExpired, PasswordLastSet, EmailAddress |where {$_.Enabled -eq "True"} | where { $_.PasswordNeverExpires -eq $false } | where { $_.passwordexpired -eq $false }
$maxPasswordAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge

# Process Each User for Password Expiry
foreach ($user in $users)
{
    $Name = (Get-ADUser $user | foreach { $_.Name})
    $emailaddress = $user.emailaddress
    $passwordSetDate = (get-aduser $user -properties * | foreach { $_.PasswordLastSet })
    $PasswordPol = (Get-AduserResultantPasswordPolicy $user)
    # Check for Fine Grained Password
    if (($PasswordPol) -ne $null)
    {
        $maxPasswordAge = ($PasswordPol).MaxPasswordAge
    }

    $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 = " tra " + "$daystoexpire" + " giorni. "
    }
    else
    {
        $messageDays = "oggi."
    }

    # Email Subject Set Here
    $subject="La tua password scadra' $messageDays"

    # Email Body Set Here, Note You can use HTML, including Images.
    $body ="
    Gentile $name,
    <p> La password dell'account $emailaddress scadra' $messageDays <br>
    Per evitare problemi di accesso si prega di cambiarla il prima possibile.<br>
    <p>Grazie! <br><br>
    Questo messaggio e' generato da un sistema automatico, si prega di non rispondere a questa mail. <br>
    Per maggiori informazioni chiamare l'Help Desk al numero XXXXXXXXXXX <br>
 
    </P>"

 
    # If Testing Is Enabled - Email Administrator
    if (($testing) -eq "Enabled")
    {
        $emailaddress = $testRecipient
    } # End Testing

    # If a user has no email address listed
    if (($emailaddress) -eq $null)
    {
        $emailaddress = $testRecipient  
    }# End No Valid Email

    # Send Email Message
    if (($daystoexpire -ge "0") -and ($daystoexpire -lt $expireindays))
    {
         # If Logging is Enabled Log Details
        if (($logging) -eq "Enabled")
        {
            Add-Content $logfile "$date,$Name,$emailaddress,$daystoExpire,$expireson"
        }
        # Send Email Message
        Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject $subject -body $body -bodyasHTML -priority High

    } # End Send Message
 
} # End User Processing



# End

Per fare in modo che l'utente non riceva decine di mail prima della scadenza sarà sufficiente pianificare due script:
Il primo che controlla le scadenze delle password a 7 giorni, dunque con il valore $expireindays = 7 in questo modo verranno avvisati gli utenti che hanno la password in scadenza da 1 a 7 giorni
Questo script (ad esempio) lo pianifichiamo al mattino solo due giorni a settimana, MARTEDI e VENERDI
Il secondo che controlla la scadenza della password a 1 giorno dunque con il valore $expireindays = 1
in questo modo verranno avvisati gli utenti che hanno la password in scadenza "domani". 
Questo script invece lo pianifichiamo tutti gli altri giorni in cui non viene eseguito il primo script, dunque LUNEDI MERCOLEDI GIOVEDI SABATO E DOMENICA.

In questo modo l'utente riceverà due volte a settimana una mail che lo avvisa che tra 7,6,5,4,3,2 o 1 giorno gli scadrà la password, ma tutti i giorni verrà eseguito lo script che lo avvisa che la password del suo account scadrà DOMANI
Il giorno prima della scadenza verrà comunque avvisato.

Ma non preoccupatevi più di tanto perchè vi chiameranno lo stesso per fare il reset della loro password il giorno dopo la scadenza.
Se questo articolo ti è stato utile puoi ringraziarmi con un clic su questo banner di Google. Grazie.

Commenti

Post popolari in questo blog

Piccoli spiragli

How to collect diagnostic information when a failed ESXi Host upgrade