Showing posts with label windows server. Show all posts
Showing posts with label windows server. Show all posts

Monday, June 11, 2012

Active Directory Password Expiration Email Notification

This is a PowerShell script that will send an email notification to Active Directory users when their password will expire in 14, 7, 3, 1, and Zero days. Administrators/helpdesk also get a daily report of passwords that are expired and the users who got an email reminder, a great heads up for your helpdesk.

The script generates a different message when there are zero days remaining (password is expired and must be changed today) and won't continue to notify users when there are less than zero days remaining. Negitive dedlines are used for some account options such as the passwords must be changed at next logon account flag, and you don't want to fill up a user's mailbox when they can't access their mail before changing their password anyway.

To setup the script, search for the "# CONFIG:" strings and edit the following line as documented, then set it up on a server as a daily scheduled task.

Active Directory Password Expiration Email Notification Script
Import-Module ActiveDirectory

$maxdays=(Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge.TotalDays
$summarybody="Name `t ExpireDate `t DaysToExpire `n"

(Get-ADUser -filter {(mail -like "*@domain.com") -and (Enabled -eq "True") -and (PasswordNeverExpires -eq "False")} -properties *) | Sort-Object pwdLastSet |
foreach-object {

    $lastset=Get-Date([System.DateTime]::FromFileTimeUtc($_.pwdLastSet))
    $expires=$lastset.AddDays($maxdays).ToShortDateString()
    $daystoexpire=[math]::round((New-TimeSpan -Start $(Get-Date) -End $expires).TotalDays)
    $samname=$_.samaccountname
    $firstname=$_.GivenName
    if (($daystoexpire -eq 14) -or ($daystoexpire -eq 7) -or ($daystoexpire -eq 3) -or ($daystoexpire -eq 1) -or ($daystoexpire -eq 0)) {
    #if ($daystoexpire -le 14) {
        $ThereAreExpiring=$true
        
         # CONFIG: Enter from email address.
        $emailFrom = "helpdesk@domain.com"
        # CONFIG: Replace domain domain.com with your email domain. Do not change $samname.
        $emailTo = "$samname@domain.com"
        if ($daystoexpire -eq 0) {
        # CONFIG: Enter text for subject and body of email notification for zero days remaining.
            $subject = "$firstname, your password has expried!"
            $body = "$firstname,
Your password has expired and you must change it immediately. No further email notifications will be sent.

Contact support at extension XXXX for assistance."
        }
        Else {
        # CONFIG: Enter text for subject and body of email notification for 14, 7, 3, and 1 days remaining. 
            $subject = "$firstname, your password expires in $daystoexpire day(s)!"
            $body = "$firstname,
Your password expires in $daystoexpire day(s).

If you are using a Windows computer, press Ctrl + Alt + Del the click Change password.

If you are using a Mac computer follow the instructions at http://sharepoint/Documentation to change your password.
"
        }
        # CONFIG: Enter your smtp server here.
        $smtpServer = "email.domain.com"
        $smtp = new-object Net.Mail.SmtpClient($smtpServer)
        $smtp.Send($emailFrom, $emailTo, $subject, $body)   
       
        $summarybody += "$samname `t $expires `t $daystoexpire `n"
    }
    elseif ($daystoexpire -lt 0) {
        $ThereAreExpiring=$true
        # Add a note to the report email, but don't notify user.
        $summarybody += "$samname `t $expires `t $daystoexpire `n"
    }
}
if ($ThereAreExpiring) {
    # CONFIG: From address for report to Helpdesk/IT Admin staff.
    $emailFrom = "helpdesk@domain.com"
    # CONFIG: Address to send report email to (for Helpdesk/IT Admin staff.
    $emailTo = "helpdesk@domain.com"
    # CONFIG: Subject for report email.
    $subject = "Expiring passwords"
    $body = $summarybody
    # CONFIG: SMTP Server.
    $smtpServer = "email.domain.com"
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $smtp.Send($emailFrom, $emailTo, $subject, $body)
}



Monday, June 29, 2009

To the rescue!

Moving MS SQL Server databases from one server to another tonight. Nothing like working into the late hours to screw up.

Removed old DB server from domain. Joined new one. Realized I forgot to get some data off the old server. Old server is removed from domain so only local administrator password will let me log in.

And for some reason, the documented local admin password does not match. Usually I remember to check this before doing something drastic like removing a server from the domain. Heck usually I change the local admin password and create a second account with admin rights just in case. But not tonight. Tonight I'm tired and I'm rushing things. So now I'm locked out of the server...

ARRRRRGH!

I was worried that my faithful tool for resolving this issue, the freeware Offline NT Password & Registry Editor wouldn't be able to save my butt this time. The server is Windows 2003 x64. Up until now I've only used the Offline NT Password & Registry Editor on 32 bit platforms. Also the server had a hardware RAID, who knows if the the boot CD would have the needed drivers.

Deep calming breath. Give it a try.

It's booting...it's detected the RAID card!

Yes it's found the Windows drives!

Yes yes yes it's reading in the SAM!

Offline NT Password & Registry Editor does indeed work on Windows 2003 64 bit!

Yes, I did a happy dance in the server room.

Friday, February 29, 2008

Force detection on Windows Server Update Services Clients

Common Scenario: You just rebooted your server after installing updates. You know there are still updates the your server doesn't have installed, but the server isn't pulling them down from your Windows Server Update Services (WSUS) server. There's not enough time in your maintenance window to go to update.microsoft.com and download the remaining updates, that's why you have a WSUS server in the first place right?

On Windows XP and Windows Server 2003 you can force detection by running the command:

%windir%\System32\wuauclt.exe /detectnow

You won't see any immediate results, but within a few minutes the familiar yellow shield should show up in the notification area informing you that updates are being downloaded or that updates have been downloaded and are ready to install.

Even though I'm fairly up to date with updates in my system images and Microsoft Office installs, I still try to run this after imaging a system or installing Microsoft Office just to make sure nothing was missed before handing things over to the customer.