-
Notifications
You must be signed in to change notification settings - Fork 49
/
Get-WarrantyInformation.ps1
360 lines (281 loc) · 9.75 KB
/
Get-WarrantyInformation.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#Requires -Version 7
<#
_author_ = Grant Curell <grant_curell@dell.com>
Copyright (c) 2022 Dell EMC Corporation
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
#>
<#
.SYNOPSIS
Retrieves the warranty information for all devices on an OME instance.
.DESCRIPTION
You can provide a keyword argument to filter devices by the service description. For example you can specify 'pro'
and that would match a Service Level Description of 'Silver Support or ProSupport'
For authentication X-Auth is used over Basic Authentication Note that the credentials entered are not stored to disk.
.PARAMETER IpAddress
IP Address of the OME Appliance
.PARAMETER Credentials
Credentials used to talk to the OME Appliance
.PARAMETER OutFile
Path to which you want to write the output.
.PARAMETER WarrantyKeyword
Performs a case insensitive search against the field 'Service Level Description' in the OME UI. This allows you
to search for a specific type of warranty. For example, searching prosupport would return all warranties with the word prosupport in their description.
.EXAMPLE
.\Get-WarrantyInformation.ps1' -IpAddress 192.168.1.93 -credentials $creds -outfile test.csv -WarrantyKeyword silver
#>
[CmdletBinding()]
param(
[Parameter(Mandatory)]
[System.Net.IPAddress] $IpAddress,
[Parameter(Mandatory)]
[pscredential] $Credentials,
[Parameter(Mandatory=$false)]
[string] $OutFile,
[Parameter(Mandatory=$false)]
[string] $WarrantyKeyword
)
function Get-Data {
<#
.SYNOPSIS
Used to interact with API resources
.DESCRIPTION
This function retrieves data from a specified URL. Get requests from OME return paginated data. The code below
handles pagination. This is the equivalent in the UI of a list of results that require you to go to different
pages to get a complete listing.
.PARAMETER Url
The API url against which you would like to make a request
.PARAMETER OdataFilter
An optional parameter for providing an odata filter to run against the API endpoint.
.PARAMETER MaxPages
The maximum number of pages you would like to return
.INPUTS
None. You cannot pipe objects to Get-Data.
.OUTPUTS
dict. A dictionary containing the results of the API call or an empty dictionary in the case of a failure
#>
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]
$Url,
[Parameter(Mandatory = $false)]
[string]
$OdataFilter,
[Parameter(Mandatory = $false)]
[int]
$MaxPages = $null
)
$Data = @()
$NextLinkUrl = $null
try {
if ($PSBoundParameters.ContainsKey('OdataFilter')) {
$CountData = Invoke-RestMethod -Uri $Url"?`$filter=$($OdataFilter)" -Method Get -Credential $Credentials -SkipCertificateCheck
if ($CountData.'@odata.count' -lt 1) {
Write-Error "No results were found for filter $($OdataFilter)."
return @{}
}
}
else {
$CountData = Invoke-RestMethod -Uri $Url -Method Get -Credential $Credentials -ContentType $Type `
-SkipCertificateCheck
}
if ($null -ne $CountData.'value') {
$Data += $CountData.'value'
}
else {
$Data += $CountData
}
if ($CountData.'@odata.nextLink') {
$NextLinkUrl = "https://$($IpAddress)$($CountData.'@odata.nextLink')"
}
$i = 1
while ($NextLinkUrl) {
if ($MaxPages) {
if ($i -ge $MaxPages) {
break
}
$i = $i + 1
}
$NextLinkData = Invoke-RestMethod -Uri "$($NextLinkUrl)" -Method Get -Credential $Credentials `
-ContentType $Type -SkipCertificateCheck
if ($null -ne $NextLinkData.'value') {
$Data += $NextLinkData.'value'
}
else {
$Data += $NextLinkData
}
if ($NextLinkData.'@odata.nextLink') {
$NextLinkUrl = "https://$($IpAddress)$($NextLinkData.'@odata.nextLink')"
}
else {
$NextLinkUrl = $null
}
}
return $Data
}
catch [System.Net.Http.HttpRequestException] {
Write-Error "There was a problem connecting to OME or the URL supplied is invalid. Did it become unavailable?"
return @{}
}
}
function Read-Confirmation {
<#
.SYNOPSIS
Prompts a user with a yes or no question
.DESCRIPTION
Prompts a user with a yes or no question. The question text should include something telling the user
to type y/Y/Yes/yes or N/n/No/no
.PARAMETER QuestionText
The text which you want to display to the user
.OUTPUTS
Returns true if the user enters yes and false if the user enters no
#>
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]
$QuestionText
)
do {
$Confirmation = (Read-Host $QuestionText).ToUpper()
} while ($Confirmation -ne 'YES' -and $Confirmation -ne 'Y' -and $Confirmation -ne 'N' -and $Confirmation -ne 'NO')
if ($Confirmation -ne 'YES' -and $Confirmation -ne 'Y') {
return $false
}
return $true
}
function Confirm-IsValid {
<#
.SYNOPSIS
Tests whether a filepath is valid or not.
.DESCRIPTION
Performs different tests depending on whether you are testing a file for the ability to read
(InputFilePath) or write (OutputFilePath)
.PARAMETER OutputFilePath
The path to an output file you want to test
.PARAMETER InputFilePath
The path to an input file you want to test
.OUTPUTS
Returns true if the path is valid and false if it is not
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[string]
$OutputFilePath,
[Parameter(Mandatory = $false)]
[string]
$InputFilePath
)
if ($PSBoundParameters.ContainsKey('InputFilePath') -and $PSBoundParameters.ContainsKey('OutputFilePath')) {
Write-Error "You can only provide either an InputFilePath or an OutputFilePath."
Exit
}
# Some of the tests are the same - we can use the same variable name
if ($PSBoundParameters.ContainsKey('InputFilePath')) {
$OutputFilePath = $InputFilePath
}
if ($PSBoundParameters.ContainsKey('InputFilePath')) {
if (-not $(Test-Path -Path $InputFilePath -PathType Leaf)) {
Write-Error "The file $($InputFilePath) does not exist."
return $false
}
}
else {
if (Test-Path -Path $OutputFilePath -PathType Leaf) {
if (-not $(Read-Confirmation "$($OutputFilePath) already exists. Do you want to continue? (Y/N)")) {
return $false
}
}
}
$ParentPath = $(Split-Path -Path $OutputFilePath -Parent)
if ($ParentPath -ne "") {
if (-not $(Test-Path -PathType Container $ParentPath)) {
Write-Error "The path '$($OutputFilePath)' does not appear to be valid."
return $false
}
}
if (Test-Path $(Split-Path -Path $OutputFilePath -Leaf) -PathType Container) {
Write-Error "You must provide a filename as part of the path. It looks like you only provided a folder in $($OutputFilePath)!"
return $false
}
return $true
}
function ConvertPSObjectToHashtable
{
<#
.SYNOPSIS
Converts a PSObject to a HashTable
.DESCRIPTION
Often, when we get input back from the API we want to be able to manipulate the output as a hashtable rather
than a PSCustomObject. This function will take as input a PSObject and convert it to a hashtable. When data
is converted using ConvertFromJson that requires some extra handling.
Note: This was shamelessly stolen from @Dave Wyatt's answere here:
https://stackoverflow.com/questions/22002748/hashtables-from-convertfrom-json-have-different-type-from-powershells-built-in-h
.PARAMETER InputObject
The PSObject you would like to convert.
.OUTPUTS
A HashTable equivalent of the input PSObject.
#>
param (
[Parameter(ValueFromPipeline)]
$InputObject
)
process
{
if ($null -eq $InputObject) { return $null }
if ($InputObject -is [System.Collections.IEnumerable] -and $InputObject -isnot [string])
{
$Collection = @(
foreach ($object in $InputObject) { ConvertPSObjectToHashtable $object }
)
Write-Output -NoEnumerate $Collection
}
elseif ($InputObject -is [psobject])
{
$Hash = @{}
foreach ($Property in $InputObject.PSObject.Properties)
{
$Hash[$Property.Name] = ConvertPSObjectToHashtable $Property.Value
}
return $Hash
}
else
{
return $InputObject
}
}
}
try {
Write-Host "Sending the request to OME..."
$WarrantyInfo = ConvertPSObjectToHashtable $(Get-Data "https://$($IpAddress)/api/WarrantyService/Warranties")
if ($PSBoundParameters.ContainsKey('WarrantyKeyword')) {
$WarrantyInfo = $WarrantyInfo | Where-Object 'ServiceLevelDescription' -Match ^*$WarrantyKeyword*
}
if($WarrantyInfo.count -gt 0) {
if ($PSBoundParameters.ContainsKey('OutFile')) {
if (-not $(Confirm-IsValid -OutputFilePath $OutFile)) {
Exit
}
$WarrantyInfo | Export-Csv -Path $OutFile -NoTypeInformation
$(Foreach($Case in $WarrantyInfo){
New-object psobject -Property $Case
}) | Export-Csv $OutFile
}
else {
$WarrantyInfo
}
}
Write-Host "Task completed successfully!"
}
catch {
Write-Error "Exception occured at line $($_.InvocationInfo.ScriptLineNumber) - $($_.Exception.Message)"
}