Public/New-bConnectDynamicGroup.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
Function New-bConnectDynamicGroup() {
    <#
        .Synopsis
            Create a new DynamicGroup.
        .Parameter Name
            Name of the DynamicGroup.
        .Parameter ParentGuid
            Valid GUID of the parent OrgUnit in hierarchy (default: "Dynamic Groups").
        .Parameter Statement
            Valid SQL Statement ("SELECT * FROM machine " will be automatically added).
        .Parameter Comment
            Comment for the DynamicGroup.
        .Outputs
            DynamicGroup (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 = "BDE918DC-89C0-458A-92F7-0BB9147A2706", #guid of "Dynamic Groups" as fallback
        [Parameter(Mandatory=$true)][string]$Statement,
        [string]$Comment
    )

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

        If($Statement -imatch "WHERE") {
            $_body += @{ Statement = $Statement }
        } else {
            return $false
        }

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

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