Public/Get-HPEntWarrantyEntitlement.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
Function Get-HPEntWarrantyEntitlement {
    
    [CmdletBinding(
        DefaultParameterSetName = '__AllParameterSets'
    )]
    [OutputType(
        [HashTable]
    )]
    
    Param (
        [Parameter(
            ParameterSetName = 'Computer',
            ValueFromPipeline = $true
        )]
        [ValidateScript({
            if ($_ -eq $env:COMPUTERNAME) { 
                $true 
            } else { 
                try { 
                    Test-Connection -ComputerName $_ -Count 1 -ErrorAction Stop
                    $true 
                } catch { 
                    throw "Unable to connect to $_." 
                }
            }
        })]
        [String[]]
        $ComputerName = $env:ComputerName,

        [Parameter(
            ParameterSetName = 'Computer'
        )]
        [ValidateNotNullOrEmpty()]
        [PSCredential]
        [System.Management.Automation.Credential()]
        $Credential = $null,

        [Parameter(
            Mandatory = $true,
            ParameterSetName = 'Static',
            ValueFromPipeLineByPropertyName = $true
        )]
        [String]
        $ProductNumber,

        [Parameter(
            Mandatory = $true,
            ParameterSetName = 'Static',
            ValueFromPipelineByPropertyName = $true
        )]
        [String]
        $SerialNumber,

        [Parameter()]
        [String]
        $CountryCode = 'US',

        [Parameter()]
        [ValidateNotNullOrEmpty()]
        [String]
        $XmlExportPath = $null
    )

    Begin {
        if ($Script:HPEntRegistration.DateTime -lt (Get-Date).AddMinutes($Script:HPEntRegistration.ThresholdInMinutes)) {
            $registrationRequest = (Get-Content -Path "$PSScriptRoot\..\RequestTemplates\HPEntWarrantyRegistration.xml").Replace(
                '<[!--UniversialDateTime--!]>', $([DateTime]::SpecifyKind($(Get-Date), [DateTimeKind]::Local).ToUniversalTime().ToString('yyyy\/MM\/dd hh:mm:ss \G\M\T'))
            ).Replace(
                '<[!--SerialNumber--!]>', $SerialNumber
            )

            [Xml]$registration = Invoke-HPEntSOAPRequest -SOAPRequest $registrationRequest -URL 'https://services.isee.hp.com/ClientRegistration/ClientRegistrationService.asmx' -Action 'http://www.hp.com/isee/webservices/RegisterClient2'

            $Script:HPEntRegistration = @{
                Gdid = $registration.Envelope.Body.RegisterClient2Response.RegisterClient2Result.Gdid
                Token = $registration.Envelope.Body.RegisterClient2Response.RegisterClient2Result.RegistrationToken
                DateTime = Get-Date
            }
        }
        
        $request = (Get-Content -Path "$PSScriptRoot\..\RequestTemplates\HPEntWarrantyEntitlement.xml").Replace(
            '<[!--Gdid--!]>', $Script:HPEntRegistration.Gdid
        ).Replace(
            '<[!--Token--!]>', $Script:HPEntRegistration.Token
        ).Replace(
            '<[!--CountryCode--!]>', $CountryCode
        )
    }

    Process {
        for ($i = 0; $i -lt $ComputerName.Length; $i++) {
            if (-not ($PSCmdlet.ParameterSetName -eq 'Static')) {
                if ($null -ne ($systemInformation = Get-HPProductNumberAndSerialNumber -ComputerName $ComputerName[$i] -Credential $Credential)) {
                    $ProductNumber = $systemInformation.ProductNumber
                    $SerialNumber = $systemInformation.SerialNumber
                } else {
                    continue
                }
            } else {
                $ComputerName[$i] = $null
            }

            try {
                [Xml]$entitlement = (
                    Invoke-HPEntSOAPRequest -SOAPRequest $request.Replace(
                        '<[!--ProductNumber--!]>', $ProductNumber
                    ).Replace(
                        '<[!--SerialNumber--!]>', $SerialNumber
                    ) -Url 'https://services.isee.hp.com/EntitlementCheck/EntitlementCheckService.asmx' -Action 'http://www.hp.com/isee/webservices/GetOOSEntitlementList2'
                ).Envelope.Body.GetOOSEntitlementList2Response.GetOOSEntitlementList2Result.Response
            } catch {
                Write-Error -Message 'Failed to invoke SOAP request.'
                continue
            }

            if ($DebugPreference -eq 'Inquire') {
                Write-Debug -Message 'Returning raw xml.'
                return $entitlement
            }

            if ($null -ne $entitlement) {
                if ($null -ne $entitlement.'ISEE-GetOOSEntitlementInfoResponse'.Data.EIAError) {
                    Write-Error -Message $entitlement.'ISEE-GetOOSEntitlementInfoResponse'.Data.EIAError.ErrorText
                }
                if ($PSBoundParameters.ContainsKey('XmlExportPath')) {
                    try {
                        $entitlement.Save("${XmlExportPath}\${SerialNumber}_entitlement.xml")
                    } catch {
                        Write-Error -Message 'Failed to save xml file.'
                    }
                }
                
                [HashTable]$output = @{
                    'SerialNumber' = $SerialNumber
                    'ProductNumber' = $ProductNumber
                    'ActiveEntitlement' = $entitlement.'ISEE-GetOOSEntitlementInfoResponse'.Data.EsReply.CombinedUnitEntitlement.ActiveWarrantyEntitlement
                    'OverallEntitlementStartDate' = $entitlement.'ISEE-GetOOSEntitlementInfoResponse'.Data.EsReply.CombinedUnitEntitlement.OverallWarrantyStartDate
                    'OverallEntitlementEndDate' = $entitlement.'ISEE-GetOOSEntitlementInfoResponse'.Data.EsReply.CombinedUnitEntitlement.OverallWarrantyEndDate
                }

                if ($PSCmdlet.ParameterSetName -eq 'Computer') {
                    $output.Add('ComputerName', $ComputerName[$i])
                }

                Write-Output -InputObject $output
            } else {
                Write-Error -Message 'No entitlement found.'
                continue
            }
        }
    }
}