Get-OMEDeviceGroup.ps1

Function Get-OMEDeviceGroup
{

    <#
    .SYNOPSIS
        Gets the groups from the OME server.
    .DESCRIPTION
        Gets the groups from the OME server.
    .PARAMETER All
        Query all the device groups.
    .PARAMETER Id
        Query group with the specified Id.
    .PARAMETER Name
        Query group with the specified Name.
    .PARAMETER ParentGroupId
        Specified Parent Group Id for the query.
    .PARAMETER ParentGroupName
        Specified Parent Group Name for the query.
    .PARAMETER RollupHealth
        Query only the groups in the specified health status.
    .PARAMETER Type
        Query only the groups of the specified type.
    .PARAMETER Session
        Specifies the Session Id for the OME server.
 
    .EXAMPLE
        $session=Set-OMEConnection -Name "Session" -Server OMEserver.example.com -IgnoreSSLErrors
        Get-OMEDeviceGroup -All -Session $Session -Type User -RollupHealth Critical
         
        Id Type Name
        -- ---- ----
        468126390 User London
        600065159 User Paris
        1509790902 User Nakhalovka
     
         
    .EXAMPLE
        Get-OMEDeviceGroup -Session $session -Name Paris | select *
         
        WarningCount : 0
        NormalCount : 2
        CriticalCount : 0
        DeviceCount : 2
        UnknownCount : 0
        Id : 16808985
        Type : 4
        RollupHealth : 4
        Description :
        Name : Paris
 
    .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/DeviceGroups
    #>
  

    [CmdletBinding(
    )]
    Param(
        [parameter(Mandatory=$true,ParameterSetName="All")]
        [switch]$All,
        [parameter(Mandatory=$true,ParameterSetName="Id")]
        $Id,
        [parameter(Mandatory=$true,ParameterSetName="Name")]
        $Name,
        [parameter(Mandatory=$true,ParameterSetName="ParentGroupId")]
        $ParentGroupId,
        [parameter(Mandatory=$true,ParameterSetName="ParentGroupName")]
        $ParentGroupName,
        [parameter(ParameterSetName="ParentGroupId")]
        [parameter(ParameterSetName="ParentGroupName")]
        [parameter(ParameterSetName="All")]
        [OMERollupHealthType[]]$RollupHealth,
        [parameter(ParameterSetName="ParentGroupId")]
        [parameter(ParameterSetName="ParentGroupName")]
        [parameter(ParameterSetName="All")]
        [OMEDeviceGroupType[]]$Type,
        $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 -or $Name) {
                #get basic info
                if ($Name) {
                    $Id=$(Get-OMEDeviceGroup -Session $Session -All | where {$_.Name -eq $Name}).Id
                }
                $Info=@()
                $ListOfuri=@()
                $Id |  foreach {$ListOfuri+=$BaseUri+"/DeviceGroups/$_"}
                Foreach ($uri in $ListOfuri) {
                    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.GetDeviceGroupResponse.GetDeviceGroupResult
                        $BasicInfo=Convert-XMLtoPSObject -xml $result -ObjectType "OME.DeviceGroup"
                    }
                    #get summary
                    $uri=$Uri+"/Summary"
                    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.GetDeviceGroupSummaryResponse.GetDeviceGroupSummaryResult
                        $SummaryInfo=Convert-XMLtoPSObject -xml $result -ObjectType "OME.DeviceGroup"
                    }    
                    #merge Info
                    foreach ( $Property in $BasicInfo.psobject.Properties){
                        if ($Property.Name -notin $SummaryInfo.psobject.Properties.Name) {
                            $SummaryInfo | add-member -MemberType NoteProperty -Name $Property.Name -Value $Property.value
                        }
                    }
                    $SummaryInfo.PSObject.Properties.Remove('i')
                    $Info+=$SummaryInfo
                }
                return $Info
            }
            if ($ParentGroupId -or $ParentGroupName -or $All) {
                if ($ParentGroupName) {
                    $ParentGroupId_=$(Get-OMEDeviceGroup -Session $Session -All | where {$_.Name -eq $ParentGroupName}).Id
                    If (!$ParentGroupId_ -Or $ParentGroupId_ -is [Array]) {
                        return
                    }
                    else {
                        $ParentGroupId=$ParentGroupId_
                    }
                }
                if ($ParentGroupId) {
                    $uri=$BaseUri+"/DeviceGroups/$ParentGroupId/ChildGroups"
                }
                else {
                    $uri=$BaseUri+"/DeviceGroups"
                }
                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 ($ParentGroupId) {
                        $result=$xml.GetChildDeviceGroupsResponse.GetChildDeviceGroupsResult.DeviceGroup
                    }
                    else {
                        $result=$xml.GetDeviceGroupsResponse.GetDeviceGroupsResult.DeviceGroup
                    }    
                }
                if ($RollupHealth) {
                    $result=$result | where {$_.RollupHealth -in $RollupHealth.Value__}
                }
                if ($Type) {
                    $result=$result | where {$_.Type -in $Type.Value__}
                }
                if ($result) {
                    return Convert-XMLtoPSObject -xml $result  -ObjectType "OME.DeviceGroup"
                }
            }
        }
        Catch {
            Out-OMEException -Exception $_
        }
    }
    
    End{}                
}