Get-OMETask.ps1

Function Get-OMETask
{

    <#
    .SYNOPSIS
        Gets OME tasks.
    .DESCRIPTION
        Gets OME tasks.
    .PARAMETER Id
        Specifies Task Id.
    .PARAMETER Summary
        Query summary information about task.
    .PARAMETER Session
        Specifies the Session Id for the OME server.
 
    .EXAMPLE
        Get-OMETask -Session $session -Summary
        Id Schedule Name Description
        -- -------- ---- -----------
        1834162083 System Update Task - 4/13/2016 3:15:44 PM ... iDRAC Server Software Update task.
        1834730300 System Update Task - 3/23/2016 6:16:10 PM ... iDRAC Server Software Update task.
        1843244388 System Update Task - 4/28/2016 9:47:48 AM ... CMC Enclosure Software Update task
        2051725540 System Update Task - 9/6/2016 4:25:19 PM -... iDRAC Server Software Update task.
        2075405309 System Update Task - 7/21/2016 11:37:01 AM... iDRAC Server Software Update task.
        2132056103 System Update Task - 5/28/2016 8:45:23 PM ... iDRAC Server Software Update task.
        <....>
         
    .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/Tasks
    #>
  

    [CmdletBinding(
    )]
    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+"/Tasks/$Id"
            }
            else {
                $uri=$BaseUri+"/Tasks"
            }
            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.GetTaskResponse.GetTaskResult
                }
                else {
                    $result=$xml.GetAllTasksResponse.GetAllTasksResult.Task
                }
                $BasicInfo=Convert-XMLtoPSObject -xml $result  -ObjectType "OME.Task"
                if ($Summary) {
                    $SummaryInfo=@()
                    foreach ($object in $BasicInfo) {
                        $uri=$BaseUri+"/Tasks/$($object.Id)/ExecutionSummary"
                        if ($CurrentSession.Credentials) {
                            try {
                                $result = Invoke-WebRequest -Uri $uri -Credential $CurrentSession.Credentials
                            } 
                            catch {
                                Write-Warning "Can't get Summary Info"
                                return $BasicInfo
                            }
                        }
                        else {
                            try {
                                $result = Invoke-WebRequest -Uri $uri -UseDefaultCredentials
                            } 
                            catch {
                                Write-Warning "Can't get Summary Info"
                                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.GetTaskExecutionSummaryResponse.GetTaskExecutionSummaryResult
                            if ($result) {
                                $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 -Force
                                    }
                                }
                                $Info.PSObject.Properties.Remove('i')
                                $SummaryInfo+=$Info
                            }
                            else {
                                $SummaryInfo=$BasicInfo
                            }
                        }
                    }
                    $BasicInfo=$SummaryInfo.PSObject.Copy()
                    $SummaryInfo=@()
                    foreach ($object in $BasicInfo) {
                        $uri=$BaseUri+"/Tasks/$($object.Id)/ExecutionDetails"
                        if ($CurrentSession.Credentials) {
                            try {
                                $result = Invoke-WebRequest -Uri $uri -Credential $CurrentSession.Credentials
                            } 
                            catch {
                                Write-Warning "Can't get Summary Info"
                                return $BasicInfo
                            }
                        }
                        else {
                            try {
                                $result = Invoke-WebRequest -Uri $uri -UseDefaultCredentials
                            } 
                            catch {
                                Write-Warning "Can't get Summary Info"
                                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.GetTaskExecutionDetailsResponse.GetTaskExecutionDetailsResult.ExecutionDetails
                            if ($result){
                                $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 -Force
                                    }
                                }
                                $Info.PSObject.Properties.Remove('i')
                                $SummaryInfo+=$Info
                            }
                            else {
                                $SummaryInfo=$BasicInfo
                            }
                        }
                    }
                    return $SummaryInfo
                }
                return $BasicInfo
            }
        }
        Catch {
            Out-OMEException -Exception $_
        }
    }
    
    End{}                
}