Skip to content

Instantly share code, notes, and snippets.

@thegooddoctorgonzo
Last active November 22, 2021 20:21
Show Gist options
  • Select an option

  • Save thegooddoctorgonzo/06b81acd00fe42d811441e2ccdae9a30 to your computer and use it in GitHub Desktop.

Select an option

Save thegooddoctorgonzo/06b81acd00fe42d811441e2ccdae9a30 to your computer and use it in GitHub Desktop.
:
#This script will cycle through the organizational home folders and check to see if the acccounts they are associated with are enabled/existing
#Set the erro action preference
$ErrorActionPreference = "Stop"
$rootPath = "\\SHAREPATH\admin\ProfileUnity"
$folders = gci -Path $rootPath | Where-Object {$_.Name -notlike "_tobe*"} | Sort-Object Name | Select -ExpandProperty Name
#Let's go ahead and create some custom objects to store the problem children
$nonExistentAccounts = @()
$disabledAccounts = @()
foreach($folder in $folders){
#First let's see if the account even exists
try{
$account = Get-ADUser -Identity $folder -ErrorAction Stop
if($account){
$status = $account.enabled
if($status -eq $false){
$disabledAccounts += $folder
}
}
}
catch{
$nonExistentAccounts += $folder
#Write-Host -ForegroundColor Red "$folder is not associated with an existing account!"
}
}
#Generate html for email body
$file = "C:\PEOManage\temp\body.html"
New-Item -Path $file -ItemType File -Force
Add-Content -Path $file -Value "<hr>"
Add-Content -Path $file -Value "<b>The following Profile Unity folders are NOT associated with EXISTING domain accounts:</b><br>"
Add-Content -Path $file -Value "<hr>"
foreach($n in $nonExistentAccounts){
Add-Content -Path $file -Value "$n<br>"
}
Add-Content -Path $file -Value "<hr>"
Add-Content -Path $file -Value "<b>The following Profile Unity folders are associated with DISABLED ACCOUNTS:</b><br>"
Add-Content -Path $file -Value "<hr>"
foreach($d in $disabledAccounts){
Add-Content -Path $file -Value "$d<br>"
}
#Send Alert Email
if($nonExistentAccounts -or $disabledAccounts){
$body = [System.IO.File]::ReadAllText($file)
$subject = "Profile Unity Check!!"
$to = "[email protected]"
Send-MailMessage -Body $body -Subject $subject -From "[email protected]" -To $to -SmtpServer "smtpgw.DOMAIN" -Port 25 -BodyAsHtml
}
Remove-Item -Path $file -Force -ErrorAction SilentlyContinue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment