Param ([switch]$Continuous, [int]$Interval = 10, [switch]$Force) $TimeSkew = 4 $ListUrl = "http://sharepoint/Lists/OpsMgrAlerts/AllItems.aspx" $AlertUrl = "http://opsmgr:51908/default.aspx?DisplayMode=Pivot&AlertID=" $States = @{ 0 = "New" 255 = "Resolved" } #Connect to SP List Write-Host "Connecting to SharePoint List" $DllPath = "${env:CommonProgramFiles}\Microsoft Shared\Web Server Extensions\12\ISAPI\Microsoft.SharePoint.Dll" [System.Reflection.Assembly]::LoadFrom($DllPath) | Out-Null $Site = New-Object -Type "Microsoft.SharePoint.SPSite" -ArgumentList "http://localhost" $Web = $Site.OpenWeb() $List = $Web.GetList($ListUrl) #Connect to OpsMgr Write-Host "Connecting to OpsMgr" add-pssnapin Microsoft.EnterpriseManagement.OperationsManager.Client cd "C:\Program Files\System Center Operations Manager 2007" . .\Microsoft.EnterpriseManagement.OperationsManager.ClientShell.NonInteractiveStartup.ps1 function Sync-Alerts ([switch]$Force) { if ($Force) {Write-Warning "Forced sync enabled, alerts will not sync back to OpsMgr"} #Get alerts from opsmgr by ids, and update status in list #Also sync back if Resolution is changed Write-Host "Updating list alerts" $Delete=@() foreach ($Item in $List.Items) { $Alert = $null $Alert = Get-Alert -ID $Item["OMID"] if ($Alert) { if (($Alert.LastModified.addhours($TimeSkew) -gt $Item["Modified"]) -or $Force) { if ($Alert.ResolutionState -eq 255) { Write-Host "Item $($Alert.ID) is resolved in OpsMgr and marked for removal from list" $Delete += $Item.Id } else { Write-Host "Updating alert $($Alert.ID)" $Item["Description"] = $Alert.Description $Item["RepeatCount"] = $Alert.RepeatCount $Item["Changed"] = $Alert.LastModified $Item["Resolution"] = $States[[int]$Alert.ResolutionState] $Item.Update() } } else { if ($States[[int]$Alert.ResolutionState] -ne $Item["Resolution"]) { Write-Host "Setting resolution state for item $($Alert.ID) back to OpsMgr" $Alert.ResolutionState = ($States.GetEnumerator() | ?{$_.Value -eq $Item["Resolution"]}).Name $Alert.Update("ResolutionState synchonized from SharePoint at $(get-date)") if ($Alert.ResolutionState -eq 255) { Write-Host "Alert $($Alert.ID) is resolved in list and marked for removal" $Delete += $Item.Id } } } } else { Write-Warning "Alert with ID $($Item["OMID"]) not found in OpsMgr, marking for removal" $Delete += $Item.Id } } Write-Host "Removing resolved items from the list" $Delete | foreach {Write-host "Removing item $_ from list"; $list.Items.DeleteItemById($_)} #Get-newalerts form opsmgr, and add to list if it doenst exist Write-Host "Adding new alerts to list" foreach ($Alert in (Get-Alert -Criteria 'ResolutionState != 255')) { if (-not ($list.Items | where {$_["OMID"] -eq $Alert.ID})) { Write-host "Adding alert $($AlertID) ($($Alert.Name)) to list" $New = $List.Items.Add() $New["Title"] = $Alert.Name $New["OMID"] = $Alert.ID $New["Description"] = $Alert.Description $New["Computer"] = $Alert.NetbiosComputerName $New["FQDN"] = $Alert.PrincipalName $New["RepeatCount"] = $Alert.RepeatCount $New["Raised"] = $Alert.TimeRaised $New["Changed"] = $Alert.LastModified $New["Resolution"] = $States[[int]$Alert.ResolutionState] $New["Severity"] = $Alert.Severity $New["Priority"] = $Alert.Priority $New["Category"] = $Alert.Category $New["Link"] = $AlertUrl + $Alert.ID + ", View" $New.Update() } } Write-Host "Done" } if ($Continuous) { $Counter = 0 while ($True) { $Counter ++ if ($Counter -eq 10) { Write-Host "`nCollecting garbage" [GC]::Collect() Write-Host "Done" $Counter = 0 } Write-Host "`nStarting sync at $(get-date)" Sync-Alerts -force:$Force Sleep $Interval } } else { Sync-Alerts -force:$Force }