Public/New-bConnectStaticGroup.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
59
60
61
62
Function New-bConnectStaticGroup() {
    <#
        .Synopsis
            Create a new StaticGroup.
        .Parameter Name
            Name of the StaticGroup.
        .Parameter ParentGuid
            Valid GUID of the parent OrgUnit in hierarchy (default: "Static Groups").
        .Parameter Endpoints
            Array of Endpoints or single Endpoint.
        .Parameter Comment
            Comment for the StaticGroup.
        .Outputs
            StaticGroup (see bConnect documentation for more details).
    #>


    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'low')]
    [OutputType("System.Management.Automations.PSObject","System.Boolean")]
    Param (
        [Parameter(Mandatory=$true)][string]$Name,
        [string]$ParentGuid = "5020494B-04D3-4654-A256-80731E953746", #guid of "Static Groups" as fallback
        [PSCustomObject[]]$Endpoints,
        [string]$Comment
    )

    $_connectVersion = Get-bConnectVersion
    If($_connectVersion -ge "1.0") {
        $_body = @{
            Name = $Name;
            ParentId = $ParentGuid;
        }

        If(![string]::IsNullOrEmpty($Endpoints)) {
            $_endpointIds = @()
            Foreach($_ep in $Endpoints) {
                $_endpointIds += $_ep.Id
            }
            $_body += @{
                EndpointIds  = $_endpointIds
            }
        #if $Endpoints is just a single EP Object instead of an Array of EPs
        }elseif ((($Endpoints -is [array])-and $Endpoints.Count -gt 0)) {
            $_endpointIds = @($Endpoints.Id)
            $_body += @{
                EndpointIds  = $_endpointIds
            }
        }

        If(![string]::IsNullOrEmpty($Comment)) {
            $_body += @{ Comment = $Comment }
        }

        if($PSCmdlet.ShouldProcess($_body.Name, "Create new static group.")){
            return Invoke-bConnectPost -Controller "StaticGroups" -Version $_connectVersion -Data $_body
        } else {
            return $false
        }
    } else {
        return $false
    }
}