Exporting Assets from Freshservice using Powershell

I recently got a request to write an automation to check the warranty status of all our computers. Simply request, huh?

I decided the best way was to write a script that would export all the assets from our Freshservice instance and get the serial number of them. The next stage is to write a script that actually checks the warranty status with HP, but currently their warranty API is offline so that will come later. Right now we’re only exporting the assets and getting the serials from Freshservice.

You will need to find your API key in your profile for Fresh, and your instance URL which you already should now. The next thing to check for is if your ID for serial number is the same, in our case it’s “serial_number_26000476017” but I guess that may vary.

#################################################
# Export all assets and get serial to check with HP warranty
#################################################

# API Key
$FDApiKey="XXXXXX"

#################################################
# Prep
$pair = "$($FDApiKey):$($FDApiKey)"
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)
$basicAuthValue = "Basic $base64"
$FDHeaders = @{ Authorization = $basicAuthValue }
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::TLS12
# The Doing part
$FDAssetData = ""
$Output = @()
$i = 1
do
	{
		$FDAssetDatatmp = ""
		$FDBaseEndpointSummary = "https://<your fresh URL>/cmdb/items.json?page=$i"
		# You may want to write this out to check how many pages it loads
		# write-host $FDBaseEndpointSummary
		$FDAssetData = Invoke-WebRequest -uri $FDBaseEndpointSummary -Headers $FDHeaders -Method GET -ContentType application/json | ConvertFrom-Json
		$Output = $Output + $FDAssetData
		$i++
	} while($FDAssetData)
Write-host -foregroundcolor Cyan "$i pages imported..."

#Now let's go through every row and only filter out the HP computers and get the serial number.
foreach($Outputrow in $Output)
{
    $name = ""
    $serial = ""
    $name = $Outputrow.name
    # This levelfield for serial number may not be the same for everyone, please check this up!
	$serial = $Outputrow.levelfield_values.serial_number_26000476017
    # Using the example of HP here
	if($Outputrow.product_name -like "HP*")
        {
            write-host -foregroundcolor Green "Here you can check warranty for $name with serial $serial..."
        }
    else
        {
            write-host -foregroundcolor Yellow "Asset $name is not HP computer."
        }
}

Credit goes to Mark Wilkinson for writing the basics of this in a forum post here.

Still Alive and Kicking

Hey. It’s me. I know, it’s been a long, long time since I updated this place. But the past months have been pretty boring. Which considering everything that is happening is a good thing!

It got a bit depressing after summer when my wife lost her job and was unemployed for a while but she got a new job so that’s over and done with. But other than that we’re pretty much in a holding pattern until we get vaccinated (or infected) so we can start living again and society starting to open up. Because I’m pretty tired of our house by now after spending almost 12 months here. I’m one of the lucky ones that can do 99% of my job from home but that also means I don’t get out much. Or at all…

But so far we’ve been able to avoid getting covid19, the closest it has gotten was my sister that got symptoms day after she was here for an evening of F1. She had pretty rough 2 weeks and is still suffering a bit unfortunately even after 5 months!

So here’s hoping we can get vaccinated and start living again and people can start travelling so our company doesn’t go down as a result of this considering how much we’ve survived. Tsunami, ash clouds, bankruptcy, we beat them all!

We managed to find a new ice cream joint in Stockholm that we didn’t know about so we won’t have to travel to Söderköping that much!