Functions/Expand-BambooResource.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<#
.SYNOPSIS
    Helper cmdlet to convert Bamboo REST API responses to PS pipeline output
.DESCRIPTION
    It accepts any response XML object as an input and expands it content by
    the -ResourceName parameter.
.PARAMETER Response
    Mandatory - Response object as an input
.PARAMETER ResourceName
    Mandatory - Name of the single resource to be expanded from the response XML.
.PARAMETER PluralResourceName
    Optional - Plural name of the resource in case teh response contains
    multiple items. This parameter requires override if appending an 's'
    character to the -ResourceName is not the valid PluralResourceName.
.PARAMETER Root
    Optional - Root node of the XML response that needs to be processed.
    This parameter requires an override, if the root element name is different
    from the -PluralResourceName.

.EXAMPLE
    Invoke-BambooRestMethod -Resource "plan/$PlanKey/artifact" |
    Expand-BambooResource -ResourceName 'artifact' |
#>

function Expand-BambooResource {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory=$True,ValueFromPipeline=$True)]
        [AllowNull()]
        [psobject[]]$Response,

        [Parameter(Mandatory)]
        [string]$ResourceName,

        [Parameter()]
        [string]$PluralResourceName="$($ResourceName)s",

        [Parameter()]
        [string]$Root=$PluralResourceName

    )

    if ($null -eq $Response) { return }

    if ($Response | Get-Member $Root) {
        if ($Response.$Root.$PluralResourceName | Get-Member $ResourceName) {
            $response.$Root.$PluralResourceName.$ResourceName
        }
    }
    elseif ($Response | Get-Member $ResourceName) {
        @($response.$ResourceName)
    }
}