Last active
September 2, 2021 08:15
-
-
Save changbowen/ceca57097c1deb5a61e0909145f5a4f3 to your computer and use it in GitHub Desktop.
Dell API - Get Warranty Info from Service Tag
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Get-DellWarranty { | |
Param( | |
[Parameter(Mandatory,ValueFromPipeline)] | |
[String[]] $ServiceTag, | |
[Parameter(Mandatory)] | |
[string] $APIKey, | |
[Parameter(Mandatory)] | |
[string] $APISecret, | |
[ValidateSet('serviceLevelCode', 'serviceLevelDescription', 'serviceLevelGroup')] | |
[string]$EntitlementProperty = 'serviceLevelDescription' | |
) | |
begin { | |
$Results = [System.Collections.ArrayList]@() | |
$SecPro = [System.Collections.ArrayList]@() | |
$SecPro.AddRange(([System.Net.ServicePointManager]::SecurityProtocol -split ', ')) | |
if ($SecPro -notcontains [System.Net.SecurityProtocolType]::Tls12) { | |
$null = $SecPro.Add([System.Net.SecurityProtocolType]::Tls12) | |
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]$SecPro | |
} | |
try { | |
$Auth = Invoke-WebRequest -Uri ('https://apigtwb2c.us.dell.com/auth/oauth/v2/token?client_id={0}&client_secret={1}&grant_type=client_credentials' -f $APIKey, $APISecret) -Method Post -ErrorAction Stop | |
$AuthKey = ($Auth.Content | ConvertFrom-Json).access_token | |
} | |
catch { | |
[System.Management.Automation.ErrorRecord]$e = $_ | |
[PSCustomObject]@{ | |
Type = $e.Exception.GetType().FullName | |
Exception = $e.Exception.Message | |
Reason = $e.CategoryInfo.Reason | |
Target = $e.CategoryInfo.TargetName | |
Line = $e.InvocationInfo.ScriptLineNumber | |
Column = $e.InvocationInfo.OffsetInLine | |
} | |
throw $_ | |
} | |
} | |
process { | |
foreach ($STag in $ServiceTag) { | |
$Response = Invoke-WebRequest -Uri ('https://apigtwb2c.us.dell.com/PROD/sbil/eapi/v5/asset-entitlements?servicetags={0}&Method=Get' -f $STag) -Headers @{'Authorization'=('bearer {0}' -f $AuthKey);'Accept'='application/json'} | |
$Content = $Response.Content | ConvertFrom-Json | |
# $WarrantyStartDate = [datetime]::Parse($Content.Entitlements.StartDate[-1]) | |
# $WarrantyEndDate = [datetime]::Parse( $Content.Entitlements.EndDate[-1]) | |
# $WarrantyLevel = ($Content.Entitlements.ServiceLevelDescription)[-1] | |
$ShipDate = [datetime]::Parse($Content.ShipDate) | |
$Model = $Content.SystemDescription | |
$Summary = [ordered]@{ | |
ServiceTag = $STag | |
Model = $Model | |
ShipDate = $ShipDate.ToShortDateString() | |
} | |
$Content.Entitlements.ForEach{ | |
$Summary[$_.$EntitlementProperty] = $_.EndDate | |
} | |
$Results.Add([pscustomobject]$Summary) > $null | |
} | |
} | |
end { | |
return $Results | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment