Public/Get-PdqLicense.ps1
<#
.SYNOPSIS Retrieves and decodes your PDQ license(s). .INPUTS System.String[] .OUTPUTS System.Object[] System.Xml.XmlElement System.String .EXAMPLE Get-PdqLicense Retrieves and decodes the Deploy and Inventory licenses from the registry. .EXAMPLE Get-PdqLicense -Product 'Deploy' -Format 'Raw' Retrieves your Deploy license from the registry without decoding it. .EXAMPLE Get-PdqLicense -License (Get-Clipboard) Retrieves a PDQ license from your clipboard and decodes it. #> function Get-PdqLicense { [CmdletBinding(DefaultParameterSetName = 'Product')] param ( [ValidateSet('Deploy', 'Inventory')] [Parameter(ParameterSetName = 'Product')] # The PDQ application whose license you would like to retrieve. [String[]]$Product = ('Deploy', 'Inventory'), # The license(s) you would like to decode, if you don't want to retrieve them from the registry. [Parameter(ParameterSetName = 'License')] [String[]]$License, [ValidateSet('Object', 'Raw', 'RawMiddle', 'XML')] # How you would like the license(s) to be formatted. [String]$Format = 'Object' ) function Exit-AfterFormatting { [CmdletBinding()] param ( $Format, $FormatToTest, $DesiredOutput ) if ( $Format -eq $FormatToTest ) { $DesiredOutput continue } } $Licenses = @() if ( $License ) { # Flatten multi-line licenses, such as those from emails. if ( $License[0] -eq '--- START LICENSE ---' ) { $TempLicenses = @() foreach ( $Line in $License ) { if ( $Line -eq '--- START LICENSE ---' ) { $TempLicense = $Line } else { $TempLicense += $Line } if ( $Line -eq '--- END LICENSE ---' ) { $TempLicenses += $TempLicense } } $License = $TempLicenses } foreach ( $SuspiciousLicense in $License ) { if ( -not $SuspiciousLicense.StartsWith('--- START LICENSE ---') ) { throw 'Licenses must start with: --- START LICENSE ---' } if ( -not $SuspiciousLicense.EndsWith('--- END LICENSE ---') ) { throw 'Licenses must end with: --- END LICENSE ---' } $Licenses += $SuspiciousLicense } } else { foreach ( $ProductName in $Product ) { try { $Licenses += Get-ItemPropertyValue -Path "HKLM:\SOFTWARE\Admin Arsenal\PDQ $ProductName" -Name 'License' $Count ++ } catch { Write-Warning "Unable to find your PDQ $ProductName license" } } if ( -not $Count ) { throw 'Unable to find any licenses in the registry.' } } foreach ( $LicenseRaw in $Licenses ) { Exit-AfterFormatting $Format 'Raw' $LicenseRaw # Get the main part of the license. $LicenseMiddle = ($LicenseRaw -split "---")[2] Exit-AfterFormatting $Format 'RawMiddle' $LicenseMiddle # Decode base64. # https://stackoverflow.com/a/15415708 $LicenseDecoded = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($LicenseMiddle)) Exit-AfterFormatting $Format 'XML' $LicenseDecoded # Convert the XML string into a PowerShell object, and output it. ([XML]$LicenseDecoded).License } } |