I'm using the JiraPS module and I have already raised a bug for this issue, however I'm trying to work around it. I am using Jira Cloud and have found some gaps between how Jira Server and Jira Cloud operate (namely on things like using username for server but userid for cloud). When I try to call the JiraPS function Add-JiraGroupMember it errors out throwing a 401 (the same $credential is being passed for adding user, which doesn't fail).
See bug report
However I took that function and did some tweaking to it to try to make it work:
function Add-JiraGroupMemberCloud {
[CmdletBinding( SupportsShouldProcess )]
param(
[Parameter( Mandatory, ValueFromPipeline )]
[Alias(<span>'GroupName'</span>)]
[ValidateNotNullOrEmpty()]
[Object[]]
$Group,
[Parameter( Mandatory )]
[ValidateNotNullOrEmpty()]
[Object[]]
$User,
[Parameter()]
[System.Management.Automation.PSCredential]
[System.Management.Automation.Credential()]
$Credential,
[Switch]
$PassThru
)
Write-Verbose <span>"[$($MyInvocation.MyCommand.Name)] Function started"</span>
$server = Get-JiraConfigServer -ErrorAction Stop
$resourceURi = <span>"$server/rest/api/2/group/user?groupId={0}"</span>
<span>foreach</span> ($_group in $Group) {
Write-Verbose <span>"[$($MyInvocation.MyCommand.Name)] Processing [$_group]"</span>
Write-Debug <span>"[$($MyInvocation.MyCommand.Name)] Processing `$_group [$_group]"</span>
$groupObj = Get-JiraGroup -GroupName $_group -Credential $Credential -ErrorAction Stop
$groupMembers = (Get-JiraGroupMember -Group $_group -Credential $Credential -ErrorAction Stop).AccountId
<span>if</span> ($groupMembers -notcontains $user.AccountId) {
Write-Debug -Message <span>"[$($MyInvocation.MyCommand.Name)] User [$($user.EmailAddress)] is not already in group [$_group]. Adding user."</span>
$parameter = @{
URI = $resourceURi -f ($groupObj.RestURL -<span>split</span> (<span>"groupID="</span>))[<span>1</span>]
Method = <span>"POST"</span>
headers = @{<span>'Accept'</span>=<span>'application/json'</span>; <span>'Content-Type'</span>=<span>'application/json'</span>}
Body = ConvertTo-Json -InputObject @{ <span>'accountId'</span> = $user.AccountId }
Credential = $Credential
}
<span># echo "curl --request POST \`n --url $($parameter.URI) \`n --user ""$($credential.GetNetworkCredential().Username):$($credential.GetNetworkCredential().password)"" \`n --header 'Accept: application/json' \`n --header 'Content-Type: application/json' \`n --data '$($parameter.Body)'"</span>
Write-Debug -Message <span>"[$($MyInvocation.MyCommand.Name)] Invoking JiraMethod with `$parameter"</span>
<span>if</span> ($PSCmdlet.ShouldProcess($GroupName, <span>"Adding user '$($user.AccountId)'."</span>)) {
Try{
$result = Invoke-RestMethod @parameter
}
Catch{
<span>return</span> $_
}
}
}
<span>else</span> {
Write-Warning -Message <span>"User [$($user.EmailAddress)] is already a member of group [$_group]"</span>
}
}
<span>if</span> ($PassThru) {
Write-Output (ConvertTo-JiraGroup -InputObject $result)
}
}When it hits the invoke-restmethod it does bomb out and return an error:
Invoke-RestMethod : The remote server returned an <span>error</span>: (<span>401</span>) Unauthorized.
so I added a step in there that basically builds out the call using cURL (based on the structure example in the Jira REST API docs). When I put a breakpoint in at the invoke-restmethod, and I run the echo to get the cURL command and run it in ubuntu, it works without any issues. I cannot seem to figure out what the issue with the invoke-restmethod call is, or why it would work in cURL, but I assuming I'm doing something stupid that I'm hoping someone here can see better than I can.