Public/Get-JiraGroup.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
function Get-JiraGroup {
    <#
    .Synopsis
       Returns a group from Jira
    .DESCRIPTION
       This function returns information regarding a specified group from JIRA.
 
       By default, this function does not display members of the group. This is JIRA's default
       behavior as well. To display group members, use Get-JiraGroupMember.
    .EXAMPLE
       Get-JiraGroup -GroupName testGroup -Credential $cred
       Returns information about the group "testGroup"
    .EXAMPLE
       Get-ADUser -filter "Name -like 'John*Smith'" | Select-Object -ExpandProperty samAccountName | Get-JiraUser -Credential $cred
       This example searches Active Directory for the username of John W. Smith, John H. Smith,
       and any other John Smiths, then obtains their JIRA user accounts.
    .INPUTS
       [Object[]] The group to look up in JIRA. This can be a String or a JiraPS.Group object.
    .OUTPUTS
       [JiraPS.Group]
    #>

    [CmdletBinding(DefaultParameterSetName = 'ByGroupName')]
    param(
        # Name of the group to search for.
        [Parameter(
            Position = 0,
            Mandatory = $true,
            ParameterSetName = 'ByGroupName'
        )]
        [ValidateNotNullOrEmpty()]
        [Alias('Name')]
        [String[]] $GroupName,

        # Object of the group to search for.
        [Parameter(
            Position = 0,
            Mandatory = $true,
            ValueFromPipeline = $true,
            ParameterSetName = 'ByInputObject',
            ValueFromPipelineByPropertyName = $true
        )]
        [Object[]] $InputObject,

        # Credentials to use to connect to JIRA.
        # If not specified, this function will use anonymous access.
        [Parameter(Mandatory = $false)]
        [System.Management.Automation.PSCredential] $Credential
    )

    begin {
        Write-Debug "[Get-JiraGroup] Reading server from config file"
        $server = Get-JiraConfigServer -ConfigFile $ConfigFile -ErrorAction Stop

        Write-Debug "[Get-JiraIssue] ParameterSetName=$($PSCmdlet.ParameterSetName)"

        Write-Debug "[Get-JiraGroup] Building URI for REST call"
        $groupUrl = "$server/rest/api/latest/group?groupname={0}"
    }

    process {
        if ($PSCmdlet.ParameterSetName -eq 'ByGroupName') {
            foreach ($g in $GroupName) {
                Write-Debug "[Get-JiraGroup] Escaping group name [$g]"
                $escapedGroupName = [System.Web.HttpUtility]::UrlPathEncode($g)

                Write-Debug "[Get-JiraGroup] Escaped group name: [$escapedGroupName]"
                $thisUrl = $groupUrl -f $escapedGroupName

                Write-Debug "[Get-JiraGroup] Preparing for blastoff!"
                $result = Invoke-JiraMethod -Method Get -URI $thisUrl -Credential $Credential

                if ($result) {
                    Write-Debug "[Get-JiraGroup] Converting results to JiraPS.Group"
                    $obj = ConvertTo-JiraGroup -InputObject $result

                    Write-Debug "[Get-JiraGroup] Outputting results"
                    Write-Output $obj
                }
                else {
                    Write-Debug "[Get-JiraGroup] No results were returned from JIRA"
                    Write-Verbose "No results were returned from JIRA."
                }
            }
        }
        else {
            foreach ($i in $InputObject) {
                Write-Debug "[Get-JiraGroup] Processing InputObject [$i]"
                if ((Get-Member -InputObject $i).TypeName -eq 'JiraPS.Group') {
                    Write-Debug "[Get-JiraGroup] User parameter is a JiraPS.Group object"
                    $thisGroupName = $i.Name
                }
                else {
                    $thisGroupName = $i.ToString()
                    Write-Debug "[Get-JiraGroup] Username is assumed to be [$thisGroupName] via ToString()"
                }

                Write-Debug "[Get-JiraGroup] Invoking myself with the UserName parameter set to search for user [$thisGroupName]"
                $groupObj = Get-JiraGroup -GroupName $thisGroupName -Credential $Credential
                Write-Debug "[Get-JiraGroup] Returned from invoking myself; outputting results"
                Write-Output $groupObj
            }
        }
    }

    end {
        Write-Debug "[Get-JiraGroup] Complete"
    }
}