Get-OMEAlert.ps1

Function Get-OMEAlert
{

    <#
    .SYNOPSIS
        Gets the events in an Alert log on the OME server.
    .DESCRIPTION
        Gets the events in an Alert log on the OME server.
    .PARAMETER Id
        Specifies the ID of the device.
    .PARAMETER Name
        Specifies the Name of the device.
    .PARAMETER AssetTag
        Specifies the Asset Tag of the device.
    .PARAMETER ServiceTag
        Specifies the Service Tag of the device.
    .PARAMETER All
        Query all alert messages for all the devices.
    .PARAMETER Severity
        Query only alert messages with the specific Severity type.
    .PARAMETER Before
        Specifies the date and time that this cmdlet get events that occur
        before.
    .PARAMETER After
        Specifies the date and time that this cmdlet get events that occur
        after.
    .PARAMETER Newest
        Specifies the maximum number of events that this cmdlet gets. This
        cmdlet gets the specified number of events, beginning with the newest event
        in the log.
    .PARAMETER Session
        Specifies the Session Id for the OME server.
 
    .EXAMPLE
        $session=Set-OMEConnection -Name "Session" -Server OMEserver.example.com -IgnoreSSLErrors
        Get-OMEAlert -Session $session -All -Severity Critical -Newest 5
         
        Id Severity Time DeviceName Message
        -- -------- ---- ---------- -------
        -2147468663 Critical 2016-09-09T03:00:14 Server1.example.com Device Server1.example.com ...
        -2147468655 Critical 2016-09-09T11:11:00 Server2.example.com Device Server2.example.com ...
        -2147468649 Critical 2016-09-09T22:30:38 Server1.example.com Device Server1.example.com ...
 
    .EXAMPLE
        Get-OMEAlert -Session $session -Name Server3.eeu.mkcorp.com -Before "2016-09-09T11:10:22" -After "2016-08-02T11:19:34" -Newest 1
 
        Id Severity Time DeviceName Message
        -- -------- ---- ---------- -------
        -2147468663 Critical 2016-09-09T03:00:14 Server3.eexample.com Device Server3.example.com ...
 
    .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/Alerts
    #>
  

    [CmdletBinding(
    )]
    Param(
        [parameter(Mandatory=$true,ParameterSetName="Id",ValueFromPipeline=$true)]
        $Id,
        [parameter(Mandatory=$true,ParameterSetName="Name")]
        [String]$Name,
        [parameter(Mandatory=$true,ParameterSetName="AssetTag")]
        [String]$AssetTag,
        [parameter(Mandatory=$true,ParameterSetName="ServiceTag")]
        [String]$ServiceTag,
        [parameter(Mandatory=$true,ParameterSetName="All")]
        [Switch]$All,
        [OMESeverityType]$Severity,
        [datetime]$Before,
        [datetime]$After,
        [int]$Newest,
        $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) {
                if (!($Id -as [int])) {
                    $Id=$Id.Id
                }
            }
            if ($Name) {
                $Id=$(Get-OMEDevice -Session $Session -All | where {$_.Name -eq $Name}).Id
            }
            if ($AssetTag) {
                $Id=$(Get-OMEDevice -Session $Session -All | where {$_.AssetTag -eq $AssetTag}).Id
            }
            if ($ServiceTag) {
                $Id=$(Get-OMEDevice -Session $Session -All | where {$_.ServiceTag -eq $ServiceTag}).Id
            }
            if ($Id) {
                $uri=$BaseUri+"/Devices/$Id/Alerts"
                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
                    $result=$xml.DeviceAlertsResponse.DeviceAlertsResult.Alert
                    if ($severity) {
                        $result=$result | where {$_.Severity -eq $severity}
                    }
                    if ($Before) {
                        $result=$result | where {[datetime]$_.Time -lt $Before}
                    }
                    if ($After) {
                        $result=$result | where {[datetime]$_.Time -gt $After}
                    }
                    if ($Newest) {
                        $result=$result | Sort-Object {$_.Time -as [datetime]} | select -Last $Newest
                    }
                }
                return Convert-XMLtoPSObject -xml $result -ObjectType "OME.Alert"
            }
            else {
                $uri=$BaseUri+"/Alerts"
                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
                    $result=$xml.GetAllAlertsResponse.GetAllAlertsResult.Alert
                    if ($severity) {
                        $result=$result | where {$_.Severity -eq $severity}
                    }
                    if ($Before) {
                        $result=$result | where {[datetime]$_.Time -lt $Before}
                    }
                    if ($After) {
                        $result=$result | where {[datetime]$_.Time -gt $After}
                    }
                    if ($Newest) {
                        $result=$result | Sort-Object {$_.Time -as [datetime]} | select -Last $Newest
                    }
                    if ($result) {
                        return Convert-XMLtoPSObject -xml $result -ObjectType "OME.Alert"
                    }
                }
            }
        }
        Catch {
            Out-OMEException -Exception $_
        }
    }
    
    End{}                
}