NN.HpWarranty.psm1
#Region './Private/Get-HpwAccessToken.ps1' 0 function Get-HpwAccessToken { [CmdletBinding()] param ( $accessTokenPath = "$env:USERPROFILE\.creds\HP\hpWarrantyAccessToken.xml" ) process { #Conditions to refresh access token if (Test-Path $accessTokenPath) { [datetime]$accessTokenExpiryDate = (Import-Clixml $accessTokenPath | ConvertFrom-SecureString -AsPlainText | ConvertFrom-Json).expiry_date #Refresh access token if there's less than 5 minutes till token expiry if (($accessTokenExpiryDate.AddMinutes(-5)) -lt (Get-Date)) { New-HpwAccessToken } } else { New-HpwAccessToken } #Import the existing access token (Import-Clixml $accessTokenPath | ConvertFrom-SecureString -AsPlainText | ConvertFrom-Json).access_token } } #EndRegion './Private/Get-HpwAccessToken.ps1' 24 #Region './Public/Invoke-HpwRequest.ps1' 0 function Invoke-HpwRequest { [CmdletBinding()] param ( [Parameter(Mandatory,ValueFromPipeline,Position=0)]$sn, [string]$pn, [string]$cc = "no" ) begin { $queryBody = New-Object -TypeName System.Collections.ArrayList } process { $null = $queryBody.Add( @{ "sn" = $sn "pn" = $pn "cc" = $cc } ) } end { $splat = @{ "Uri" = "warranty.api.hp.com/productwarranty/v2/jobs" "Method" = "POST" "Headers" = @{ "accept" = "application/json" "ContentType" = "application/json" "Authorization" = "Bearer $(Get-HpwAccessToken)" } "Body" = $queryBody | ConvertTo-Json } Invoke-RestMethod @splat } } #EndRegion './Public/Invoke-HpwRequest.ps1' 37 #Region './Public/New-HpwAccessToken.ps1' 0 function New-HpwAccessToken { [CmdletBinding()] param ( $clientIdPath = "$env:USERPROFILE\.creds\HP\hpWarrantyClientId.xml", $clientSecretPath = "$env:USERPROFILE\.creds\HP\hpWarrantyClientSecret.xml", $accessTokenPath = "$env:USERPROFILE\.creds\HP\hpWarrantyAccessToken.xml" ) process { #Create parent folders of the access token file $accessTokenDir = $accessTokenPath.Substring(0, $accessTokenPath.lastIndexOf('\')) if (!(Test-Path $accessTokenDir)) { $null = New-Item -ItemType Directory $accessTokenDir } #Create clientId file if (!(Test-Path $clientIdPath)) { Read-Host "Enter HP client_id" -AsSecureString | Export-Clixml $clientIdPath } #Create clientSecret file if (!(Test-Path $clientSecretPath)) { Read-Host "Enter HP client_secret" -AsSecureString | Export-Clixml $clientSecretPath } $clientId = Import-Clixml $clientIdPath | ConvertFrom-SecureString -AsPlainText $clientSecret = Import-Clixml $clientSecretPath | ConvertFrom-SecureString -AsPlainText $b64EncodedCred = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes("$($clientId):$($clientSecret)")) $splat = @{ "Method" = "POST" "Uri" = "https://warranty.api.hp.com/oauth/v1/token" "Headers" = @{ "accept" = "application/json" "authorization" = "Basic $b64EncodedCred" "content-type" = "application/x-www-form-urlencoded" } "Body" = @{ "grant_type" = "client_credentials" } } $result = Invoke-RestMethod @splat #Adds access token and expiry date to access token file [PSCustomObject]@{ access_token = $result.access_token expiry_date = (Get-Date).AddSeconds($result.expires_in) } | ConvertTo-Json | ConvertTo-SecureString -AsPlainText | Export-Clixml -Path $accessTokenPath } } #EndRegion './Public/New-HpwAccessToken.ps1' 51 |