Get-OMEAlertFilter.ps1

Function Get-OMEAlertFilter
{

    <#
    .SYNOPSIS
        Get OME alert filters.
    .DESCRIPTION
        Get OME alert filters.
    .PARAMETER Id
        Specifies Id of the alert filter
    .PARAMETER Summary
        Gets summary information about the alert filter(s).
    .PARAMETER Session
        Specifies the Session Id for the OME server.
 
    .EXAMPLE
        Get-OMEAlertFilter -Session $Session
         
        Id Type Name
        -- ---- ----
        -635829646 2 All Alerts
        1472269306 2 All Internal Alerts
        1006921498 2 Critical Alerts
        336860766 1 DefaultDuplicateAlertFilter
        1127988348 0 Email Critical Server Alerts to Admin
         
    .EXAMPLE
        Get-OMEAlertFilter -Session $Session -Id 1006921498 -Summary
         
        WarningCount : 0
        NormalCount : 0
        TotalAlerts : 270
        InfoCount : 0
        CriticalCount : 270
        UnknownCount : 0
        IsEnabled : true
        Name : Critical Alerts
        Id : 1006921498
        IsReadOnly : true
        Type : 2
         
    .NOTES
        Author: Mike Khar
    .LINK
        http://www.dell.com/support/home/us/en/04/product-support/product/dell-openmanage-essentials-v2.2/research
        https://$Server:$Port/api/OME.svc/AlertFilters
    #>
  

    [CmdletBinding(DefaultParametersetName="Id"
    )]
    Param(
        [int]$Id,
        [switch]$Summary,
        $Session="OMEConnection.DefaultOMESession"
    )
    
    Begin
    {
        $CurrentSession = Get-Variable -Scope Global -Name $Session -ErrorAction SilentlyContinue -ValueOnly
        If (!$CurrentSession) {
            Write-Warning "Please use Set-OMEConnection first"
            Break
            Return
        }
        else {
            $BaseUri="https://"+$CurrentSession.Server+":"+$CurrentSession.Port+"/api/OME.svc"
        }
    }
    
    Process
    {    Try {
            If ($Id) {
                $uri=$BaseUri+"/AlertFilters/$Id"
            }
            else {
                $uri=$BaseUri+"/AlertFilters"
            }
            if ($CurrentSession.Credentials) {
                $result = Invoke-WebRequest -Uri $uri -Credential $CurrentSession.Credentials
            }
            else {
                $result = Invoke-WebRequest -Uri $uri -UseDefaultCredentials
            }
            if ($result.StatusCode -ne 200) {
                Out-OMEException -Exception $_
                return
            }
            else {
                Write-Debug "HTTP request: $uri HTTP status code: $($result.StatusCode)"
                [xml]$xml=$result.Content
                if ($Id) {
                    $result=$xml.GetAlertFilterResponse.GetAlertFilterResult.AlertFilter
                }
                else {
                    $result=$xml.GetAllAlertFiltersResponse.GetAllAlertFiltersResult.AlertFilter
                }
                $BasicInfo=Convert-XMLtoPSObject -xml $result  -ObjectType "OME.AlertFilters"
                if ($Summary) {
                    $SummaryInfo=@()
                    foreach ($object in $BasicInfo) {
                        $uri=$BaseUri+"/AlertFilters/$($object.Id)/Summary"
                        if ($CurrentSession.Credentials) {
                            try {
                                $result = Invoke-WebRequest -Uri $uri -Credential $CurrentSession.Credentials
                            } 
                            catch {
                                Out-OMEException -Exception $_
                                return $BasicInfo
                            }
                        }
                        else {
                            try {
                                $result = Invoke-WebRequest -Uri $uri -UseDefaultCredentials
                            } 
                            catch {
                                Out-OMEException -Exception $_
                                return $BasicInfo
                            }
                        }
                        if ($result.StatusCode -ne 200) {
                            Out-OMEException -Exception $_
                            return
                        }
                        else {
                            Write-Debug "HTTP request: $uri HTTP status code: $($result.StatusCode)"
                            [xml]$xml=$result.Content
                            $result=$xml.GetAlertFilterSummaryResponse.GetAlertFilterSummaryResult
                            $Info=Convert-XMLtoPSObject -xml $result
                            #merge Info
                            foreach ( $Property in $object.psobject.Properties){
                                if ($Property.Name -notin $Info.psobject.Properties.Name) {
                                    $Info | add-member -MemberType NoteProperty -Name $Property.Name -Value $Property.value
                                }
                            }
                            $Info.PSObject.Properties.Remove('i')
                            $SummaryInfo+=$Info
                        }
                    }
                    return $SummaryInfo
                }
                return $BasicInfo
            }
        }
        Catch {
            Out-OMEException -Exception $_
        }
    }
    
    End{}                
}