Public/Edit-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
Function Edit-bConnectStaticGroup() {
    <#
        .Synopsis
            Updates a existing StaticGroup.
        .Parameter StaticGroup
            Valid modified StaticGroup
        .Outputs
            StaticGroup (see bConnect documentation for more details).
    #>



    Param (
        [Parameter(Mandatory=$true)][PSCustomObject]$StaticGroup
    )

    $_connectVersion = Get-bConnectVersion
    If($_connectVersion -ge "1.0") {
        If(Test-Guid $StaticGroup.Id) {
            # bms2016r1
            # We can not send the whole object because of not editable fields.
            # So we need to create a new one with editable fields only...
            # And as this might be too easy we face another problem: we are only allowed to send the changed fields :(
            # Dirty workaround: reload the object and compare new vs. old
            $_old_group = Get-bConnectStaticGroup -StaticGroup $StaticGroup.Id
            $_old_group = ConvertTo-Hashtable $_old_group

            $_new_group = @{ Id = $StaticGroup.Id }
            $_propertyList = @(
                "ParentId",
                "Name",
                "EndpointIds",
                "Comment"
            )
            $StaticGroup = ConvertTo-Hashtable $StaticGroup

            $_endpointIds = @()
            Foreach($_ep in $StaticGroup.EndpointIds) {
                $_endpointIds += $_ep
            }
            $StaticGroup.EndpointIds = $_endpointIds

            Foreach($_property in $_propertyList) {
                If($StaticGroup[$_property] -ine $_old_group[$_property]) {
                    $_new_group += @{ $_property = $StaticGroup[$_property] }
                }
            }

            return Invoke-bConnectPatch -Controller "StaticGroups" -Version $_connectVersion -objectGuid $StaticGroup.Id -Data $_new_group
        } else {
            return $false
        }
    } else {
        return $false
    }
}