# sn-uuid.ps1 (ConstrainedLanguage-safe) # Options via environment variables: # $env:SNUUID_CSVONLY = "1" -> only prints CSV header+row # $env:SNUUID_OUTFILE = "C:\path\file.csv" -> writes CSV there (also prints unless CSVONLY=1) # $env:SNUUID_APPEND = "1" -> append row to existing file # $env:SNUUID_SERIALSOURCE= "BIOS" or "CSP" -> which serial to use (default BIOS) function Escape-Csv([string]$s) { if ($null -eq $s) { return "" } $s = [string]$s if ($s -match '[",\r\n]') { return '"' + ($s -replace '"','""') + '"' } return $s } # Read options (env vars) $CsvOnly = ($env:SNUUID_CSVONLY -eq "1") $OutFile = $env:SNUUID_OUTFILE $Append = ($env:SNUUID_APPEND -eq "1") $SerialSource = $env:SNUUID_SERIALSOURCE if ([string]::IsNullOrWhiteSpace($SerialSource)) { $SerialSource = "BIOS" } # Prefer CIM, fall back to WMI try { $cs = Get-CimInstance -ClassName Win32_ComputerSystem -ErrorAction Stop } catch { $cs = Get-WmiObject -Class Win32_ComputerSystem } try { $csp = Get-CimInstance -ClassName Win32_ComputerSystemProduct -ErrorAction Stop } catch { $csp = Get-WmiObject -Class Win32_ComputerSystemProduct } try { $bios= Get-CimInstance -ClassName Win32_BIOS -ErrorAction Stop } catch { $bios= Get-WmiObject -Class Win32_BIOS } $serial = if ($SerialSource -match '^(?i)csp$') { $csp.IdentifyingNumber } else { $bios.SerialNumber } # Pretty output (unless CSV-only) if (-not $CsvOnly) { "ComputerName`t{0}" -f $cs.Name "UUID`t`t{0}" -f $csp.UUID "Manufacturer`t{0}" -f $cs.Manufacturer "Model`t`t{0}" -f $cs.Model "Serial (BIOS)`t{0}" -f $bios.SerialNumber "Serial (CSP)`t{0}" -f $csp.IdentifyingNumber "Vendor`t`t{0}" -f $csp.Vendor "Name`t`t{0}" -f $csp.Name "" "--- Corporate Identifier CSV ---" } # CSV output $header = "Manufacturer,Model,SerialNumber" $row = "{0},{1},{2}" -f (Escape-Csv $cs.Manufacturer), (Escape-Csv $cs.Model), (Escape-Csv $serial) $csvText = $header + "`r`n" + $row # Optionally write to file if (-not [string]::IsNullOrWhiteSpace($OutFile)) { $fileExists = Test-Path -LiteralPath $OutFile if ($Append -and $fileExists) { # Append ONLY the row (avoid duplicate header) Add-Content -LiteralPath $OutFile -Value $row -Encoding UTF8 } else { # Create/overwrite with header + row Set-Content -LiteralPath $OutFile -Value $csvText -Encoding UTF8 } } # Always print CSV (so you can copy/paste or pipe to clipboard) $header $row