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



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

    $_connectVersion = Get-bConnectVersion
    If($_connectVersion -ge "1.0") {
        If(Test-Guid $Endpoint.Id) {
            # 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_endpoint = Get-bConnectEndpoint -EndpointGuid $Endpoint.Id
            $_old_endpoint = ConvertTo-Hashtable $_old_endpoint

            # common properties
            $_new_endpoint = @{ Id = $Endpoint.Id }
            $_propertyList = @(
                "DisplayName",
                "GuidOrgUnit",
                "Comments"
            )
            $Endpoint = ConvertTo-Hashtable $Endpoint

            # Windows
            If($Endpoint.Type -eq [bConnectEndpointType]::WindowsEndpoint) {
                $_propertyList += @(
                    "HostName",
                    "Options",
                    "PrimaryMAC",
                    "Domain",
                    "GuidBootEnvironment",
                    "GuidHardwareProfile",
                    "PublicKey",
                    "Mode",
                    "ExtendedInternetMode",
                    "PrimaryUser",
                    "PrimaryIP",
                    "CustomStateText",
                    "CustomStateType"
                )
            }

            # BmsNet = Android, iOS, WP (deprecated!), OSX
            If(($Endpoint.Type -eq [bConnectEndpointType]::AndroidEndpoint) -or
                ($Endpoint.Type -eq [bConnectEndpointType]::iOSEndpoint) -or
                ($Endpoint.Type -eq [bConnectEndpointType]::WindowsPhoneEndpoint) -or
                ($Endpoint.Type -eq [bConnectEndpointType]::MacEndpoint)) {
                $_propertyList += @(
                    "PrimaryUser",
                    "Owner",
                    "ComplianceCheckCategory"
                )
            }

            # OSX
            If($Endpoint.Type -eq [bConnectEndpointType]::MacEndpoint) {
                $_propertyList += @(
                    "HostName"
                )
            }

            Foreach($_property in $_propertyList) {
                If($Endpoint[$_property] -ine $_old_endpoint[$_property]) {
                    $_new_endpoint += @{ $_property = $Endpoint[$_property] }
                }
            }

            return Invoke-bConnectPatch -Controller "Endpoints" -Version $_connectVersion -objectGuid $Endpoint.Id -Data $_new_endpoint
        } else {
            return $false
        }
    } else {
        return $false
    }
}