Get-OMEReport.ps1

Function Get-OMEReport
{

    <#
    .SYNOPSIS
        Gets specified OME report.
    .DESCRIPTION
        Gets specified OME report.
    .PARAMETER Session
        Specifies the Session Id for the OME server.
    .PARAMETER Report
        Specifies the report type.
 
    .EXAMPLE
        Get-OMEReport -Session $session -Report AgentHealthStatus | Out-GridView
    .EXAMPLE
        Get-OMEReport -Session $session -Report NicInformation | Out-HTML | Out-File "Report.html"
 
    .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/Reports/$Report
    #>
  

    [CmdletBinding(
    )]
    Param(
        $Session="OMEConnection.DefaultOMESession"
    )
    DynamicParam {
            $ssn=$(Get-Variable -Scope Global | where {$_.Name -like "OMEConnection.*"}).Name
            if ($($ssn | Measure-Object).Count -gt 1) {$ssn=$ssn[0]}
            $_Values = Get-OMEReportType -Session $ssn -WarningAction SilentlyContinue
            $attributes = new-object System.Management.Automation.ParameterAttribute
            $attributes.ParameterSetName = "__AllParameterSets"
            $attributes.Mandatory = $true
            $attributeCollection =
              new-object -Type System.Collections.ObjectModel.Collection[System.Attribute]
            $attributeCollection.Add($attributes)
            $ValidateSet =
              new-object System.Management.Automation.ValidateSetAttribute($_Values)
            $attributeCollection.Add($ValidateSet)
            $Report =
              new-object -Type System.Management.Automation.RuntimeDefinedParameter(
              "Report", [string], $attributeCollection)
            $paramDictionary =
              new-object -Type System.Management.Automation.RuntimeDefinedParameterDictionary
            $paramDictionary.Add("Report", $Report)
            return $paramDictionary
    }    
    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 {
            $uri=$BaseUri+"/Reports/$($Report.Value)"
            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.GetReportResponse.GetReportResult
                $_array=@{}
                $Properties=@{}
                $obj=New-Object -TypeName PSObject
                $result.ColumnHeaders.ColumnHeader | Sort-Object -Property ColumnNumber| foreach {
                    $_array.Add($_.ColumnNumber,$_.HeaderName)
                    $Properties.Add($_.HeaderName,"")
                    $Obj | Add-Member -MemberType NoteProperty $_.HeaderName -Value ""
                    $Obj.PsObject.TypeNames.Insert(0,"OME.Report")
                }
                $all=@()
                foreach ($row in $result.ReportDataRows.ArrayOfCellData) {
                    foreach ($column in $row.CellData) {
                        $Obj.$($_array.$($column.ColumnNumber))=$column.Data
                    }
                    $all+=$Obj.PSObject.Copy()
                }
                return $all
            }
        }
        Catch {
            Out-OMEException -Exception $_
        }
    }
    
    End{}                
}