Skip to content

Instantly share code, notes, and snippets.

@soulemike
Created August 6, 2024 17:46
Show Gist options
  • Save soulemike/08f673eb71243f0ca0055aab554c5e1a to your computer and use it in GitHub Desktop.
Save soulemike/08f673eb71243f0ca0055aab554c5e1a to your computer and use it in GitHub Desktop.
function Update-MtLicenseCache {
[OutputType([System.Void])]
[CmdletBinding(
SupportsShouldProcess,
ConfirmImpact = "Low"
)]
param (
[Switch]$Force,
[String]$FileName="maesterLicenses.csv"
)
process {
if ($Force -and -not $Confirm){
$ConfirmPreference = "None"
}
try{
Write-Verbose "Attempting License Cache Update"
$csv = Invoke-RestMethod -Uri "https://download.microsoft.com/download/e/3/e/e3e9faf2-f28b-490a-9ada-c6089a1fc5b0/Product%20names%20and%20service%20plan%20identifiers%20for%20licensing.csv"
}
catch{
Write-Error $_
exit
}
Write-Verbose "Getting hash of downloaded content"
$hashNew = Get-FileHash -InputStream ([IO.MemoryStream]::new([text.encoding]::UTF8.GetBytes($csv))) -Algorithm SHA256
if(Test-Path -Path $env:TEMP\$FileName){
Write-Verbose "Cache exists"
$skus = Get-Content $env:TEMP\$FileName
Write-Verbose "Getting cache hash"
$hashOld = Get-FileHash -InputStream ([IO.MemoryStream]::new([text.encoding]::UTF8.GetBytes($skus))) -Algorithm SHA256
if($hashNew.Hash -ne $hashOld.Hash -and $PSCmdlet.ShouldProcess($FileName)){
Write-Verbose "Cache does not match download, overwriting"
$csv|Out-File $env:TEMP\$FileName -Confirm:$false
$skus = $csv|ConvertFrom-Csv
}else{
Write-Verbose "Cache matches"
$skus = $skus|ConvertFrom-Csv
}
}else{
Write-Verbose "Cache does not exist, setting cache"
if($PSCmdlet.ShouldProcess($FileName)){
$csv|Out-File $env:TEMP\$FileName -Confirm:$false
}
$skus = $csv|ConvertFrom-Csv
}
Write-Verbose "$(($skus|Measure-Object).Count) SKUs in cache"
return $null
}
}
function Get-MtLicenseRelationship {
[OutputType([pscustomobject])]
[CmdletBinding()]
param (
[Parameter(Mandatory, ParameterSetName = "regex")]
[regex]$regexSearchString,
[Parameter(Mandatory, ParameterSetName = "id")]
[guid]$servicePlanId,
[Parameter(Mandatory, ParameterSetName = "name")]
[string]$servicePlanName,
[Parameter(Mandatory, ParameterSetName = "sku")]
[guid]$skuId,
[Parameter(Mandatory, ParameterSetName = "pn")]
[string]$skuPartNumber
)
process {
if(-not (Test-Path $env:TEMP\maesterLicenses.csv)){
Write-Verbose "License cache not found, updating"
Update-MtLicenseCache
}
$plans = Import-Csv -Path $env:TEMP\maesterLicenses.csv
$relationships = @()
if($skuId){
Write-Verbose "Processing as skuId"
$relationships = $plans | Where-Object {`
$_.GUID -eq $skuId
}
}elseif($servicePlanId){
Write-Verbose "Processing as servicePlanId"
$relationships = $plans | Where-Object {`
$_.Service_Plan_Id -eq $servicePlanId
}
}elseif($servicePlanName){
Write-Verbose "Processing as servicePlanName"
$relationships = $plans | Where-Object {`
$_.String_Id -eq $servicePlanName -or `
$_.Service_Plan_Name -eq $servicePlanName
}
}elseif($skuPartNumber){
Write-Verbose "Processing as skuPartNumber"
$relationships = $plans | Where-Object {`
$_.String_Id -eq $skuPartNumber -or `
$_.Service_Plan_Name -eq $skuPartNumber
}
}elseif($regexSearchString){
Write-Verbose "Processing as regexSearchString"
foreach($plan in $plans){
$match = $regexSearchString.Match($plan.Product_Display_Name).Success -or `
$regexSearchString.Match($plan.String_Id).Success -or `
$regexSearchString.Match($plan.Service_Plan_Name).Success -or `
$regexSearchString.Match($plan.Service_Plans_Included_Friendly_Names).Success
if($match){
$relationships += $plan
}
}
}
return $relationships
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment