-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibUIrest.ps1
142 lines (115 loc) · 3.37 KB
/
libUIrest.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
# libUIrest
#
# Functions that do REST methods for Unifi Controller
# NAME :
# PURPOSE :
# INPUTS : []
# OUTPUTS : []
function New-UnifiRESTHeader
{
param(
$uiHost = $null,
$uriPath = $null,
$method = "GET",
$body = $null,
$session = $null,
$SessionVariable = $null,
$SkipCertificateCheck = $false,
$SkipHeaderValidation = $false
)
# find the Unifi host
if (-NOT $uiHost)
{
$uiHost = $script:defaultHost
}
# build the base header
$hdrSplat = @{
Uri = "https://$uiHost/api/$uriPath"
ContentType = "application/json"
Method = $method
}
# add the WebSession (auth) when we aren't logging in
if (-NOT $session -and $uriPath -notmatch 'login')
{
if (-NOT $script:defaultSession)
{
Write-Error "No Unifi session provided and there is no default session. Please use New-UnifiSession to create a login session."
return $null
}
$session = $script:defaultSession
}
if ($session -and $uriPath -notmatch 'login')
{
$hdrSplat.Add('WebSession', $session)
}
# header body
if ($body)
{
$hdrSplat.Add('Body', $body)
}
# SessionVariable is used to return the login information
if ($SessionVariable)
{
$hdrSplat.Add('SessionVariable', $SessionVariable)
}
# for self-signed, untrusted certs, this is needed
if ($SkipCertificateCheck -or $script:SkipCertificateCheck)
{
$hdrSplat.Add('SkipCertificateCheck', $true)
}
# hopefully this isn't needed, but just in case
if ($SkipHeaderValidation -or $script:SkipHeaderValidation)
{
$hdrSplat.Add('SkipHeaderValidation', $true)
}
return $hdrSplat
} #end
# NAME : Invoke-UnifiRESTMethod
# PURPOSE :
# INPUTS : []
# OUTPUTS : []
function Invoke-UnifiRESTMethod
{
param(
[hashtable]$headerSplat
)
if ($headerSplat -isnot [hashtable])
{
Write-Error ("The header must be a hashtable. Please use New-UnifiRESTHeader to create the UniFI REST header.")
return $null
}
# the session cookies will be stored in $uiSession
try
{
$result = Invoke-RestMethod @headerSplat -EA Stop
}
catch
{
Write-Error "Failed to logon to $uiHost`: `n $_"
return $null
}
if ($headerSplat.Uri -match "login")
{
return $uiSession
}
else
{
return $result.Data
}
} #end Invoke-UnifiRESTMethod
### LOGON ###
# NAME : New-UnifiLoginBody
# PURPOSE : Creates the Login Json text
# INPUTS : [PSCredential] - UniFi username and password
# OUTPUTS : [string] - A properly structured login string. WARNING!!! This string contains the plaintext password.
function New-UnifiLoginBody
{
param ([PSCredential]$uiLogon)
# do not modify the spacing. the body must be an exact match or the call will fail.
$uiBody = [PSCustomObject]@{
username = "$($uiLogon.UserName)"
password = "$($uiLogon.GetNetworkCredential().Password)"
remember = $true
} | ConvertTo-Json
return $uiBody
} #end New-UnifiLoginBody