Redhalo

How to monitor Dell warranty expiration with Powershell and PRTG

I have recently created my first repository on Github to share a powershell script under the MIT license.

It has been selected by PRTG hub so I believe my code is not so much gross and it can be introduced here with a blog post.

the purpose of the script

The script is actually used for creating a custom monitoring sensor on PRTG.

It allows to monitor the remaining days of a dell device warranty based on his service tag.

Basically the script send a GET request to the Dell TechDirect API to fetch a json file which include the expired warranty date of the hardware.

review of the code

The first thing to do is to authenticate ourselves to the API.

We can do that with an OAuth2 Access Token in an authorization request header field.

For additionnal information, you can refer to the official documentation that you can download here.

So we get our token with an Invoke-RestMethod command which contains the dell endpoint url and a body including the grant type and our API credential

$dell_api_id = 'XXXXXXXXXXXXXXXXXXXXXXXX'
$dell_api_secret = 'XXXXXXXXXXXXXXXXXXXXXXXX'
$access_token_endpoint_url = 'https://apigtwb2c.us.dell.com/auth/oauth/v2/token'
$requestBody = @{
grant_type = "client_credentials"
client_id = $dell_api_id
client_secret = $dell_api_secret
}
$authResponse = Invoke-RestMethod -Method Post -Uri $access_token_endpoint_url -Body $requestBody -ContentType "application/x-www-form-urlencoded"
$token = $authResponse.access_token

Since we have our token, we can now fetch the json data with again a Invoke-RestMethod. This time, the command includes our bearer token in the header for authentication and a body with the dell service tag.

The json data is then filtered to keep the only information which we are interrested to and it is saved into the variable $endsupport

$api_url = "https://apigtwb2c.us.dell.com/PROD/sbil/eapi/v5/asset-entitlements"
$headers = @{"Authorization" = "Bearer $token" }
$body = @{
    servicetags = $tag
}
$json = Invoke-RestMethod -URI $api_url -Method GET -contenttype 'Application/json' -Headers $headers -body $body
$endsupport = $json.entitlements.endDate | Sort-Object -Descending | Select-Object -First 1

This how looks like the json return:

[
  {
    "id": 180120972,
    "serviceTag": "CARV007",
    "orderBuid": 11,
    "shipDate": "2016-08-22T05:00:00Z",
    "productCode": "^`004",
    "localChannel": "US_19",
    "productId": null,
    "productLineDescription": "POWER EDGE R610",
    "productFamily": null,
    "systemDescription": null,
    "productLobDescription": "PowerEdge",
    "countryCode": null,
    "duplicated": false,
    "invalid": false,
    "entitlements": [
     {
       "itemNumber": "925-0200",
       "startDate": "2016-08-18T05:00:00Z",
       "endDate": "2017-08-19T04:59:59.999Z",
       "entitlementType": "INITIAL",
       "serviceLevelCode": "ND",
       "serviceLevelDescription": "C, NBD ONSITE",
       "serviceLevelGroup": 5
     }
    ]
  }
]

We must calculate now the time span between today’s date and the expired date

$today = Get-Date
$timespan = (New-TimeSpan -Start $today -End $endsupport).Days

We store today’s date into the variable $today, then we calculate the time span with New-Timespan and we keep only the value Days (I don’t care about hours, minutes and seconds..)

At the end, we create a result table with the XML format to create our sensor channel into PRTG

$channel_name = 'Remaining Support Days'
$custom_unit = 'Days'
$limit_min_error = '30'
$limit_min_warning = '75'
$limit_warning_msg = 'Less than 75 days remaining !'
$limit_error_msg = 'Less than 30 days remaining !'
Write-Host
"<prtg>"
    "<result>"
        "<channel>$channel_name</channel>"
        "<CustomUnit>$custom_unit</CustomUnit>"
        "<mode>Absolute</mode>"
        "<showChart>1</showChart>"
        "<showTable>1</showTable>"
        "<warning>0</warning>"
        "<float>1</float>"
        "<value>$timespan</value>"
        "<LimitMinError>$limit_min_error</LimitMinError>"
        "<LimitMinWarning>$limit_min_warning</LimitMinWarning>"
        "<LimitWarningMsg>$limit_warning_msg</LimitWarningMsg>"
        "<LimitErrorMsg>$limit_error_msg</LimitErrorMsg>"
        "<LimitMode>1</LimitMode>"
     "</result>"
"</prtg>"

The options available for the XML output can be reviewed here.

The final visual result looks like:

My PRTG remote probe is behind a proxy therefor I had to use the [system.net.webrequest]::defaultwebproxy property to define it.

$proxy = 'http://XXXXXXXXXXXXXXXXXXXXXXXX:XX'
[system.net.webrequest]::defaultwebproxy = new-object system.net.webproxy($proxy)
[system.net.webrequest]::defaultwebproxy.credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
[system.net.webrequest]::defaultwebproxy.BypassProxyOnLocal = $true

I also had the following error message the first I have tried to run my script:

Invoke-RestMethod : The request was aborted: Could not create SSL/TLS secure channel.

By googling I have read that Powershell is using by default TLS 1.0 when the dell TechDirect API requires a newer protocol.

I used [Net.ServicePointManager]::SecurityProtocol property to select the protocol required.

The code below allows to use all protocols but you can specify single one:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls, [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12, [Net.SecurityProtocolType]::Ssl3
[Net.ServicePointManager]::SecurityProtocol = "Tls, Tls11, Tls12, Ssl3"

The creation of the PRTG sensor is then explained on my github here and you have here the full code source of the script.

If you have any idea to improve the script or to add new feature, do not hesitate to let it know on my github.