Functions/Public/Get-vRABusinessGroup.psm1

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
function Get-vRABusinessGroup {
<#
    .SYNOPSIS
    Retrieve vRA Business Groups
    
    .DESCRIPTION
    Retrieve vRA Business Groups
    
    .PARAMETER TenantId
    Specify the ID of a Tenant

    .PARAMETER Name
    Specify the Name of a Business Group

    .PARAMETER Limit
    The number of entries returned per page from the API. This has a default value of 100.

    .INPUTS
    System.String

    .OUTPUTS
    System.Management.Automation.PSObject.
    
    .EXAMPLE
    Get-vRABusinessGroup

    .EXAMPLE
    Get-vRABusinessGroup -TenantId Tenant01 -Name BusinessGroup01,BusinessGroup02
#>

[CmdletBinding()][OutputType('System.Management.Automation.PSObject')]

    Param (

    [parameter(Mandatory=$false)]
    [ValidateNotNullOrEmpty()]
    [String]$TenantId = $Global:vRAConnection.Tenant,
    
    [parameter(Mandatory=$false)]
    [ValidateNotNullOrEmpty()]
    [String[]]$Name,     
    
    [parameter(Mandatory=$false)]
    [ValidateNotNullOrEmpty()]
    [String]$Limit = "100"
    )
    
# --- Test for vRA API version
xRequires -Version 7.0
                
try {

    # --- Check the TenantId
    if ($PSBoundParameters.ContainsKey("TenantId")) {

        $TenantId = (Get-vRATenant -Id $TenantId).Id
    }

    # --- Get business group by name
    if ($PSBoundParameters.ContainsKey("Name")) {

        foreach ($BusinessGroupName in $Name){

            $URI = "/identity/api/tenants/$($TenantId)/subtenants?`$filter=name%20eq%20'$($BusinessGroupName)'"

            # --- Run vRA REST Request
            $Response = Invoke-vRARestMethod -Method GET -URI $URI
            
            $BusinessGroup = $Response.content
            
            if (-not $BusinessGroup){

                Write-Warning "Did not find Business Group $BusinessGroupName"
                break
            }

            # --- Get the role details
            $BusinessGroupRolesURI = "/identity/api/tenants/$($TenantId)/subtenants/$($BusinessGroup.id)/roles"

            # --- Run vRA REST Request
            $BusinessGroupRolesResponse = Invoke-vRARestMethod -Method GET -URI $BusinessGroupRolesURI

            $GroupManagerRole = $BusinessGroupRolesResponse.content | Where-Object {$_.name -eq "Business Group Manager"}
            $SupportUserRole = $BusinessGroupRolesResponse.content | Where-Object {$_.name -eq "Support User"}
            $UserRole = $BusinessGroupRolesResponse.content | Where-Object {$_.name -eq "Basic User"}

            [pscustomobject]@{

                Name = $BusinessGroup.name
                ID = $BusinessGroup.id
                Description = $BusinessGroup.description
                Roles = $BusinessGroup.subtenantRoles
                ExtensionData = $BusinessGroup.extensionData
                GroupManagerRole = $GroupManagerRole.principalId
                SupportUserRole = $SupportUserRole.principalId
                UserRole = $UserRole.principalId
                Tenant = $BusinessGroup.tenant
            }
        }
    }
    else {

        $URI = "/identity/api/tenants/$($TenantId)/subtenants?limit=$($Limit)"

        # --- Run vRA REST Request
        $Response = Invoke-vRARestMethod -Method GET -URI $URI

        foreach ($BusinessGroup in $Response.content){
            
            # --- Get the role details
            $BusinessGroupRolesURI = "/identity/api/tenants/$($TenantId)/subtenants/$($BusinessGroup.id)/roles"

            # --- Run vRA REST Request
            $BusinessGroupRolesResponse = Invoke-vRARestMethod -Method GET -URI $BusinessGroupRolesURI

            $GroupManagerRole = $BusinessGroupRolesResponse.content | Where-Object {$_.name -eq "Business Group Manager"}
            $SupportUserRole = $BusinessGroupRolesResponse.content | Where-Object {$_.name -eq "Support User"}
            $UserRole = $BusinessGroupRolesResponse.content | Where-Object {$_.name -eq "Basic User"}

            [pscustomobject]@{

                Name = $BusinessGroup.name
                ID = $BusinessGroup.id
                Description = $BusinessGroup.description
                Roles = $BusinessGroup.subtenantRoles
                ExtensionData = $BusinessGroup.extensionData
                GroupManagerRole = $GroupManagerRole.principalId
                SupportUserRole = $SupportUserRole.principalId
                UserRole = $UserRole.principalId
                Tenant = $BusinessGroup.tenant
            }
        }
    }
}
catch [Exception]{

    throw
}
}