<# .SYNOPSIS Installs this computer's MCZ licence key from the CSV you downloaded from mcz.software. .DESCRIPTION Finds the row whose MachineId matches this computer (not the PC name, so renamed PCs still match) and writes its key to %ProgramData%\MegaCompressorZip\license.key, which MCZ reads for all users. Run elevated: as a GPO computer startup script, an Intune script, or by hand. The same CSV on a read-only share serves every PC. Exits 0 when installed, 2 when this PC is not in the CSV. .EXAMPLE .\Install-MczLicense.ps1 -Csv \\server\share\mcz-licences.csv .EXAMPLE Import-Csv .\mcz-licences.csv | ForEach-Object { Invoke-Command -ComputerName $_.PcName -FilePath .\Install-MczLicense.ps1 -ArgumentList $null, $_.LicenseKey } Push keys over PowerShell remoting instead. #> [CmdletBinding()] param( [string] $Csv, [string] $Key ) function Get-MczMachineId { $b32 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567' $hklm = [Microsoft.Win32.RegistryKey]::OpenBaseKey('LocalMachine', [Microsoft.Win32.RegistryView]::Registry64) $guid = $hklm.OpenSubKey('SOFTWARE\Microsoft\Cryptography').GetValue('MachineGuid') $drive = $env:SystemRoot.Substring(0, 2) $serial = [Convert]::ToUInt32((Get-CimInstance Win32_LogicalDisk -Filter "DeviceID='$drive'").VolumeSerialNumber, 16) $hash = [System.Security.Cryptography.SHA256]::Create().ComputeHash([System.Text.Encoding]::UTF8.GetBytes("$guid|$serial|MegaCompressorZip")) $acc = [uint64]0; $bits = 0; $s = '' for ($i = 0; $i -lt 10; $i++) { $acc = (($acc -shl 8) -bor $hash[$i]) -band [uint64]65535; $bits += 8 while ($bits -ge 5) { $bits -= 5; $s += $b32[[int](($acc -shr $bits) -band 31)] } } '{0}-{1}-{2}-{3}' -f $s.Substring(0, 4), $s.Substring(4, 4), $s.Substring(8, 4), $s.Substring(12, 4) } if (-not $Key) { if (-not $Csv) { throw 'Give -Csv or -Key .' } $id = Get-MczMachineId $row = Import-Csv -Path $Csv | Where-Object { ($_.MachineId -replace '[\s-]', '').ToUpper() -eq ($id -replace '-', '') } | Select-Object -First 1 if (-not $row) { Write-Warning "$env:COMPUTERNAME (Machine ID $id) is not in $Csv"; exit 2 } $Key = $row.LicenseKey } if ($Key -notmatch '^MCZ1-') { throw 'That is not an MCZ licence key.' } $dir = Join-Path $env:ProgramData 'MegaCompressorZip' New-Item -ItemType Directory -Force -Path $dir | Out-Null $path = Join-Path $dir 'license.key' [System.IO.File]::WriteAllText($path, $Key.Trim() + [Environment]::NewLine, (New-Object System.Text.UTF8Encoding $false)) Write-Host "MCZ licence installed for $env:COMPUTERNAME at $path" exit 0