In this article, we will see, how to retrieve the Alerts from the SharePoint WebApplication using PowerShell.
######################### this method will return the Alerts in the WebApplication
function GetAlerts()
{
Write-Host "Entered GetAlerts Method" -ForegroundColor Yellow
Add-Content "$ProgressFile" "Entered GetAlerts Method"
$script:Progress = "6:Entered"
# Get WebApplication Object
$WebApplication = Get-SPWebApplication $Config.Configuration.WebApplication.URL
# Assign the XML Output File Paths
$Alerts_XML_Path = $scriptBase + "Reports-$LogTimeAlerts.xml"
# Create the XML File Tags
$xmlWriter = New-Object System.XMl.XmlTextWriter($Alerts_XML_Path,$Null)
$xmlWriter.Formatting = 'Indented'
$xmlWriter.Indentation = 1
$XmlWriter.IndentChar = "`t"
$xmlWriter.WriteStartDocument()
$xmlWriter.WriteComment('Files List On the WebApplication ' + $WebApplication.DisplayName)
$xmlWriter.WriteStartElement('WebApplication')
$xmlWriter.WriteEndElement()
$xmlWriter.WriteEndDocument()
$xmlWriter.Flush()
$xmlWriter.Close()
# Get All the Site collections
$SiteCollections = $WebApplication | Get-SPSite -Limit All
#$SiteCollections = Get-SPSite "http://ctsc00556722901:5555/sites/TeamSite2/"
# write the output on XML File
$xmlDoc = [System.Xml.XmlDocument](Get-Content $Alerts_XML_Path);
$siteCollectionNode = $xmlDoc.CreateElement("SiteCollections")
$xmlDoc.SelectSingleNode("//WebApplication").AppendChild($siteCollectionNode)
$xmlDoc.Save($Alerts_XML_Path)
# Iterate through the SiteCollections
foreach($SiteCollection in $SiteCollections)
{
$siteCollectionName = $SiteCollection | select @{label = "Title";Ex = {$_.rootweb.Title}}
# write the output on XML File
$xmlDoc = [System.Xml.XmlDocument](Get-Content $Alerts_XML_Path);
$siteCollectionNode = $xmlDoc.CreateElement("SiteCollection")
$xmlDoc.SelectSingleNode("//WebApplication/SiteCollections").AppendChild($siteCollectionNode)
$siteCollectionNode.SetAttribute("Name", $siteCollectionName.Title)
$subSitesNode = $siteCollectionNode.AppendChild($xmlDoc.CreateElement("SubSites"));
$xmlDoc.Save($Alerts_XML_Path)
# Get the Webs
$Webs = Get-SPWeb -site $SiteCollection -Limit All
# Iterate through the Webs
foreach($Web in $Webs)
{
# write the output on XML File
$subSiteNameNode = $subSitesNode.AppendChild($xmlDoc.CreateElement("SubSite"));
$subSiteNameNode.SetAttribute("Name", $Web.Title)
$subSiteNameNode.SetAttribute("Url", $Web.Url)
$subSiteNameNode.SetAttribute("WebID", $Web.Id)
$parentWebTitle = ""
if($Web.ParentWebID -ne "00000000-0000-0000-0000-000000000000")
{
$parentWeb = $SiteCollection.OpenWeb($Web.ParentWebID)
$parentWebTitle = $parentWeb.Title
}
else
{
$parentWebTitle = "RootWeb"
}
$subSiteNameNode.SetAttribute("ParentWebName", $parentWebTitle)
$subSiteNameNode.SetAttribute("ParentWebID", $Web.ParentWebID)
$AlertsNode = $subSiteNameNode.AppendChild($xmlDoc.CreateElement("Alerts"));
$xmlDoc.Save($Alerts_XML_Path)
# Iterate through the Users
foreach ($user in $Web.SiteUsers)
{
# Get the User Alerts and Iterate
foreach($alert in $user.Alerts)
{
$AlertNode = $AlertsNode.AppendChild($xmlDoc.CreateElement("Alert"));
$AlertNode.SetAttribute("Name", $alert.Title)
$AlertNode.SetAttribute("List", $alert.List)
$AlertNode.SetAttribute("AssociatedUser", $user.LoginName)
$AlertNode.SetAttribute("AlertFerequency", $alert.AlertFrequency)
$AlertNode.SetAttribute("AlertType", $alert.AlertType)
$xmlDoc.Save($Alerts_XML_Path)
}
}
}
}
# write the output on XML File
$xmlDoc.Save($Alerts_XML_Path)
Write-Host "Completed GetAlerts Method" -ForegroundColor Yellow
Add-Content "$ProgressFile" "Completed GetAlerts Method"
$script:Progress = "6:Success"
}
########### End of Method #################
The snippet is self-explanatory.
Happy Coding,
Sathish Nadarajan.
Leave a comment