-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibUIsite.ps1
149 lines (106 loc) · 2.69 KB
/
libUIsite.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
<#
Contains cmdlets for Unifi site related work.
#>
###############
## ##
## GETTERS ##
## ##
###############
#region
# NAME : Get-UnifiSite
# PURPOSE : Gets the details of the session user account.
# INPUTS : [WebRequestSession]$uiSession - The UI session from New-UnifiSession.
# OUTPUTS : [PSCustomObject] - An object table of user details.
function Get-UnifiSite
{
param($uiSession)
## connect to the UniFi Controller ##
$siteSplat = New-UnifiRESTHeader -uriPath 'self/sites' -session $uiSession
## make the REST API call ##
# the session cookies will be stored in $uiSession
try
{
$results = Invoke-UnifiRestMethod $siteSplat -EA Stop
}
catch
{
Write-Error "Failed to get sites from $uiHost`: `n $_"
return $null
}
return ($results)
} #end Get-UnifiSite
# NAME : Get-UnifiSiteStats
# PURPOSE : Gets the details of the session user account.
# INPUTS : [WebRequestSession]$uiSession - The UI session from New-UnifiSession.
# OUTPUTS : [PSCustomObject] - An object table of user details.
function Get-UnifiSiteStats
{
param($uiSession)
## connect to the UniFi Controller ##
$statSplat = New-UnifiRESTHeader -uriPath 'stat/sites' -session $uiSession
## make the REST API call ##
# the session cookies will be stored in $uiSession
try
{
$results = Invoke-UnifiRestMethod $statSplat -EA Stop
}
catch
{
Write-Error "Failed to get sites from $uiHost`: `n $_"
return $null
}
return ($results)
} #end Get-UnifiSiteStats
#endregion GETTERS
###############
## ##
## SETTERS ##
## ##
###############
#region
<#
Set-UnifiSite
{
# pending
}
#>
#endregion SETTERS
############
## ##
## MAIN ##
## ##
############
#region
function Select-UnifiSite
{
param($site = $null)
<#
Site selection priority:
1. Passed site.
2. Default site.
3. last_site_name from the current logged on user.
#>
if (-NOT $site)
{
if ($script:defaultSite)
{
$site = $script:defaultSite
}
else
{
# get the site of the logged on user
$site = (Get-UnifiUser).last_site_name
}
}
# make sure the site is valid before returning
$siteList = Get-UnifiSite
if ($site -in $siteList.name -or $site -in $siteList._id)
{
return $site
}
else
{
return $null
}
}
#endregion MAIN