# 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 # $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 $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 } # Get active network adapters with a MAC address try { $netAdapters = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -ErrorAction Stop | Where-Object { $_.IPEnabled -eq $true -and -not [string]::IsNullOrWhiteSpace($_.MACAddress) } } catch { $netAdapters = Get-WmiObject -Class Win32_NetworkAdapterConfiguration | Where-Object { $_.IPEnabled -eq $true -and -not [string]::IsNullOrWhiteSpace($_.MACAddress) } } # Join MAC addresses if multiple adapters are active $mac = "" foreach ($adapter in $netAdapters) { if (-not [string]::IsNullOrWhiteSpace($adapter.MACAddress)) { if ([string]::IsNullOrWhiteSpace($mac)) { $mac = $adapter.MACAddress } else { $mac = $mac + " | " + $adapter.MACAddress } } } # Select serial source if ($SerialSource -match '^(?i)csp$') { $serial = $csp.IdentifyingNumber } else { $serial = $bios.SerialNumber } # Pretty output if (-not $CsvOnly) { "ComputerName`t{0}" -f $cs.Name "UUID`t`t{0}" -f $csp.UUID "MAC`t`t{0}" -f $mac "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,UUID,MACAddress" $row = "{0},{1},{2},{3},{4}" -f ` (Escape-Csv $cs.Manufacturer), ` (Escape-Csv $cs.Model), ` (Escape-Csv $serial), ` (Escape-Csv $csp.UUID), ` (Escape-Csv $mac) $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 to 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 $header $row