POWERSHELL: Get Warranty Info for Microsoft Surface Devices

Feed the below script a CSV containing the “ComputerName” & “SerialNumber” of your devices and it will output a CSV with the warrenty information (Handles Extended Warranties properly.)

#point at your csv file containing "ComputerName" & "SerialNumber"
$list_of_surface_serials = import-csv -path c:\path\to\serials.csv

#Define the path for the output file
$outputpath = c:\path\for\results.csv

$results = [System.Collections.ArrayList]@()
foreach($device in $list_of_surface_serials)
    {
    $body = ConvertTo-Json @{
        sku          = "Surface_"
        SerialNumber = $device.serialnumber
        ForceRefresh = $false
    }
    $today = Get-Date
    $PublicKey = Invoke-RestMethod -Uri 'https://surfacewarrantyservice.azurewebsites.net/api/key' -Method Get
    $AesCSP = New-Object System.Security.Cryptography.AesCryptoServiceProvider 
    $AesCSP.GenerateIV()
    $AesCSP.GenerateKey()
    $AESIVString = [System.Convert]::ToBase64String($AesCSP.IV)
    $AESKeyString = [System.Convert]::ToBase64String($AesCSP.Key)
    $AesKeyPair = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes("$AESIVString,$AESKeyString"))
    $bodybytes = [System.Text.Encoding]::UTF8.GetBytes($body)
    $bodyenc = [System.Convert]::ToBase64String($AesCSP.CreateEncryptor().TransformFinalBlock($bodybytes, 0, $bodybytes.Length))
    $RSA = New-Object System.Security.Cryptography.RSACryptoServiceProvider
    $RSA.ImportCspBlob([System.Convert]::FromBase64String($PublicKey))
    $EncKey = [System.Convert]::ToBase64String($rsa.Encrypt([System.Text.Encoding]::UTF8.GetBytes($AesKeyPair), $false))
    
    $FullBody = @{
        Data = $bodyenc
        Key  = $EncKey
    } | ConvertTo-Json
    
    $WarReq = Invoke-RestMethod -uri "https://surfacewarrantyservice.azurewebsites.net/api/v2/warranty" -Method POST -body $FullBody -ContentType "application/json"
    if ($WarReq.warranties) 
        {
            $WarObj = [PSCustomObject]@{
                'Serial'                = $device.SerialNumber
                'Warranty Product name' = $WarReq.warranties.name -join "`n"
                'StartDate'             = (($WarReq.warranties.effectivestartdate | sort-object -Descending | select-object -last 1) -split 'T')[0]
                'EndDate'               = (($WarReq.warranties.effectiveenddate | sort-object | select-object -last 1) -split 'T')[0]
                'Warranty Status'       = if (((($WarReq.warranties.effectiveenddate | sort-object | select-object -last 1) -split 'T')[0] | get-date) -le $today) { "Expired" } else { "OK" }
                'Client'                = $Client
            }
        }
    else
        {
            $WarObj = [PSCustomObject]@{
                'Serial'                = $device.SerialNumber
                'Warranty Product name' = 'Could not get warranty information'
                'StartDate'             = $null
                'EndDate'               = $null
                'Warranty Status'       = 'Could not get warranty information'
                'Client'                = $Client
            }
        }
    
    $info = $null
    $info = new-object -ItemType PSCustomObject
    $info | Add-Member -MemberType NoteProperty -Name ComputerName -Value $device.computername -Force
    $info | Add-Member -MemberType NoteProperty -Name Serial -Value $WarObj.Serial -Force 
    $info | Add-Member -MemberType NoteProperty -Name StartDate -Value $WarObj.StartDate -Force
    $info | Add-Member -MemberType NoteProperty -Name EndDate -Value $WarObj.EndDate -Force
    $info | Add-Member -MemberType NoteProperty -Name WarrantyStatus -Value $WarObj.'Warranty Status' -Force
    $info | Add-Member -MemberType NoteProperty -Name Client -Value $WarObj.Client -Force
$results.Add($info)  
}
$results | export-csv -Path $outputpath -NoTypeInformation

About the Author

Leave a Reply

You may also like these