<# .SYNOPSIS AutopilotAdder: show this PC's Windows Autopilot hardware hash as QR codes for the AutopilotAdder iOS app, which imports it into Intune (classic Windows Autopilot). .DESCRIPTION Run during Windows setup (OOBE): press Shift+F10 and type powershell "irm https://ap.lshort.dk|iex" The hash is read from the same source as Get-WindowsAutopilotInfo, compressed and split over a few QR codes that cycle on screen. Scan every part with the app, import, wait for the app to report "Profile assigned", then press Reboot here. The hash never leaves this PC except through the QR codes on screen: it is not written to disk and not sent over the network. QR payload, one per part: aphash:/1/?sn=&p=/&id=<8 hex>&crc=<8 hex>&data= The data chunks, joined in order, are base64(gzip(hash bytes)); crc is the CRC-32 of that joined base64 text; id ties the parts of one run together. MIT License, Copyright (c) 2026 the AutopilotAdder authors. Includes a port of the QR Code generator library by Project Nayuki (MIT License), https://www.nayuki.io/page/qr-code-generator-library; see the notice in the C# source below. #> $ErrorActionPreference = 'Stop' # C# 5 only: Windows PowerShell 5.1's Add-Type compiles with the .NET Framework compiler. $AutopilotAdderCSharp = @' /* * QR Code generator library - byte-mode subset, ported to C# for AutopilotAdder. * * Copyright (c) Project Nayuki. (MIT License) * https://www.nayuki.io/page/qr-code-generator-library * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * - The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - The Software is provided "as is", without warranty of any kind, express or * implied, including but not limited to the warranties of merchantability, * fitness for a particular purpose and noninfringement. In no event shall the * authors or copyright holders be liable for any claim, damages or other * liability, whether in an action of contract, tort or otherwise, arising from, * out of or in connection with the Software or the use or other dealings in the * Software. */ using System; using System.Collections.Generic; using System.Text; namespace AutopilotAdder { public sealed class QrCode { // Error correction levels by ordinal: 0 = L, 1 = M, 2 = Q, 3 = H. private static readonly int[] FormatBitsOfEcl = { 1, 0, 3, 2 }; public readonly int Version; public readonly int Size; public readonly int Ecl; public readonly int Mask; private readonly bool[,] modules; // [y, x], true = dark private bool[,] isFunction; /// UTF-8 text in byte mode, smallest version that fits, error correction at least `minEcl` /// (raised while the version stays the same). public static QrCode EncodeText(string text, int minEcl) { return EncodeBinary(Encoding.UTF8.GetBytes(text), minEcl); } public static QrCode EncodeBinary(byte[] data, int minEcl) { if (minEcl < 0 || minEcl > 3) throw new ArgumentException("Invalid error correction level"); int ecl = minEcl; int version; int dataUsedBits; for (version = 1; ; version++) { int capacityBits = GetNumDataCodewords(version, ecl) * 8; dataUsedBits = 4 + (version < 10 ? 8 : 16) + data.Length * 8; if (dataUsedBits <= capacityBits) break; if (version >= 40) throw new ArgumentException("Data too long for one QR code: " + data.Length + " bytes"); } for (int e = ecl + 1; e <= 3; e++) { if (dataUsedBits <= GetNumDataCodewords(version, e) * 8) ecl = e; } List bb = new List(); AppendBits(bb, 0x4, 4); // byte mode AppendBits(bb, data.Length, version < 10 ? 8 : 16); // character count foreach (byte b in data) AppendBits(bb, b, 8); int dataCapacityBits = GetNumDataCodewords(version, ecl) * 8; AppendBits(bb, 0, Math.Min(4, dataCapacityBits - bb.Count)); AppendBits(bb, 0, (8 - bb.Count % 8) % 8); for (int padByte = 0xEC; bb.Count < dataCapacityBits; padByte ^= 0xEC ^ 0x11) AppendBits(bb, padByte, 8); byte[] dataCodewords = new byte[bb.Count / 8]; for (int i = 0; i < bb.Count; i++) { if (bb[i]) dataCodewords[i >> 3] |= (byte)(1 << (7 - (i & 7))); } return new QrCode(version, ecl, dataCodewords); } private QrCode(int ver, int ecl, byte[] dataCodewords) { Version = ver; Size = ver * 4 + 17; Ecl = ecl; modules = new bool[Size, Size]; isFunction = new bool[Size, Size]; DrawFunctionPatterns(); DrawCodewords(AddEccAndInterleave(dataCodewords)); int msk = 0; int minPenalty = int.MaxValue; for (int i = 0; i < 8; i++) { ApplyMask(i); DrawFormatBits(i); int penalty = GetPenaltyScore(); if (penalty < minPenalty) { msk = i; minPenalty = penalty; } ApplyMask(i); // undo (XOR) } Mask = msk; ApplyMask(msk); DrawFormatBits(msk); isFunction = null; } public bool GetModule(int x, int y) { return 0 <= x && x < Size && 0 <= y && y < Size && modules[y, x]; } /// Rows of '1' (dark) and '0' (light), no quiet zone. For tests. public string ToText() { StringBuilder sb = new StringBuilder(); for (int y = 0; y < Size; y++) { for (int x = 0; x < Size; x++) sb.Append(modules[y, x] ? '1' : '0'); sb.Append('\n'); } return sb.ToString(); } private static void AppendBits(List bb, int val, int len) { for (int i = len - 1; i >= 0; i--) bb.Add(((val >> i) & 1) != 0); } private void DrawFunctionPatterns() { for (int i = 0; i < Size; i++) { SetFunctionModule(6, i, i % 2 == 0); SetFunctionModule(i, 6, i % 2 == 0); } DrawFinderPattern(3, 3); DrawFinderPattern(Size - 4, 3); DrawFinderPattern(3, Size - 4); int[] alignPatPos = GetAlignmentPatternPositions(); int numAlign = alignPatPos.Length; for (int i = 0; i < numAlign; i++) { for (int j = 0; j < numAlign; j++) { if (!(i == 0 && j == 0 || i == 0 && j == numAlign - 1 || i == numAlign - 1 && j == 0)) DrawAlignmentPattern(alignPatPos[i], alignPatPos[j]); } } DrawFormatBits(0); DrawVersion(); } private void DrawFormatBits(int msk) { int data = FormatBitsOfEcl[Ecl] << 3 | msk; int rem = data; for (int i = 0; i < 10; i++) rem = (rem << 1) ^ ((rem >> 9) * 0x537); int bits = (data << 10 | rem) ^ 0x5412; for (int i = 0; i <= 5; i++) SetFunctionModule(8, i, GetBit(bits, i)); SetFunctionModule(8, 7, GetBit(bits, 6)); SetFunctionModule(8, 8, GetBit(bits, 7)); SetFunctionModule(7, 8, GetBit(bits, 8)); for (int i = 9; i < 15; i++) SetFunctionModule(14 - i, 8, GetBit(bits, i)); for (int i = 0; i < 8; i++) SetFunctionModule(Size - 1 - i, 8, GetBit(bits, i)); for (int i = 8; i < 15; i++) SetFunctionModule(8, Size - 15 + i, GetBit(bits, i)); SetFunctionModule(8, Size - 8, true); // always dark } private void DrawVersion() { if (Version < 7) return; int rem = Version; for (int i = 0; i < 12; i++) rem = (rem << 1) ^ ((rem >> 11) * 0x1F25); int bits = Version << 12 | rem; for (int i = 0; i < 18; i++) { bool bit = GetBit(bits, i); int a = Size - 11 + i % 3; int b = i / 3; SetFunctionModule(a, b, bit); SetFunctionModule(b, a, bit); } } private void DrawFinderPattern(int x, int y) { for (int dy = -4; dy <= 4; dy++) { for (int dx = -4; dx <= 4; dx++) { int dist = Math.Max(Math.Abs(dx), Math.Abs(dy)); int xx = x + dx, yy = y + dy; if (0 <= xx && xx < Size && 0 <= yy && yy < Size) SetFunctionModule(xx, yy, dist != 2 && dist != 4); } } } private void DrawAlignmentPattern(int x, int y) { for (int dy = -2; dy <= 2; dy++) for (int dx = -2; dx <= 2; dx++) SetFunctionModule(x + dx, y + dy, Math.Max(Math.Abs(dx), Math.Abs(dy)) != 1); } private void SetFunctionModule(int x, int y, bool isDark) { modules[y, x] = isDark; isFunction[y, x] = true; } private byte[] AddEccAndInterleave(byte[] data) { int numBlocks = NumErrorCorrectionBlocks[Ecl][Version]; int blockEccLen = EccCodewordsPerBlock[Ecl][Version]; int rawCodewords = GetNumRawDataModules(Version) / 8; int numShortBlocks = numBlocks - rawCodewords % numBlocks; int shortBlockLen = rawCodewords / numBlocks; byte[][] blocks = new byte[numBlocks][]; byte[] rsDiv = ReedSolomonComputeDivisor(blockEccLen); for (int i = 0, k = 0; i < numBlocks; i++) { int datLen = shortBlockLen - blockEccLen + (i < numShortBlocks ? 0 : 1); byte[] dat = new byte[datLen]; Array.Copy(data, k, dat, 0, datLen); k += datLen; byte[] block = new byte[shortBlockLen + 1]; Array.Copy(dat, block, datLen); byte[] ecc = ReedSolomonComputeRemainder(dat, rsDiv); Array.Copy(ecc, 0, block, block.Length - blockEccLen, ecc.Length); blocks[i] = block; } byte[] result = new byte[rawCodewords]; for (int i = 0, k = 0; i < blocks[0].Length; i++) { for (int j = 0; j < blocks.Length; j++) { // Skip the padding byte in short blocks if (i != shortBlockLen - blockEccLen || j >= numShortBlocks) { result[k] = blocks[j][i]; k++; } } } return result; } private void DrawCodewords(byte[] data) { int i = 0; for (int right = Size - 1; right >= 1; right -= 2) { if (right == 6) right = 5; for (int vert = 0; vert < Size; vert++) { for (int j = 0; j < 2; j++) { int x = right - j; bool upward = ((right + 1) & 2) == 0; int y = upward ? Size - 1 - vert : vert; if (!isFunction[y, x] && i < data.Length * 8) { modules[y, x] = GetBit(data[i >> 3], 7 - (i & 7)); i++; } } } } } private void ApplyMask(int msk) { for (int y = 0; y < Size; y++) { for (int x = 0; x < Size; x++) { bool invert; switch (msk) { case 0: invert = (x + y) % 2 == 0; break; case 1: invert = y % 2 == 0; break; case 2: invert = x % 3 == 0; break; case 3: invert = (x + y) % 3 == 0; break; case 4: invert = (x / 3 + y / 2) % 2 == 0; break; case 5: invert = x * y % 2 + x * y % 3 == 0; break; case 6: invert = (x * y % 2 + x * y % 3) % 2 == 0; break; case 7: invert = ((x + y) % 2 + x * y % 3) % 2 == 0; break; default: throw new ArgumentException("Mask value out of range"); } modules[y, x] ^= invert & !isFunction[y, x]; } } } private int GetPenaltyScore() { int result = 0; for (int y = 0; y < Size; y++) { bool runColor = false; int runX = 0; int[] runHistory = new int[7]; for (int x = 0; x < Size; x++) { if (modules[y, x] == runColor) { runX++; if (runX == 5) result += PenaltyN1; else if (runX > 5) result++; } else { FinderPenaltyAddHistory(runX, runHistory); if (!runColor) result += FinderPenaltyCountPatterns(runHistory) * PenaltyN3; runColor = modules[y, x]; runX = 1; } } result += FinderPenaltyTerminateAndCount(runColor, runX, runHistory) * PenaltyN3; } for (int x = 0; x < Size; x++) { bool runColor = false; int runY = 0; int[] runHistory = new int[7]; for (int y = 0; y < Size; y++) { if (modules[y, x] == runColor) { runY++; if (runY == 5) result += PenaltyN1; else if (runY > 5) result++; } else { FinderPenaltyAddHistory(runY, runHistory); if (!runColor) result += FinderPenaltyCountPatterns(runHistory) * PenaltyN3; runColor = modules[y, x]; runY = 1; } } result += FinderPenaltyTerminateAndCount(runColor, runY, runHistory) * PenaltyN3; } for (int y = 0; y < Size - 1; y++) { for (int x = 0; x < Size - 1; x++) { bool color = modules[y, x]; if (color == modules[y, x + 1] && color == modules[y + 1, x] && color == modules[y + 1, x + 1]) result += PenaltyN2; } } int dark = 0; for (int y = 0; y < Size; y++) for (int x = 0; x < Size; x++) if (modules[y, x]) dark++; int total = Size * Size; int k = (Math.Abs(dark * 20 - total * 10) + total - 1) / total - 1; result += k * PenaltyN4; return result; } private int[] GetAlignmentPatternPositions() { if (Version == 1) return new int[0]; int numAlign = Version / 7 + 2; int step = (Version * 8 + numAlign * 3 + 5) / (numAlign * 4 - 4) * 2; int[] result = new int[numAlign]; result[0] = 6; for (int i = result.Length - 1, pos = Size - 7; i >= 1; i--, pos -= step) result[i] = pos; return result; } private static int GetNumRawDataModules(int ver) { int size = ver * 4 + 17; int result = size * size; result -= 8 * 8 * 3; result -= 15 * 2 + 1; result -= (size - 16) * 2; if (ver >= 2) { int numAlign = ver / 7 + 2; result -= (numAlign - 1) * (numAlign - 1) * 25; result -= (numAlign - 2) * 2 * 20; if (ver >= 7) result -= 6 * 3 * 2; } return result; } private static byte[] ReedSolomonComputeDivisor(int degree) { byte[] result = new byte[degree]; result[degree - 1] = 1; int root = 1; for (int i = 0; i < degree; i++) { for (int j = 0; j < result.Length; j++) { result[j] = (byte)ReedSolomonMultiply(result[j], root); if (j + 1 < result.Length) result[j] ^= result[j + 1]; } root = ReedSolomonMultiply(root, 0x02); } return result; } private static byte[] ReedSolomonComputeRemainder(byte[] data, byte[] divisor) { byte[] result = new byte[divisor.Length]; foreach (byte b in data) { int factor = b ^ result[0]; Array.Copy(result, 1, result, 0, result.Length - 1); result[result.Length - 1] = 0; for (int i = 0; i < result.Length; i++) result[i] ^= (byte)ReedSolomonMultiply(divisor[i], factor); } return result; } private static int ReedSolomonMultiply(int x, int y) { int z = 0; for (int i = 7; i >= 0; i--) { z = (z << 1) ^ ((z >> 7) * 0x11D); z ^= ((y >> i) & 1) * x; } return z; } private static int GetNumDataCodewords(int ver, int ecl) { return GetNumRawDataModules(ver) / 8 - EccCodewordsPerBlock[ecl][ver] * NumErrorCorrectionBlocks[ecl][ver]; } private int FinderPenaltyCountPatterns(int[] runHistory) { int n = runHistory[1]; bool core = n > 0 && runHistory[2] == n && runHistory[3] == n * 3 && runHistory[4] == n && runHistory[5] == n; return (core && runHistory[0] >= n * 4 && runHistory[6] >= n ? 1 : 0) + (core && runHistory[6] >= n * 4 && runHistory[0] >= n ? 1 : 0); } private int FinderPenaltyTerminateAndCount(bool currentRunColor, int currentRunLength, int[] runHistory) { if (currentRunColor) { FinderPenaltyAddHistory(currentRunLength, runHistory); currentRunLength = 0; } currentRunLength += Size; FinderPenaltyAddHistory(currentRunLength, runHistory); return FinderPenaltyCountPatterns(runHistory); } private void FinderPenaltyAddHistory(int currentRunLength, int[] runHistory) { if (runHistory[0] == 0) currentRunLength += Size; Array.Copy(runHistory, 0, runHistory, 1, runHistory.Length - 1); runHistory[0] = currentRunLength; } private static bool GetBit(int x, int i) { return ((x >> i) & 1) != 0; } private const int PenaltyN1 = 3; private const int PenaltyN2 = 3; private const int PenaltyN3 = 40; private const int PenaltyN4 = 10; private static readonly int[][] EccCodewordsPerBlock = new int[][] { new int[] {-1, 7, 10, 15, 20, 26, 18, 20, 24, 30, 18, 20, 24, 26, 30, 22, 24, 28, 30, 28, 28, 28, 28, 30, 30, 26, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Low new int[] {-1, 10, 16, 26, 18, 24, 16, 18, 22, 22, 26, 30, 22, 22, 24, 24, 28, 28, 26, 26, 26, 26, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28}, // Medium new int[] {-1, 13, 22, 18, 26, 18, 24, 18, 22, 20, 24, 28, 26, 24, 20, 30, 24, 28, 28, 26, 30, 28, 30, 30, 30, 30, 28, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // Quartile new int[] {-1, 17, 28, 22, 16, 22, 28, 26, 26, 24, 28, 24, 28, 22, 24, 24, 30, 28, 28, 26, 28, 30, 24, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30}, // High }; private static readonly int[][] NumErrorCorrectionBlocks = new int[][] { new int[] {-1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 4, 4, 4, 4, 4, 6, 6, 6, 6, 7, 8, 8, 9, 9, 10, 12, 12, 12, 13, 14, 15, 16, 17, 18, 19, 19, 20, 21, 22, 24, 25}, // Low new int[] {-1, 1, 1, 1, 2, 2, 4, 4, 4, 5, 5, 5, 8, 9, 9, 10, 10, 11, 13, 14, 16, 17, 17, 18, 20, 21, 23, 25, 26, 28, 29, 31, 33, 35, 37, 38, 40, 43, 45, 47, 49}, // Medium new int[] {-1, 1, 1, 2, 2, 4, 4, 6, 6, 8, 8, 8, 10, 12, 16, 12, 17, 16, 18, 21, 20, 23, 23, 25, 27, 29, 34, 34, 35, 38, 40, 43, 45, 48, 51, 53, 56, 59, 62, 65, 68}, // Quartile new int[] {-1, 1, 1, 2, 4, 4, 4, 5, 6, 8, 8, 11, 11, 16, 16, 18, 16, 19, 21, 25, 25, 25, 34, 30, 32, 35, 37, 40, 42, 45, 48, 51, 54, 57, 60, 63, 66, 70, 74, 77, 81}, // High }; } public static class Crc32 { private static readonly uint[] Table = BuildTable(); private static uint[] BuildTable() { uint[] t = new uint[256]; for (uint n = 0; n < 256; n++) { uint c = n; for (int k = 0; k < 8; k++) c = (c & 1) != 0 ? 0xEDB88320u ^ (c >> 1) : c >> 1; t[n] = c; } return t; } public static uint Compute(byte[] data) { uint c = 0xFFFFFFFFu; foreach (byte b in data) c = Table[(c ^ b) & 0xFF] ^ (c >> 8); return c ^ 0xFFFFFFFFu; } } } '@ if (-not ('AutopilotAdder.QrCode' -as [type])) { Add-Type -TypeDefinition $AutopilotAdderCSharp -Language CSharp } # --------------------------------------------------------------------------------------------- # Payload (pure - also used by the tests on macOS/Linux with pwsh) # --------------------------------------------------------------------------------------------- function New-AutopilotHashPayloads { param( [Parameter(Mandatory = $true)][string]$Serial, [Parameter(Mandatory = $true)][string]$HashBase64, [int]$ChunkSize = 900, [string]$BatchId ) $hash = [Convert]::FromBase64String($HashBase64) $buffer = New-Object System.IO.MemoryStream $gzip = New-Object System.IO.Compression.GZipStream($buffer, [System.IO.Compression.CompressionMode]::Compress) $gzip.Write($hash, 0, $hash.Length) $gzip.Close() $data = [Convert]::ToBase64String($buffer.ToArray()) if (-not $BatchId) { $BatchId = -join (1..8 | ForEach-Object { '{0:x}' -f (Get-Random -Maximum 16) }) } $crc = '{0:x8}' -f [AutopilotAdder.Crc32]::Compute([Text.Encoding]::ASCII.GetBytes($data)) $count = [int][Math]::Ceiling($data.Length / $ChunkSize) $sn = [Uri]::EscapeDataString($Serial) for ($i = 0; $i -lt $count; $i++) { $start = $i * $ChunkSize $chunk = $data.Substring($start, [Math]::Min($ChunkSize, $data.Length - $start)) "aphash:/1/?sn=$sn&p=$($i + 1)/$count&id=$BatchId&crc=$crc&data=$chunk" } } # --------------------------------------------------------------------------------------------- # Windows only: read the hash, draw the QR codes, show them # --------------------------------------------------------------------------------------------- function Get-AutopilotHashInfo { $devDetail = Get-CimInstance -Namespace 'root/cimv2/mdm/dmmap' -ClassName 'MDM_DevDetail_Ext01' ` -Filter "InstanceID='Ext' AND ParentID='./DevDetail'" $bios = Get-CimInstance -ClassName 'Win32_BIOS' [pscustomobject]@{ Serial = ("" + $bios.SerialNumber).Trim() HashBase64 = $devDetail.DeviceHardwareData } } function New-QrBitmap { param([string]$Text, [int]$MaxPixels) $qr = [AutopilotAdder.QrCode]::EncodeText($Text, 1) # at least ECC M $border = 4 $modulesWide = $qr.Size + 2 * $border $scale = [Math]::Max(2, [Math]::Floor($MaxPixels / $modulesWide)) $pixels = [int]($modulesWide * $scale) $bitmap = New-Object System.Drawing.Bitmap($pixels, $pixels) $graphics = [System.Drawing.Graphics]::FromImage($bitmap) $graphics.Clear([System.Drawing.Color]::White) $black = [System.Drawing.Brushes]::Black for ($y = 0; $y -lt $qr.Size; $y++) { for ($x = 0; $x -lt $qr.Size; $x++) { if ($qr.GetModule($x, $y)) { $graphics.FillRectangle($black, [int](($x + $border) * $scale), [int](($y + $border) * $scale), [int]$scale, [int]$scale) } } } $graphics.Dispose() $bitmap } function Show-AutopilotHashQR { Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing [System.Windows.Forms.Application]::EnableVisualStyles() try { $info = Get-AutopilotHashInfo } catch { [System.Windows.Forms.MessageBox]::Show("Could not read the hardware hash: $($_.Exception.Message)", 'AutopilotAdder') | Out-Null return } if (-not $info.HashBase64) { [System.Windows.Forms.MessageBox]::Show('This PC did not report a hardware hash. Run this during Windows setup (OOBE) on a supported PC.', 'AutopilotAdder') | Out-Null return } $payloads = @(New-AutopilotHashPayloads -Serial $info.Serial -HashBase64 $info.HashBase64) $bounds = [System.Windows.Forms.Screen]::PrimaryScreen.Bounds $qrPixels = [int]([Math]::Min($bounds.Width, $bounds.Height) * 0.72) $images = @() foreach ($p in $payloads) { $images += New-QrBitmap -Text $p -MaxPixels $qrPixels } $state = @{ Index = 0; Auto = $true } $form = New-Object System.Windows.Forms.Form $form.Text = 'AutopilotAdder' $form.FormBorderStyle = 'None' $form.WindowState = 'Maximized' $form.TopMost = $true $form.BackColor = [System.Drawing.Color]::White $form.KeyPreview = $true $title = New-Object System.Windows.Forms.Label $title.Dock = 'Top' $title.Height = 90 $title.TextAlign = 'MiddleCenter' $title.Font = New-Object System.Drawing.Font('Segoe UI', 20) $picture = New-Object System.Windows.Forms.PictureBox $picture.Dock = 'Fill' $picture.SizeMode = 'CenterImage' $buttons = New-Object System.Windows.Forms.FlowLayoutPanel $buttons.Dock = 'Bottom' $buttons.Height = 70 $buttons.FlowDirection = 'LeftToRight' $buttons.Padding = New-Object System.Windows.Forms.Padding(20, 10, 20, 10) $update = { $n = $images.Count $picture.Image = $images[$state.Index] $mode = if ($state.Auto) { 'changes automatically - Space pauses' } else { 'paused - Space resumes' } $title.Text = "Scan with AutopilotAdder: part $($state.Index + 1) of $n ($mode)`nSerial number: $($info.Serial)" } $step = { param([int]$delta) $n = $images.Count $state.Index = (($state.Index + $delta) % $n + $n) % $n & $update } $font = New-Object System.Drawing.Font('Segoe UI', 14) $newButton = { param([string]$text, [scriptblock]$onClick) $b = New-Object System.Windows.Forms.Button $b.Text = $text $b.Font = $font $b.AutoSize = $true $b.Height = 48 $b.Add_Click($onClick) $buttons.Controls.Add($b) | Out-Null } & $newButton '< Previous' { $state.Auto = $false; & $step -1 } & $newButton 'Next >' { $state.Auto = $false; & $step 1 } & $newButton 'Pause / Resume' { $state.Auto = -not $state.Auto; & $update } & $newButton 'Reboot' { $answer = [System.Windows.Forms.MessageBox]::Show( 'Reboot now? Only do this after AutopilotAdder reports "Profile assigned".', 'AutopilotAdder', 'YesNo', 'Question') if ($answer -eq 'Yes') { Start-Process -FilePath 'shutdown.exe' -ArgumentList '/r /t 0' } } & $newButton 'Close' { $form.Close() } $form.Add_KeyDown({ param($sender, $e) switch ($e.KeyCode) { 'Left' { $state.Auto = $false; & $step -1 } 'Right' { $state.Auto = $false; & $step 1 } 'Space' { $state.Auto = -not $state.Auto; & $update } 'Escape' { $form.Close() } } }) $timer = New-Object System.Windows.Forms.Timer $timer.Interval = 2500 $timer.Add_Tick({ if ($state.Auto -and $images.Count -gt 1) { & $step 1 } }) $form.Controls.Add($picture) $form.Controls.Add($title) $form.Controls.Add($buttons) & $update $timer.Start() [void]$form.ShowDialog() $timer.Dispose() foreach ($image in $images) { $image.Dispose() } } # Tests dot-source this file with $AutopilotAdderNoRun = $true to use the functions only. if (-not (Get-Variable -Name AutopilotAdderNoRun -ValueOnly -ErrorAction SilentlyContinue)) { Show-AutopilotHashQR }