KelvinTegelaar / PowerShellWarrantyReports

a repo dedicated to automatic warranty reporting and retrieval from different systems such as IT-Glue, Connectwise, Autotask, and N-central.
GNU Affero General Public License v3.0
172 stars 71 forks source link

Get-LenovoWarranty.ps1 - The underlying connection was closed: An unexpected error occurred on a send. #73

Closed jared-unify closed 6 months ago

jared-unify commented 1 year ago

When I'm checking warranties now, I seem to get an issue checking Lenovo warranties. Here's an example:

Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send. At C:\Program Files\WindowsPowerShell\Modules\PSWarranty\1.6.0\private\Get-LenovoWarranty.ps1:4 char:12

The URL mentioned in the GET request seems to be not responding.

BlueHairMinerBoy commented 1 year ago

Looks like the API endpoint it's trying to hit is dead.

CoopaLoopa72 commented 1 year ago

Copying this from my other comment in case it helps. The Lenovo API is broken, you have to replace that script.

This was written by Rob Eberhardt in the comments on Kevin's Cyberdrain post. After you install the PSWarranty module, find where your PowerShell modules are installed and replace Powershell\Modules\PSWarranty\1.8.0\private\Get-LenovoWarranty.ps1 with the code below to scrape the warranty object from the Lenovo warranty webpage.


function get-LenovoWarranty([Parameter(Mandatory = $true)]$SourceDevice, $client) {
    $today = Get-Date -Format yyyy-MM-dd
    $APIURL = "https://pcsupport.lenovo.com/us/en/api/v4/mse/getproducts?productId=$SourceDevice"
    $Req = Invoke-RestMethod -Uri $APIURL -Method get

    if($req.id){
        $APIURL = "https://pcsupport.lenovo.com/us/en/products/$($req.id)/warranty"
        $Req = Invoke-RestMethod -Uri $APIURL -Method get
        $search = $Req |Select-String -Pattern "var ds_warranties = window.ds_warranties \|\| (.*);[\r\n]*"
        $jsonWarranties = $search.matches.groups[1].value |ConvertFrom-Json
        }

    if ($jsonWarranties.BaseWarranties) {
        $warfirst = $jsonWarranties.BaseWarranties |sort-object -property [DateTime]End |select-object -first 1
        $warlatest = $jsonWarranties.BaseWarranties |sort-object -property [DateTime]End |select-object -last 1
        $WarObj = [PSCustomObject]@{
            'Serial' = $jsonWarranties.Serial
            'Warranty Product name' = $jsonWarranties.ProductName
            'StartDate' = [DateTime]($warfirst.Start)
            'EndDate' = [DateTime]($warlatest.End)
            'Warranty Status' = $warlatest.StatusV2
            'Client' = $Client
            'Product Image' = $jsonWarranties.ProductImage
            'Warranty URL' = $jsonWarranties.WarrantyUpgradeURLInfo.WarrantyURL
        }
    }
    else {
        $WarObj = [PSCustomObject]@{
            'Serial' = $SourceDevice
            'Warranty Product name' = 'Could not get warranty information'
            'StartDate' = $null
            'EndDate' = $null
            'Warranty Status' = 'Could not get warranty information'
            'Client' = $Client
            'Product Image' = ""
            'Warranty URL' = ""
        }
    }
    return $WarObj
}
jared-unify commented 1 year ago

That works great. Has anyone thought about creating a pull request for that?