Functions/Public/Get-BitBucketServerUser.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
function Get-BitBucketServerUser {
    [CmdletBinding()]
    param(
        [Parameter()]
        [switch]
        $Raw
        ,
        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [string]
        $User
    )

    $Groups = @()
    $Filter = ''
    if ($User) {
        $Filter = "?filter=$User"

        $Values = Invoke-BitBucketServerApi -Path "/admin/users/more-members?context=$User"
        $Groups = $Values.name
    }

    $Values = Invoke-BitBucketServerApi -Path "/admin/users$Filter"
    
    if ($Raw) {
        $Values
        return
    }

    $EpochStart = New-Object -Type DateTime -ArgumentList 1970, 1, 1, 0, 0, 0, 0
    foreach ($Item in $Values) {
        $Timestamp = $null
        if ($Item.lastAuthenticationTimestamp) {
            $Timestamp = $EpochStart.AddSeconds($Item.lastAuthenticationTimestamp / 1000)
        }
        [pscustomobject]@{
            DisplayName       = $Item.displayName
            Name              = $Item.name
            Address           = $Item.emailAddress
            Active            = $Item.active
            Directory         = $Item.directoryName
            LastAuthenticated = $Timestamp
            Groups            = $Groups
        }
    }
}