Public/Get-bConnectOrgUnit.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
Function Get-bConnectOrgUnit() {
    <#
        .Synopsis
            Get specified OrgUnit.
        .Parameter OrgUnit
            Name (wildcards supported) or GUID of the OrgUnit.
        .Parameter SubGroups
            List subgroups of given OrgUnit.
            Only used if parameter "name" is a valid GUID.
        .Outputs
            Array of OrgUnit (see bConnect documentation for more details)
    #>


    Param(
        [Parameter(Mandatory=$true)][string]$OrgUnit,
        [switch]$SubGroups
    )

    $_connectVersion = Get-bConnectVersion
    If($_connectVersion -ge "1.0") {
        If(Test-Guid $OrgUnit) {
            If($SubGroups) {
                $_body = @{
                    OrgUnit = $OrgUnit
                }
            } else {
                $_body = @{
                    Id = $OrgUnit
                }
            }

            return Invoke-bConnectGet -Controller "OrgUnits" -Data $_body -Version $_connectVersion
        } else {
            # fetching orgunits with name is not supported; therefore we need a workaround for getting the specified orgunit...
            $_bmsVersion = Get-bConnectInfo
            If($_bmsVersion.bMSVersion -imatch "16.*") {
                # Search available since bMS 2016R1
                $_groups = Search-bConnectOrgUnit -Term $OrgUnit

                return $_groups
            } else {
                   # no search, so keep looping ;)
                $_groups = Invoke-bConnectGet -Controller "OrgUnits" -Version $_connectVersion
                $_ret = @()
                Foreach($_group in $_groups) {
                    If($_group.Name -match $orgUnit) {
                        $_ret += $_group
                    }
                }

                return $_ret
            }
        }
    } else {
        return $false
    }
}