Functions/Get-DellWarranty.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
function Get-DellWarranty {
    param(
        #Specifies the Dell Service Tag of the device we want to check
        [Parameter(Mandatory = $false,
                   ValueFromPipelineByPropertyName = $true,
                   ValueFromPipeline = $true,
                   Position = 0)]
        [Alias('SerialNumber')]
        [String] $AssetTag,
        #Optionally pass a hostname for easier reporting on the results
        [Parameter(Mandatory = $false,
                   ValueFromPipelineByPropertyName = $true,
                   ValueFromPipeline = $false,
                   Position = 1)]
        [Alias('PSComputerName','ComputerName')]
        [String] $Hostname,
        #For multiple lookups in one request use input object, AssetTag and Hostname must be in the object
        [psobject] $InputObject,
        #The API given to you by Dell Support
        [string] $APIKey,
        #If you have an API for sandbox testing use this switch
        [switch] $UseSandboxAPI
    )

    begin
    {
        # Small function to stop null or empty errors
        function ConvertTo-Date {
            Param($RawDate)
            if([string]::IsNullOrEmpty($RawDate)){
                return $RawDate
            }else{
                return [datetime]$RawDate
            }
        }
    }

    process
    {
        #Check if object is used and combine asset tags
        if($InputObject){
            $AssetTag = ($InputObject | Select-Object -ExpandProperty AssetTag) -join ','
        }

        #Dell Web Service URI
        if($UseSandboxAPI){
            [uri]$ServiceURI="https://sandbox.api.dell.com/support/assetinfo/v4/getassetwarranty/$($AssetTag)?apikey=$($APIKey)"
        }else{
            [uri]$ServiceURI="https://api.dell.com/support/assetinfo/v4/getassetwarranty/$($AssetTag)?apikey=$($APIKey)"
        }

        #Connect to web service proxy
        try
        {
            $WebRequestResult = (Invoke-WebRequest -Uri $ServiceURI -ErrorAction Stop).Content
        }
        catch
        {
            Write-Error "Unable to connect to Dell warranty API URI '$ServiceURI' - $_"
            return
        }

        #Build output list up
        $OutputHeaders = @(
            #User provided hostname
            'Hostname'
            #Asset header data
            'ServiceTag'
            'BUID'
            'CountryLookupCode'
            'CustomerNumber'
            'IsDuplicate'
            'ItemClassCode'
            'LocalChannel'
            'MachineDescription'
            'OrderNumber'
            'ParentServiceTag'
            'ShipDate'
            #Product data header
            'LOB'
            'LOBFriendlyName'
            'ProductFamily'
            'ProductId'
            'SystemDescription'
            #Entitlement data
            'EntitlementType'
            'ItemNumber'
            'ServiceLevelCode'
            'ServiceLevelDescription'
            'ServiceLevelGroup'
            'ServiceProvider'
            'StartDate'
            'EndDate'
        )

        try
        {
            if(-not $Hostname){
                $Hostname = 'Not Provided'
            }

            #Retrive Dell warranty information based on asset tag
            $DellInfoAll = ($WebRequestResult | ConvertFrom-Json).AssetWarrantyResponse
            
            #loop through results
            foreach($DellInfo IN $DellInfoAll){ 
                #If the Dell info has entitlements loop through each one else just return the single result
                if($DellInfo.AssetEntitlementData){
                    Foreach($Entitlement IN $DellInfo.AssetEntitlementData){
                        $Out = '' | Select-Object $OutputHeaders
                        $Out.Hostname = $Hostname

                        $Out.ServiceTag         = $DellInfo.AssetHeaderData.ServiceTag
                        $Out.BUID               = $DellInfo.AssetHeaderData.BUID
                        $Out.CountryLookupCode  = $DellInfo.AssetHeaderData.CountryLookupCode
                        $Out.CustomerNumber     = $DellInfo.AssetHeaderData.CustomerNumber
                        $Out.IsDuplicate        = $DellInfo.AssetHeaderData.IsDuplicate
                        $Out.ItemClassCode      = $DellInfo.AssetHeaderData.ItemClassCode
                        $Out.LocalChannel       = $DellInfo.AssetHeaderData.LocalChannel
                        $Out.MachineDescription = $DellInfo.AssetHeaderData.MachineDescription
                        $Out.OrderNumber        = $DellInfo.AssetHeaderData.OrderNumber
                        $Out.ParentServiceTag   = $DellInfo.AssetHeaderData.ParentServiceTag
                        $Out.ShipDate           = ConvertTo-Date $DellInfo.AssetHeaderData.ShipDate

                        $Out.LOB               = $DellInfo.ProductHeaderData.LOB
                        $Out.LOBFriendlyName   = $DellInfo.ProductHeaderData.LOBFriendlyName
                        $Out.ProductFamily     = $DellInfo.ProductHeaderData.ProductFamily
                        $Out.ProductId         = $DellInfo.ProductHeaderData.ProductId
                        $Out.SystemDescription = $DellInfo.ProductHeaderData.SystemDescription

                        $Out.EntitlementType         = $Entitlement.EntitlementType
                        $Out.ItemNumber              = $Entitlement.ItemNumber
                        $Out.ServiceLevelCode        = $Entitlement.ServiceLevelCode
                        $Out.ServiceLevelDescription = $Entitlement.ServiceLevelDescription
                        $Out.ServiceLevelGroup       = $Entitlement.ServiceLevelGroup
                        $Out.ServiceProvider         = $Entitlement.ServiceProvider
                        $Out.StartDate               = ConvertTo-Date $Entitlement.StartDate
                        $Out.EndDate                 = ConvertTo-Date $Entitlement.EndDate

                        $Out
                    }
                }else{
                    $Out = '' | Select-Object $OutputHeaders
                    $Out.Hostname = $Hostname

                    $Out.ServiceTag         = $DellInfo.AssetHeaderData.ServiceTag
                    $Out.BUID               = $DellInfo.AssetHeaderData.BUID
                    $Out.CountryLookupCode  = $DellInfo.AssetHeaderData.CountryLookupCode
                    $Out.CustomerNumber     = $DellInfo.AssetHeaderData.CustomerNumber
                    $Out.IsDuplicate        = $DellInfo.AssetHeaderData.IsDuplicate
                    $Out.ItemClassCode      = $DellInfo.AssetHeaderData.ItemClassCode
                    $Out.LocalChannel       = $DellInfo.AssetHeaderData.LocalChannel
                    $Out.MachineDescription = $DellInfo.AssetHeaderData.MachineDescription
                    $Out.OrderNumber        = $DellInfo.AssetHeaderData.OrderNumber
                    $Out.ParentServiceTag   = $DellInfo.AssetHeaderData.ParentServiceTag
                    $Out.ShipDate           = ConvertTo-Date $DellInfo.AssetHeaderData.ShipDate

                    $Out.LOB               = $DellInfo.ProductHeaderData.LOB
                    $Out.LOBFriendlyName   = $DellInfo.ProductHeaderData.LOBFriendlyName
                    $Out.ProductFamily     = $DellInfo.ProductHeaderData.ProductFamily
                    $Out.ProductId         = $DellInfo.ProductHeaderData.ProductId
                    $Out.SystemDescription = $DellInfo.ProductHeaderData.SystemDescription

                    $Out.EntitlementType         = $null
                    $Out.ItemNumber              = $null
                    $Out.ServiceLevelCode        = $null
                    $Out.ServiceLevelDescription = $null
                    $Out.ServiceLevelGroup       = $null
                    $Out.ServiceProvider         = $null
                    $Out.StartDate               = $null
                    $Out.EndDate                 = $null

                    $Out
                }
            }
        }
        catch
        {
            Write-Error "Failed to obtain asset information for '$Hostname' using asset tag '$AssetTag' - $_"
            return
        }
    }

}