Files
DeployCenter/HardwareInfo.ps1
T
2026-10-15 21:15:33 +02:00

180 lines
4.5 KiB
PowerShell

# HardwareInfo.ps1
# DeployCenter - Inventaire matériel
# Compatible Windows PowerShell 5.1 / WinPE
function Get-HardwareInfo {
[CmdletBinding()]
param()
$info = [ordered]@{
Manufacturer = "Inconnu"
Model = "Inconnu"
SerialNumber = "Inconnu"
Firmware = "Inconnu"
Architecture = $env:PROCESSOR_ARCHITECTURE
WinPE = $false
Disks = @()
Errors = @()
}
# Fabricant et modele du PC
try {
$computer = Get-CimInstance `
-ClassName Win32_ComputerSystem `
-ErrorAction Stop
if ($computer.Manufacturer) {
$info.Manufacturer = $computer.Manufacturer.Trim()
}
if ($computer.Model) {
$info.Model = $computer.Model.Trim()
}
}
catch {
$info.Errors += "Modele du PC : $($_.Exception.Message)"
}
# Numero de serie
try {
$bios = Get-CimInstance `
-ClassName Win32_BIOS `
-ErrorAction Stop
if ($bios.SerialNumber) {
$info.SerialNumber = $bios.SerialNumber.Trim()
}
}
catch {
$info.Errors += "Numero de serie : $($_.Exception.Message)"
}
# Detection du firmware
try {
$firmwareKey = Get-ItemProperty `
-Path "HKLM:\SYSTEM\CurrentControlSet\Control" `
-Name PEFirmwareType `
-ErrorAction Stop
switch ([int]$firmwareKey.PEFirmwareType) {
1 { $info.Firmware = "BIOS (Legacy)" }
2 { $info.Firmware = "UEFI" }
default { $info.Firmware = "Indetermine" }
}
}
catch {
$info.Errors += "Mode firmware indisponible"
}
# Detection de WinPE
$info.WinPE = Test-Path `
"HKLM:\SYSTEM\CurrentControlSet\Control\MiniNT"
# Inventaire des disques physiques
try {
$disks = Get-CimInstance `
-ClassName Win32_DiskDrive `
-ErrorAction Stop
foreach ($disk in $disks) {
$info.Disks += [PSCustomObject]@{
Index = [int]$disk.Index
Model = $disk.Model
SizeBytes = [long]$disk.Size
SizeGB = [math]::Round(
([double]$disk.Size / 1GB), 1
)
InterfaceType = $disk.InterfaceType
Status = $disk.Status
}
}
}
catch {
$info.Errors += "Inventaire des disques : $($_.Exception.Message)"
}
# Retourner un objet exploitable par les autres modules
[PSCustomObject]$info
}
# ============================================================
# Execution du test
# ============================================================
$LogDirectory = "X:\DeployLogs"
$JsonPath = Join-Path $LogDirectory "hardware.json"
$TextPath = Join-Path $LogDirectory "hardware.log"
try {
New-Item -ItemType Directory `
-Path $LogDirectory `
-Force `
-ErrorAction Stop | Out-Null
}
catch {
throw "Impossible de creer le repertoire de logs : $($_.Exception.Message)"
}
$result = Get-HardwareInfo
# Export JSON en UTF-8 avec BOM sous Windows PowerShell 5.1
try {
$json = $result | ConvertTo-Json -Depth 5
Set-Content `
-Path $JsonPath `
-Value $json `
-Encoding UTF8 `
-ErrorAction Stop
}
catch {
throw "Impossible d'enregistrer le JSON : $($_.Exception.Message)"
}
# Construire un journal texte sans utiliser Write-Host
$lines = @(
"=== DeployCenter - Inventaire materiel ==="
"Date : $(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')"
"Fabricant : $($result.Manufacturer)"
"Modele : $($result.Model)"
"Numero serie : $($result.SerialNumber)"
"Firmware : $($result.Firmware)"
"Architecture : $($result.Architecture)"
"WinPE : $($result.WinPE)"
""
"=== Disques detectes ==="
)
foreach ($disk in $result.Disks) {
$lines += (
"Disque {0} : {1} - {2} Go - {3} - {4}" -f
$disk.Index,
$disk.Model,
$disk.SizeGB,
$disk.InterfaceType,
$disk.Status
)
}
if ($result.Errors.Count -gt 0) {
$lines += ""
$lines += "=== Erreurs ==="
foreach ($message in $result.Errors) {
$lines += $message
}
}
$utf8 = New-Object System.Text.UTF8Encoding($false)
[System.IO.File]::WriteAllLines(
$TextPath,
[string[]]$lines,
$utf8
)
# Renvoyer l'objet comme sortie principale du script
$result