Get-OMEDevice.ps1

Function Get-OMEDevice
{

    <#
    .SYNOPSIS
        Gets the devices from the specified OME server.
    .DESCRIPTION
        Gets the devices from the specified OME server.
    .PARAMETER Group
        Specifies the group devices will be requested.
    .PARAMETER All
        Query devices from all groups.
    .PARAMETER AllChildDevices
        Can be used to retrieve the child groups of a specific group.
    .PARAMETER DeviceType
        Query only devices of the specific Device type.
    .PARAMETER GlobalStatus
        Query only devices with the specific Health status.
    .PARAMETER Id
        Query device with the specific Id.
    .PARAMETER Name
        Query device with the specific Name.
    .PARAMETER AssetTag
        Query device with the specific Asset Tag.
    .PARAMETER ServiceTag
        Query device with the specific Service Tag.
    .PARAMETER Inventory
        Query detailed information for the specific device.
    .PARAMETER Session
        Specifies the Session Id for the OME server.
 
    .EXAMPLE
        $session=Set-OMEConnection -Name "Session" -Server OMEserver.example.com -IgnoreSSLErrors
        Get-OMEDevice -Session $session -All
         
        Id Name ServiceTag
        -- ---- ----------
        350 server1.example.com xyzxyzz
        59 server2.example.com dkjdkjk
        65 server3.example.com cvbnmd3
        25 server4.example.com 34dfj5d
         
    .EXAMPLE
        Get-OMEDeviceGroup -Session $Session -Name MyGroup | Get-OMEDevice -Session $Session
         
        Id Name ServiceTag
        -- ---- ----------
        325 server41.example.com xyzxyzz
        369 server23.example.com dkjdkjk
         
    .EXAMPLE
        Get-OMEDevice -Session $session -Id 369 -Inventory | select *
 
    .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/Devices
    #>
  

    [CmdletBinding(
    )]
    Param(
        [parameter(Mandatory=$true,ParameterSetName="Group",ValueFromPipeline=$true)]
        $Group,
        [parameter(Mandatory=$true,ParameterSetName="All")]
        [switch]$All,
        [parameter(ParameterSetName="All")]
        [parameter(ParameterSetName="Group")]
        [switch]$AllChildDevices,
        [parameter(ParameterSetName="All")]
        [parameter(ParameterSetName="Group")]
        [OMEDeviceType[]]$DeviceType,
        [parameter(ParameterSetName="All")]
        [parameter(ParameterSetName="Group")]
        [OMEGlobalStatusType[]]$GlobalStatus,
        [parameter(Mandatory=$true,ParameterSetName="Id")]
        $Id,
        [parameter(Mandatory=$true,ParameterSetName="Name")]
        $Name,
        [parameter(Mandatory=$true,ParameterSetName="AssetTag")]
        $AssetTag,
        [parameter(Mandatory=$true,ParameterSetName="ServiceTag")]
        $ServiceTag,
        [parameter(ParameterSetName="Id")]
        [parameter(ParameterSetName="Name")]
        [parameter(ParameterSetName="AssetTag")]    
        [parameter(ParameterSetName="ServiceTag")]
        [switch]$Inventory,
        $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 -or $AssetTag -or $ServiceTag){
                if ($Id) {
                    #todo set $device using rest url
                    $device=$(Get-OMEDevice -Session $Session -All | where {$_.Id -eq $Id})
                    If (!$Inventory) {
                        return $device
                    }
                }
                if ($Name) {
                    $device=$(Get-OMEDevice -Session $Session -All | where {$_.Name -eq $Name})
                    If (!$Inventory) {
                        return $device
                    }
                }
                if ($AssetTag) {
                    $device=$(Get-OMEDevice -Session $Session -All | where {$_.AssetTag -eq $AssetTag})
                    If (!$Inventory) {
                        return $device
                    }
                }
                if ($ServiceTag) {
                    $device=$(Get-OMEDevice -Session $Session -All | where {$_.ServiceTag -eq $ServiceTag})
                    If (!$Inventory) {
                        return $device
                    }
                }
                if ($Inventory -and $device){
                    $Info=@()
                    $ListOfuri=@()
                    $device.Id | foreach {$ListOfuri+=$BaseUri+"/Devices/$_"}
                    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.DeviceInventoryResponse.DeviceInventoryResult
                            $BasicInfo=Convert-XMLtoPSObject -xml $result -ObjectType "OME.DeviceInventory"
                        }
                        $BasicInfo.PSObject.Properties.Remove('i')
                        $Info+=$BasicInfo
                    }
                    return $BasicInfo
                }
            }
            if ($Group -or $All) {
                if ($Group) {
                    if ($Group -as [int32]) {
                        $GroupId=$Group
                    }
                    if ($Group -is [PSObject]) {
                        $GroupID=$Group.Id
                        if (!$GroupID) {
                            return
                        }
                    }
                    if ($Group -is [string]) {
                        $GroupId=$(Get-OMEDeviceGroup -Session $Session -All | where {$_.Name -eq $Group}).Id
                        if (!$GroupID) {
                            return
                        }
                    }
                }
                $Info=@()
                $ListOfuri=@()
                if ($GroupID) {
                    if ($AllChildDevices) {
                        $GroupId | foreach {$ListOfuri+=$BaseUri+"/DeviceGroups/$_/Devices"}
                    }
                    else {
                        $GroupId | foreach {$ListOfuri+=$BaseUri+"/DeviceGroups/$_/ChildDevices"}
                    }
                }
                else {
                    #for -All
                    $ListOfuri+=$BaseUri+"/Devices"
                }
                
                
                
                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
                        if ($GroupID) {
                            if ($AllChildDevices) {
                                $result=$xml.GetDevicesResponse.GetDevicesResult.Device
                            }
                            else {
                                $result=$xml.GetChildDevicesResponse.GetChildDevicesResult.Device
                            }
                        }
                        else {
                            $result=$xml.DevicesResponse.DevicesResult.Devices.Device
                        }
                    }
                    Write-Debug [string]$DeviceType
                    if ($DeviceType) {
                        $result=$result | where {$_.Type -in $DeviceType.value__}
                    }
                    if ($GlobalStatus) {
                        $result=$result | where {$_.GlobalStatus -in $GlobalStatus.value__}
                    }
                    if ($result) {$Info+=$result}
                }
                return Convert-XMLtoPSObject -xml $Info -ObjectType "OME.Device"
            }
        }
        Catch {
            Out-OMEException -Exception $_
        }
    }
    
    End{}                
}