Tuesday, December 17, 2013

Find and Delete Orphaned Alerts in SharePoint

Problem Description: In Short - When users leave your organisation and removed from Active Directory (AD) they become orphans in SharePoint! Also their alerts!!

Why we care about them: To keep SharePoint clean, we've to remove em' from all SharePoint sites. So, I've written a PowerShell script to scan all site collections of the web application, find and remove orphaned users from SharePoint here: Find and Delete Orphaned Users in SharePoint with PowerShell

Well, that solves the problem? kind of, but we have one more orphan to clean-up: SharePoint alerts created for orphaned users! Alright, Not a difficult task, Lets make use of the existing script, modify it little, run it before deleting orphaned users!

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Function to Check if an User exists in AD
function CheckUserExistsInAD()
   {
   Param( [Parameter(Mandatory=$true)] [string]$UserLoginID )  
  #Search the User in AD
  $forest = [System.DirectoryServices.ActiveDirectory.Forest]::GetCurrentForest()
  foreach ($Domain in $forest.Domains)
  {
   $context = new-object System.DirectoryServices.ActiveDirectory.DirectoryContext("Domain", $Domain.Name)
         $domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetDomain($context)
   $root = $domain.GetDirectoryEntry()
         $search = [System.DirectoryServices.DirectorySearcher]$root
         $search.Filter = "(&(objectCategory=User)(samAccountName=$UserLoginID))"
         $result = $search.FindOne()
          if ($result -ne $null)
         {
           return $true
         }
  }
  return $false  
 }

 #Get All site collections of all web applications
 $sites = Get-SPWebApplication | Get-SPSite -Limit All
 #Iterate through each site collection
 foreach($site in $sites)
 { 

 Write-Host "Processing Site:"$site.Url -ForegroundColor Magenta
  foreach($Web in $site.AllWebs)
 {
  #Arrays to Hold Orphaned Alerts & Users
   $OrphanedAlerts = @()
  $AlertUsers  = @()
  #Get all Alerts created on the web
  $WebAlerts = $web.Alerts 
  #Get Unique Users from All Alerts 
  $AlertUsers = $web.Alerts | foreach { $_.User } | Select-Object -Unique

  #Check if any user with alerts is :Orphan!
  if($AlertUsers.length -gt 0)
  {
   foreach($AlertUser in $AlertUsers)
    {
     #Write-host "Checking User:"$AlertUser
     #Check if the user is valid - Not Orphan
     $UserName = $AlertUser.UserLogin.split("\")  #Get User Name from : Domain\UserName
     $AccountName = $UserName[1]    #UserName
                     if ((CheckUserExistsInAD $AccountName) -eq $false)
                     {
      $OrphanedAlerts+=$AlertUser.Alerts
      }
    }
   if($OrphanedAlerts.Length -gt 0) 
    { 
     Write-Host "Total No. of Orphaned Alerts Found:" $OrphanedAlerts.Length  -ForegroundColor Red
     #Delete each orphaned alert
     foreach ($OrphAlert in $OrphanedAlerts)
     {
      write-host "`nOrphaned Alert:" $OrphAlert.ID" on "$web.Url "List:" $OrphAlert.ListUrl "User:"$OrphAlert.User
      Write-Host "Deleting Orphaned Alert..."
      #$WebAlerts.Delete($OrphAlert.ID)
     }
    }
   }     
 }
 }

This PowerShell script scans and deletes all alerts created for Orphaned users. Please note, I've commented out the line:
#$WebAlerts.Delete($OrphAlert.ID)
Just run the script to see how many alerts on what lists & libraries, Un-comment the above line by removing the # to actually remove orphaned alerts in SharePoint.



You might also like:
SharePoint Usage Reports
Usage reports, collaboration and audit for SharePoint.
Document SharePoint Farm
Automatically generate SharePoint documentation.
*Sponsored


Check out these SharePoint products:

No comments :

Post a Comment

Please Login and comment to get your questions answered!

You might also like:

Related Posts Plugin for WordPress, Blogger...