If you’re using Lenovo laptops in your environment you most likely have checked to see if your machine is still under warranty. The Lenovo Warranty Status page works great, the only problem is, is that it doesn’t scale to well. For example, what if you needed to check the warranty status of 10 Lenovo laptops, what about 20, or even 1000? Yeah, I don’t see myself typing in the serial number for 1000 Lenovo machines into their status checker. This could take forever and a day to complete and we have way better things to be doing with out time. Luckily we have Powershell to Get Lenovo Warranty Expiration Status in all its glory. Here is the easy peazy way to get a Lenovo warranty lookup.
Inspired by Reddit user dungeongoon in this Post I decided to modify the work that he did and put a little twist of my own so I can get the output that I thought was necessary for me. For one, I kept on getting errors when I tried using Get-CimInstance since my environment didn’t have WinRM enabled. Thus, I had to continue to use the Get-WMIObject for consistent results. Second, I wanted to be pass multiple computers in the command line or pass an array of objects so I modified it as such.
Requirements
No Powershell requirements are needed.
Parameters
-ComputerName
Description: Specify one or multiple computer names to pass through the function.
-IncudeBatteryExpiration
Description: If specified, it will show if the Battery is still under warranty. The ID type specified is “1EZ*”
Examples
Example 1: Display the warranty information for the current Lenovo computer
PS C:\> Get-LenovoWarranty ComputerName : PC1 Type : BASE ID : 3EZ StartDate : 2015-01-08 EndDate : 2018-02-21 DaysRemaining : 0 Status : Expired
Example 2: Specify a computer name or multiple computer names separated by a comma.
PS C:\> Get-LenovoWarranty -ComputerName PC1, PC2 ComputerName : PC1 Type : BASE ID : 3EZ StartDate : 2015-01-08 EndDate : 2018-02-21 DaysRemaining : 0 Status : Expired ComputerName : PC2 Type : BASE ID : 3EZ StartDate : 2016-03-25 EndDate : 2019-05-08 DaysRemaining : 381 Status : Active
Example 3: Use the -IncudeBatteryExpiration parameter to determine if the battery warranty has expired.
PS C:\> Get-LenovoWarranty -IncudeBatteryExpiration ComputerName : PC1 Type : BASE ID : 3EZ StartDate : 2015-01-08 EndDate : 2018-02-21 DaysRemaining : 0 Status : Expired ComputerName : PC1 Type : BASE ID : 1EZBAT StartDate : 2015-01-08 EndDate : 2016-02-21 DaysRemaining : 0 Status : Expired
The box in red highlights the battery expiration information.
Now let’s get to the script to get the Lenovo Warranty Information.
Script to Get Lenovo Warranty Expiration Status
Function Get-LenovoWarranty { <# .SYNOPSIS This will check the warranty status of a Lenovo laptop For updated help and examples refer to -Online version. .DESCRIPTION This will check the warranty status of a Lenovo laptop For updated help and examples refer to -Online version. .NOTES Name: Get-LenovoWarranty Author: The Sysadmin Channel Version: 1.0 DateCreated: 2018-Apr-21 DateUpdated: 2018-Apr-21 .LINK https://thesysadminchannel.com/get-lenovo-warranty-expiration-status-with-powershell/ - For updated help and examples refer to -Online version. #> [CmdletBinding()] Param ( [Parameter( ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true, Position=0)] [String[]] $ComputerName = $env:COMPUTERNAME, [Parameter()] [switch] $IncudeBatteryExpiration ) BEGIN { $Object = @() $Today = Get-Date -Format 'yyyy-MM-dd' } PROCESS { Foreach ($Computer in $ComputerName) { Try { $Bios = Get-WmiObject Win32_Bios -ComputerName $Computer -ErrorAction Stop $SerialNumber = $Bios.SerialNumber $Manufacturer = $Bios.Manufacturer If ($Manufacturer -eq "LENOVO") { $ApiObject = Invoke-RestMethod -Uri "http://supportapi.lenovo.com/V2.5/Warranty?Serial=$SerialNumber" } else { Write-Error "BIOS manufacturer is not Lenovo; unable to proceed." -ErrorAction Stop } If ($IncudeBatteryExpiration) { $Notebook = $ApiObject.Warranty | Where-Object {($_.ID -like "1EZ*") -or ($_.ID -eq "36Y") -or ($_.ID -eq "3EZ")} } else { $Notebook = $ApiObject.Warranty | Where-Object {($_.ID -eq "36Y") -or ($_.ID -eq "3EZ")} } foreach ($ID in $Notebook) { $EndDate = $ID.End.Split('T')[0] $StartDate = $ID.Start.Split('T')[0] if ($EndDate -gt $Today) { $DaysRemaining = New-TimeSpan -Start $Today -End $EndDate | select -ExpandProperty Days $DaysRemaining = $DaysRemaining - 1 $Properties = @{ ComputerName = $Computer Type = $ID.Type ID = $ID.ID StartDate = $StartDate EndDate = $EndDate DaysRemaining = $DaysRemaining Status = 'Active' } } else { $Properties = @{ ComputerName = $Computer Type = $ID.Type ID = $ID.ID StartDate = $StartDate EndDate = $EndDate DaysRemaining = 0 Status = 'Expired' } } $Object += New-Object -TypeName PSObject -Property $Properties | Select ComputerName, Type, ID, StartDate, EndDate, DaysRemaining, Status } } catch { $ErrorMessage = $Computer + " Error: " + $_.Exception.Message } finally { Write-Output $Object $Object = $null if ($ErrorMessage) { #Write-Output $ErrorMessage $ErrorMessage = $null } } } } END {} }
How to run the script
In order to the run the script there are a couple of things you need to do. First and foremost, set your execution policy to RemoteSigned. This is a standard with running any powershell script.
Next you need to dot source the script since it is a function. To dot source the script do the following:
- Copy the script above and save it any location. In this example I’ll save it to my C:\_Scripts folder.
- Within the Powershell Window running as Administrator type: . .\_Scripts\Get-LenovoWarranty.ps1 – Note the two dots before the backslash.
Hopefully this script has enabled you to find the Lenovo warranty information much faster. As always, if you find any bugs within the script don’t hesitate to post it in the comments.