Skip to content

Instantly share code, notes, and snippets.

@shinjijai
Last active January 17, 2018 20:08
Show Gist options
  • Save shinjijai/8b11d7ebf4b33dffa81216006c5fc6d5 to your computer and use it in GitHub Desktop.
Save shinjijai/8b11d7ebf4b33dffa81216006c5fc6d5 to your computer and use it in GitHub Desktop.
Disable User accoiunts that are expired
#Requires -Version 4.0
#Requires -modules ActiveDirectory
function Send-AlertsMessage {
[CmdletBinding()]
param(
[Parameter(Mandatory=$false)]
[String]$ToAddress,
[Parameter(Mandatory=$false)]
[String]$FromAddress = "_SERVICEACCOUNT@BLAH.COM",
[Parameter(Mandatory=$false)]
[System.Array]$Attachments,
[Parameter(Mandatory=$false)]
[String]$SMTPServer = "EMAILSERVER.BLAH.COM",
[Parameter(Mandatory=$true)]
[String]$Subject,
[Parameter(Mandatory=$true)]
[String]$Body,
[Parameter(Mandatory=$false)]
[int16]$Port = 25
)
$SendMailParams = @{
To = $ToAddress
From = $FromAddress
Subject = $Subject
SMTPServer = $SMTPServer
Body = $Body
}
if($Attachments) {
$SendMailParams.Add("Attachments", $Attachments)
}
Send-MailMessage @SendMailParams
}
$LogLocation = ".\logs"
$LogTranscript = (Get-Date -Format "yyyy_MMM_dd") + "_user_disable_full.log"
Start-Transcript -Path (Join-Path $LogLocation $LogTranscript) -IncludeInvocationHeader #log for troubleshooting
$SelectParams = @{
Property = "UserPrincipalName",
"SamAccountName",
"Name",
"Enabled",
"AccountExpirationDate"
}
$GetUserParams = @{
Filter = {(Enabled -eq $true) -and (AccountExpirationDate -lt $Today) -and (AccountExpirationDate -ne "*")}
Properties = @("UserPrincipalName","SamAccountName","Name","Enabled","AccountExpirationDate")
}
$Today = Get-Date
$ExpiredUsers = Get-ADUser @GetUserParams | Select-Object @SelectParams
Get-Job | Remove-Job
$DisableScriptBlock = {
param(
[string]$SamAccountName
)
Write-Host $UserPrincipalName
Set-ADUser -Identity $SamAccountName -Enabled $false -Confirm:$false
}
Foreach($OldUser in $ExpiredUsers) {
Start-Job -ScriptBlock $DisableScriptBlock -ArgumentList $OldUser.SamAccountName
}
Get-Job | Wait-Job
Get-Job | Receive-Job
Start-Sleep -Seconds 60
$NewExpiredUserList = Get-ADUser @GetUserParams | Select-Object @SelectParam
if($NewExpiredUserList.Count -gt 0) {
$ErrorAccountInfo = $NewExpiredUserList | Format-Table -AutoSize | Out-String
$Body = ("The following user account are still active:
$ErrorAccountInfo
Please manually look at the account(s) and disable them manually if needed.
AUTOMATED MESSAGE.
")
$EmailParams = @{
ToAddress = "EMAILADDRESS@BLAH.COM"
Subject = "Disable account error!"
Body = $Body
}
Write-Verbose "Send Error email."
Send-AlertsMessage @EmailParams
}
else{
$DisabledAccounts = $ExpiredUsers | Format-Table -AutoSize | Out-String
$Body = ("The following user account were disabled:
$DisabledAccounts
AUTOMATED MESSAGE.")
$EmailParams = @{
ToAddress = "EMAILADDRESS@BLAH.COM"
Subject = "Disabled Accounts - $Today"
Body = $Body
}
Write-Verbose "Send disabled user emails."
Send-AlertsMessage @EmailParams
}
Stop-Transcript
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment