Public/New-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
function New-JiraGroup {
    <#
    .Synopsis
       Creates a new group in JIRA
    .DESCRIPTION
       This function creates a new group in JIRA.
    .EXAMPLE
       New-JiraGroup -GroupName testGroup
       This example creates a new JIRA group named testGroup.
    .INPUTS
       This function does not accept pipeline input.
    .OUTPUTS
       [JiraPS.Group] The user object created
    #>

    [CmdletBinding(SupportsShouldProcess = $true)]
    param(
        # Name for the new group.
        [Parameter(
            Position = 0,
            Mandatory = $true
        )]
        [Alias('Name')]
        [String] $GroupName,

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

    begin {
        Write-Debug "[New-JiraGroup] Reading information from config file"
        try {
            Write-Debug "[New-JiraGroup] Reading Jira server from config file"
            $server = Get-JiraConfigServer -ConfigFile $ConfigFile -ErrorAction Stop
        }
        catch {
            $err = $_
            Write-Debug "[New-JiraGroup] Encountered an error reading configuration data."
            throw $err
        }

        $restUrl = "$server/rest/api/latest/group"
    }

    process {
        Write-Debug "[New-JiraGroup] Defining properties"
        $props = @{
            "name" = $GroupName;
        }

        Write-Debug "[New-JiraGroup] Converting to JSON"
        $json = ConvertTo-Json -InputObject $props

        Write-Debug "[New-JiraGroup] Checking for -WhatIf and Confirm"
        if ($PSCmdlet.ShouldProcess($GroupName, "Creating group [$GroupName] to JIRA")) {
            Write-Debug "[New-JiraGroup] Preparing for blastoff!"
            $result = Invoke-JiraMethod -Method Post -URI $restUrl -Body $json -Credential $Credential
        }
        if ($result) {
            Write-Debug "[New-JiraGroup] Converting output object into a Jira user and outputting"
            ConvertTo-JiraGroup -InputObject $result
        }
        else {
            Write-Debug "[New-JiraGroup] Jira returned no results to output."
        }
    }
}