Skip to content

Commit c9aae72

Browse files
Merge pull request gavsto#18 from intellicomp/Get-AutomateClient
Create Get-AutomateClient.ps1
2 parents 94a536e + 3b0805e commit c9aae72

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

Public/Get-AutomateClient.ps1

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
function Get-AutomateClient {
2+
<#
3+
.SYNOPSIS
4+
Get Client information out of the Automate API
5+
.DESCRIPTION
6+
Connects to the Automate API and returns one or more full client objects
7+
.PARAMETER AllClients
8+
Returns all clients in Automate, regardless of amount
9+
.PARAMETER Condition
10+
A custom condition to build searches that can be used to search for specific things. Supported operators are '=', 'eq', '>', '>=', '<', '<=', 'and', 'or', '()', 'like', 'contains', 'in', 'not'.
11+
The 'not' operator is only used with 'in', 'like', or 'contains'. The '=' and 'eq' operator are the same. String values can be surrounded with either single or double quotes. IE (RemoteAgentLastContact <= 2019-12-18T00:50:19.575Z)
12+
Boolean values are specified as 'true' or 'false'. Parenthesis can be used to control the order of operations and group conditions.
13+
.PARAMETER OrderBy
14+
A comma separated list of fields that you want to order by finishing with either an asc or desc.
15+
.PARAMETER ClientName
16+
Client name to search for, uses wildcards so full client name is not needed
17+
.PARAMETER LocationName
18+
Location name to search for, uses wildcards so full location name is not needed
19+
.PARAMETER ClientID
20+
ClientID to search for, integer, -ClientID 1
21+
.PARAMETER LocationID
22+
LocationID to search for, integer, -LocationID 2
23+
.OUTPUTS
24+
Client objects
25+
.NOTES
26+
Version: 1.0
27+
Author: Gavin Stone and Andrea Mastellone
28+
Creation Date: 2019-03-19
29+
Purpose/Change: Initial script development
30+
.EXAMPLE
31+
Get-AutomateClient -AllClients
32+
.EXAMPLE
33+
Get-AutomateClient -ClientId 4
34+
.EXAMPLE
35+
Get-AutomateClient -ClientName "Rancor"
36+
.EXAMPLE
37+
Get-AutomateClient -Condition "(City != 'Baltimore')"
38+
#>
39+
param (
40+
[Parameter(Mandatory = $false, ParameterSetName = "AllResults")]
41+
[switch]$AllClients,
42+
43+
[Parameter(Mandatory = $false, ParameterSetName = "ByCondition")]
44+
[string]$Condition,
45+
46+
[Parameter(Mandatory = $false, ParameterSetName = "CustomBuiltCondition")]
47+
[Parameter(Mandatory = $false, ParameterSetName = "AllResults")]
48+
[Parameter(Mandatory = $false, ParameterSetName = "ByCondition")]
49+
[string]$IncludeFields,
50+
51+
[Parameter(Mandatory = $false, ParameterSetName = "CustomBuiltCondition")]
52+
[Parameter(Mandatory = $false, ParameterSetName = "AllResults")]
53+
[Parameter(Mandatory = $false, ParameterSetName = "ByCondition")]
54+
[string]$ExcludeFields,
55+
56+
[Parameter(Mandatory = $false, ParameterSetName = "CustomBuiltCondition")]
57+
[Parameter(Mandatory = $false, ParameterSetName = "AllResults")]
58+
[Parameter(Mandatory = $false, ParameterSetName = "ByCondition")]
59+
[string]$OrderBy,
60+
61+
[Alias("Client")]
62+
[Parameter(Mandatory = $false, ParameterSetName = "CustomBuiltCondition")]
63+
[string]$ClientName,
64+
65+
[Parameter(Mandatory = $false, ParameterSetName = "CustomBuiltCondition")]
66+
[int]$ClientId,
67+
68+
[Parameter(Mandatory = $false, ParameterSetName = "CustomBuiltCondition")]
69+
[int]$LocationId,
70+
71+
[Alias("Location")]
72+
[Parameter(Mandatory = $false, ParameterSetName = "CustomBuiltCondition")]
73+
[string]$LocationName
74+
)
75+
76+
$ArrayOfConditions = @()
77+
78+
if ($AllClients) {
79+
Return Get-AutomateAPIGeneric -AllResults -Endpoint "clients" -IncludeFields $IncludeFields -ExcludeFields $ExcludeFields -OrderBy $OrderBy
80+
}
81+
82+
if ($Condition) {
83+
Return Get-AutomateAPIGeneric -AllResults -Endpoint "clients" -Condition $Condition -IncludeFields $IncludeFields -ExcludeFields $ExcludeFields -OrderBy $OrderBy
84+
}
85+
86+
if ($ClientName) {
87+
$ArrayOfConditions += "(Name like '%$ClientName%')"
88+
}
89+
90+
if ($LocationName) {
91+
$ArrayOfConditions += "(Location.Name like '%$LocationName%')"
92+
}
93+
94+
if ($ClientID) {
95+
$ArrayOfConditions += "(Id = $ClientId)"
96+
}
97+
98+
if ($LocationID) {
99+
$ArrayOfConditions += "(Location.Id = $LocationId)"
100+
}
101+
102+
$ClientFinalCondition = Get-ConditionsStacked -ArrayOfConditions $ArrayOfConditions
103+
104+
$Clients = Get-AutomateAPIGeneric -AllResults -Endpoint "clients" -Condition $ClientFinalCondition -IncludeFields $IncludeFields -ExcludeFields $ExcludeFields -OrderBy $OrderBy
105+
106+
$FinalResult = @()
107+
foreach ($Client in $Clients) {
108+
$ArrayOfConditions = @()
109+
$ArrayOfConditions += "(Client.Id = '$($Client.Id)')"
110+
$LocationFinalCondition = Get-ConditionsStacked -ArrayOfConditions $ArrayOfConditions
111+
$Locations = Get-AutomateAPIGeneric -AllResults -Endpoint "locations" -Condition $LocationFinalCondition -IncludeFields $IncludeFields -ExcludeFields $ExcludeFields -OrderBy $OrderBy
112+
$FinalClient = $Client
113+
Add-Member -inputobject $FinalClient -NotePropertyName 'Locations' -NotePropertyValue $locations
114+
$FinalResult += $FinalClient
115+
}
116+
117+
return $FinalResult
118+
}

0 commit comments

Comments
 (0)