2

我正在尝试从我们的 Atlassian Confluence/Jira 实例中提取用户列表。但是,我很难找到有关可用 REST 服务的良好文档,而且似乎不推荐使用 SOAP 服务。

下面的代码确实得到了结果,但是我们有超过 100 个用户,这会返回 0。

if(-not ($credentials)) { #put this here so I can rerun the same script in the same IDE session without having to reinput credentials each time
    $credentials = get-credential 'myAtlassianUsername'
}
$tenant = 'myCompany'
invoke-restmethod -Method Get -Uri ('https://{0}.atlassian.net/rest/api/2/groupuserpicker?query=users' -f $tenant) -Credential $credentials | ConvertTo-Json -Depth 5

(这ConvertTo-Json只是为了更简单地查看扩展的结果集)。

{
    "users":  {
                  "users":  [

                            ],
                  "total":  0,
                  "header":  "Showing 0 of 0 matching users"
              },
    "groups":  {
                   "header":  "Showing 2 of 2 matching groups",
                   "total":  2,
                   "groups":  [
                                  {
                                      "name":  "confluence-users",
                                      "html":  "confluence-\u003cb\u003eusers\u003c/b\u003e",
                                      "labels":  [

                                                 ]
                                  },
                                  {
                                      "name":  "jira-users",
                                      "html":  "jira-\u003cb\u003eusers\u003c/b\u003e",
                                      "labels":  [

                                                 ]
                                  }
                              ]
               }
}

我认为结果是试图给我 JIRA 和 Confluence 用户 API 的 URL;但我无法弄清楚这些相对 URL 是如何映射到根 URL 的(我尝试在 URL 中的各个位置附加,所有这些都给我一个404dead link错误)。

4

2 回答 2

1

以下调用中的查询参数是对姓名或电子邮件地址的搜索查询。参考:https ://docs.atlassian.com/jira/REST/cloud/#api/2/groupuserpicker 。

您可以使用maxResults参数来获得超过 50 个结果。

遗憾的是,这个 REST API 调用不会在一次调用中为您提供所有用户。

我知道使用 Jira 获取所有用户的唯一方法是通过起始字母拨打一个电话(迭代每个字母):

GET .../rest/api/2/user/search?username=a&maxResults=1000
GET .../rest/api/2/user/search?username=b&maxResults=1000
GET .../rest/api/2/user/search?username=c&maxResults=1000
...

参考:https ://docs.atlassian.com/jira/REST/cloud/#api/2/user-findUsers

示例代码

function Get-AtlassianCloudUsers {
    [CmdletBinding()]
    param (
        [Parameter(Mandatory)][string]$Tenant
        ,
        [Parameter(Mandatory)][System.Management.Automation.Credential()]$Credential
        ,
        [Parameter(Mandatory=$false)][string]$UserFilter = '%' 
        ,
        [Parameter(Mandatory=$false)][int]$MaxResults = 9999
    )
    process {
        #refer to http://stackoverflow.com/questions/40424377/get-a-list-of-users-from-atlassians-cloud-on-demand-service for additional notes
        [string]$uri = 'https://{0}.atlassian.net/rest/api/2/user/search?username={1}&maxResults={2}' -f $Tenant, $UserFilter, $MaxResults
        Invoke-RestMethod -Method Get -Uri $Uri -Credential $credential | select -Expand syncRoot | Select-Object name, displayName, active, self 
        #| ConvertTo-Json -Depth 5
    }
}

Get-AtlassianCloudUsers -Tenant 'MyCompany' -credential (Get-Credential 'MyUsername') | ft -AutoSize
于 2016-11-07T13:41:30.313 回答
0

作为替代答案,我最近在 GitHub 上发现了 PSJira 项目:https ://github.com/replicaJunction/PSJira 。

这个库围绕 JIRA 服务提供了一组很好的包装函数,并且似乎有很好的文档和维护。

要达到上述要求,请执行以下步骤:

安装包:

配置 PSJira:

  • Set-JiraConfigServer -Server "https://$Tenant.atlassian.net"(分配$Tenant给您的实例的名称)

使用:

  • 为您的 Jira/Atlassian 帐户创建凭据:$cred = get-credential $JiraUsername
  • 获取用户列表:Get-JiraUser -UserName '%' -IncludeInactive -Credential $cred | select Name, DisplayName, Active, EmailAddress
于 2017-02-07T09:32:03.063 回答