Public/DeviceFingerprint.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#
# Copyright 2021, Alexis La Goutte <alexis.lagoutte at gmail dot com>
#
# SPDX-License-Identifier: Apache-2.0
#

function Add-ArubaCPDeviceFingerprint {

    <#
        .SYNOPSIS
        Add a Device Fingerprint on ClearPass

        .DESCRIPTION
        Add a Device Fingerprint with mac address, hostname, ip (address) and device info (Category, Name, Family)

        .EXAMPLE
        Add-ArubaCPDeviceFingerprint -mac_address 000102030405 -hostname "My PowerArubaCP Device Fingerprint"

        Add a Device Fingerprint with MAC Address 000102030405 and a hostname

        .EXAMPLE
        Add-ArubaCPDeviceFingerprint -mac_address 000102030405 -ip_address 192.0.2.1

        Add a Device Fingerprint with MAC Address 000102030405 and an IP Address

        .EXAMPLE
        Add-ArubaCPDeviceFingerprint -mac_address 000102030405 -device_category Server -device_family ClearPass -device_name ClearPass VM

        Add a Device Fingerprint with MAC Address 000102030405 with device information (Category, Name, Family)
    #>


    Param(
        [Parameter (Mandatory = $true)]
        [string]$mac_address,
        [Parameter (Mandatory = $false)]
        [string]$hostname,
        [Parameter (Mandatory = $false)]
        [ipaddress]$ip_address,
        [Parameter (Mandatory = $false)]
        [string]$device_category,
        [Parameter (Mandatory = $false)]
        [string]$device_name,
        [Parameter (Mandatory = $false)]
        [string]$device_family,
        [Parameter (Mandatory = $False)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection = $DefaultArubaCPConnection
    )

    Begin {
    }

    Process {

        $uri = "api/device-profiler/device-fingerprint"

        $_dfp = new-Object -TypeName PSObject

        $_dfp | add-member -name "mac" -membertype NoteProperty -Value (Format-ArubaCPMacAddress $mac_address)

        if ( $PsBoundParameters.ContainsKey('hostname') ) {
            $_dfp | add-member -name "hostname" -membertype NoteProperty -Value $hostname
        }

        if ( $PsBoundParameters.ContainsKey('ip_address') ) {
            $_dfp | add-member -name "ip" -membertype NoteProperty -Value $ip_address.ToString()
        }

        $_device = new-Object -TypeName PSObject
        if ( $PsBoundParameters.ContainsKey('device_category') ) {
            $_device | add-member -name "category" -membertype NoteProperty -Value $device_category
        }

        if ( $PsBoundParameters.ContainsKey('device_name') ) {
            $_device | add-member -name "name" -membertype NoteProperty -Value $device_name
        }

        if ( $PsBoundParameters.ContainsKey('device_family') ) {
            $_device | add-member -name "family" -membertype NoteProperty -Value $device_family
        }

        $_dfp | add-member -name "device" -membertype NoteProperty -Value $_device

        $dfp = Invoke-ArubaCPRestMethod -method "POST" -body $_dfp -uri $uri -connection $connection
        $dfp
    }

    End {
    }
}

function Get-ArubaCPDeviceFingerprint {

    <#
        .SYNOPSIS
        Get Device Fingerprint info on CPPM

        .DESCRIPTION
        Get Device Fingerprint (hostname, ip, device category/name/family)

        .EXAMPLE
        Get-ArubaCPDeviceFingerprint -mac_address 000102030405

        Get Device FingerPrint about Endpoint 000102030405 Aruba on the ClearPass

        .EXAMPLE
        Get-ArubaCPEndpoint 000102030405 | Get-ArubaCPDeviceFingerprint

        Get Device FingerPrint using Endpoint 000102030405

        .EXAMPLE
        Get-ArubaCPDeviceFingerprint -ip_address 192.0.2.1

        Get Device FingerPrint about Endpoint 192.0.2.1 Aruba on the ClearPass

    #>


    Param(
        [Parameter (ParameterSetName = "mac_address", Mandatory = $true)]
        [string]$mac_address,
        [Parameter (ParameterSetName = "ip_address", Mandatory = $true)]
        [IPAddress]$ip_address,
        [Parameter (ParameterSetName = "endpoint", Mandatory = $true, ValueFromPipeline = $true)]
        [ValidateScript( { Confirm-ArubaCPEndpoint $_ })]
        [psobject]$endpoint,
        [Parameter (Mandatory = $false)]
        [ValidateNotNullOrEmpty()]
        [PSObject]$connection = $DefaultArubaCPConnection
    )

    Begin {
    }

    Process {

        if ($connection.version -lt [version]"6.9.0") {
            throw "Need ClearPass >= 6.9.0 for use this cmdlet"
        }

        $invokeParams = @{ }

        $uri = "api/device-profiler/device-fingerprint/"

        switch ( $PSCmdlet.ParameterSetName ) {
            "mac_address" {
                $uri += $mac_address
            }
            "ip_address" {
                $uri += $ip_address.ToString()
            }
            "endpoint" {
                $uri += $endpoint.mac_address
            }
            default { }
        }

        $dfp = Invoke-ArubaCPRestMethod -method "GET" -uri $uri @invokeParams -connection $connection
        $dfp
    }

    End {
    }
}