Public/Get-JiraProject.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
function Get-JiraProject {
    <#
    .Synopsis
       Returns a project from Jira
    .DESCRIPTION
       This function returns information regarding a specified project from Jira. If
       the Project parameter is not supplied, it will return information about all
       projects the given user is authorized to view.
 
       The -Project parameter will accept either a project ID or a project key.
    .EXAMPLE
       Get-JiraProject -Project TEST -Credential $cred
       Returns information about the project TEST
    .EXAMPLE
       Get-JiraProject 2 -Credential $cred
       Returns information about the project with ID 2
    .EXAMPLE
       Get-JiraProject -Credential $cred
       Returns information about all projects the user is authorized to view
    .INPUTS
       [String[]] Project ID or project key
       [PSCredential] Credentials to use to connect to Jira
    .OUTPUTS
       [JiraPS.Project]
    #>

    [CmdletBinding()]
    param(
        # The Project ID or project key of a project to search.
        [Parameter(
            Position = 0,
            Mandatory = $false
        )]
        [String[]] $Project,

        # 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-JiraProject] Reading server from config file"
        try {
            $server = Get-JiraConfigServer -ConfigFile $ConfigFile -ErrorAction Stop
        }
        catch {
            $err = $_
            Write-Debug "[Get-JiraProject] Encountered an error reading the Jira server."
            throw $err
        }

        $uri = "$server/rest/api/latest/project"
    }

    process {
        if ($Project) {
            foreach ($p in $Project) {
                Write-Debug "[Get-JiraProject] Processing project [$p]"
                $thisUri = "$uri/${p}?expand=projectKeys"

                Write-Debug "[Get-JiraProject] Preparing for blastoff!"

                $result = Invoke-JiraMethod -Method Get -URI $thisUri -Credential $Credential
                if ($result) {
                    Write-Debug "[Get-JiraProject] Converting to object"
                    $obj = ConvertTo-JiraProject -InputObject $result

                    Write-Debug "[Get-JiraProject] Outputting result"
                    Write-Output $obj
                }
                else {
                    Write-Debug "[Get-JiraProject] No results were returned from Jira"
                    Write-Debug "[Get-JiraProject] No results were returned from Jira for project [$p]"
                }
            }
        }
        else {
            Write-Debug "[Get-JiraProject] Attempting to search for all projects"
            $thisUri = "$uri"

            Write-Debug "[Get-JiraProject] Preparing for blastoff!"
            $result = Invoke-JiraMethod -Method Get -URI $uri -Credential $Credential
            if ($result) {
                Write-Debug "[Get-JiraProject] Converting to object"
                $obj = ConvertTo-JiraProject -InputObject $result

                Write-Debug "[Get-JiraProject] Outputting result"
                Write-Output $obj
            }
            else {
                Write-Debug "[Get-JiraProject] No results were returned from Jira"
                Write-Debug "[Get-JiraProject] No project results were returned from Jira"
            }
        }
    }

    end {
        Write-Debug "Complete"
    }
}