# encoding: ascii
# api: powershell
# title: Get-DellWarranty by API
# description: Get-DellWarranty (Uses new Dell API; the old Get-DellWarranty scripts screen scrape their site and no longer work because they changed the formatting of the tables. This relies on their API service so in theory it should be maintained by Dell and remain working. The script itself has an array of computer names at the top and will cycle through each system and query that warranty information. Based off of all warranty lines, the highest warranty is tracked and outputted as well.  The Get-DellWarranty function itself is configured to accept a -servicetag or -serialnumber parameter or accept piped input, and outputs an object that contains objects of warranty entitlement lines.
# version: 0.1
# type: script
# author: Dane Kantner
# license: CC0
# function: Get-DellWarranty
# x-poshcode-id: 6363
# x-archived: 2016-06-02T13:28:39
# x-published: 2016-05-31T10:34:00
#
#
## Dane Kantner 4/19/2013
##

$computers="localhost","Chiv5908-2009","anyothercomputers","NYSPC-JJAJ68YG6"

foreach ($computer in $computers) {

$obj=get-wmiobject win32_systemenclosure -computername $computer -ErrorAction SilentlyContinue

    if ($obj -eq $null) {  # unable to retrieve, system may be offline
        write-output "Computer $computer unavailable (offline or WMI inaccessible).`n"
    } else {
        $WarrantyObject=Get-DellWarranty -ServiceTag $obj.SerialNumber | select @{name = "ComputerName";expression = {$Computer}},ServiceLevelCode,ServiceLevelDescription,Provider,StartDate,EndDate,DaysLeft,EntitlementType   

        #each computer itself will have a $warrantyobject returned that can have multiple warranty lines attached.
        #based off of this you can do various output scenarios. 
        $DaysLeft=0
        $HighestServiceDesc=""
        foreach ($line in $WarrantyObject) {
            if ($line.ServiceLevelCode -ne $Null) { #The last line is a null line from the dell service, discard with an if neq null check
                if ($DaysLeft -lt $line.DaysLeft) { #this warranty lasts longer than the prior line item for this computer.
                $DaysLeft=$line.DaysLeft
                $HighestServiceDesc=$line.ServiceLevelDescription
                }
            write-output $line
            #you could output it to a file here instead -- or instead when calling script do scripts.ps1 > filenametosave.txt
            }
        } #end foreach $warrantyobject
        
        # in this coding scenario, an HP computer would just return a null object.
        if ($warrantyObject -ne $null) { 
            write-output "Maximum warranty for computer $Computer has $DaysLeft days remaining. $HighestServiceDesc`n"
        } else {
            write-output "Dell returned no warranty information for $Computer. Is it a Dell?"   
        } #end if WarrantyObject is not null
       
      
    } #end if obj null because WMI failed to retrieve serial
} #end foreach computer


Function Get-DellWarranty {
## This function was created based off of modifying code from http://itx-solutions.nl/2013/01/dell-powershell-script-to-get-dell-warranty-information/
    [CmdletBinding()]
        param(
            [Parameter(Mandatory=$False,Position=0,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)]
            [alias("serialnumber")]
            [string[]]$ServiceTag
        )
    $WebProxy=New-WebServiceProxy -Uri http://xserv.dell.com/services/assetservice.asmx
    $WarrantyInformation=$WebProxy.GetAssetInformation(([guid]::NewGuid()).Guid,"Dell warranty",$serviceTag)
    $WarrantyInformation | Select-Object -ExpandProperty Entitlements
    return $WarrantyInformation
}
