76 lines
2.0 KiB
Go
76 lines
2.0 KiB
Go
//go:build windows
|
|
|
|
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
func collectHardwareComponents() (map[string]string, error) {
|
|
components := map[string]string{}
|
|
|
|
script := `
|
|
$ErrorActionPreference = 'SilentlyContinue'
|
|
$cs = Get-CimInstance Win32_ComputerSystemProduct
|
|
$bb = Get-CimInstance Win32_BaseBoard
|
|
$bios = Get-CimInstance Win32_BIOS
|
|
$cpu = Get-CimInstance Win32_Processor | Select-Object -First 1
|
|
$disk = Get-CimInstance Win32_DiskDrive | Where-Object { $_.Index -eq 0 } | Select-Object -First 1
|
|
[pscustomobject]@{
|
|
smbiosUuid = [string]$cs.UUID
|
|
baseboardSerial = [string]$bb.SerialNumber
|
|
biosSerial = [string]$bios.SerialNumber
|
|
cpuId = [string]$cpu.ProcessorId
|
|
systemDiskSerial = [string]$disk.SerialNumber
|
|
} | ConvertTo-Json -Compress
|
|
`
|
|
command := exec.Command(
|
|
"powershell.exe",
|
|
"-NoProfile",
|
|
"-NonInteractive",
|
|
"-ExecutionPolicy", "Bypass",
|
|
"-Command", script,
|
|
)
|
|
hideCommandWindow(command)
|
|
output, err := command.Output()
|
|
if err == nil {
|
|
var payload map[string]string
|
|
if json.Unmarshal([]byte(strings.TrimSpace(string(output))), &payload) == nil {
|
|
for key, value := range payload {
|
|
if normalized := normalizeComponent(value); normalized != "" {
|
|
components[key] = normalized
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if machineGUID := readMachineGUID(); machineGUID != "" {
|
|
components["machineGuid"] = machineGUID
|
|
}
|
|
if err := ensureNotEmpty(components); err != nil {
|
|
return nil, err
|
|
}
|
|
return components, nil
|
|
}
|
|
|
|
func readMachineGUID() string {
|
|
command := exec.Command(
|
|
"reg", "query", `HKLM\SOFTWARE\Microsoft\Cryptography`, "/v", "MachineGuid",
|
|
)
|
|
hideCommandWindow(command)
|
|
output, err := command.Output()
|
|
if err != nil {
|
|
return ""
|
|
}
|
|
line := strings.TrimSpace(string(output))
|
|
for _, candidate := range strings.Split(line, "\n") {
|
|
fields := strings.Fields(strings.TrimSpace(candidate))
|
|
if len(fields) >= 3 && strings.EqualFold(fields[0], "MachineGuid") {
|
|
return normalizeComponent(strings.Join(fields[2:], " "))
|
|
}
|
|
}
|
|
return ""
|
|
}
|