Files
DeployCenter/Modules/HardwareInventory.psm1
T
benoit 8d5014b4aa Ajout de la partie application des changements sur le disque
Ajout de l'installation de Windows
Correction du chargement des images iso.
Correction de la définition de la taille de WinRE.
2026-10-17 03:56:53 +02:00

152 lines
4.9 KiB
PowerShell

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-Disk -ErrorAction Stop)
foreach ($disk in $disks) {
$operationalStatus = @($disk.OperationalStatus | ForEach-Object { [string]$_ } | Where-Object { -not [string]::IsNullOrWhiteSpace($_) })
$status = if ($operationalStatus.Count -gt 0) { $operationalStatus -join ', ' } else { 'Inconnu' }
$info.Disks += [PSCustomObject]@{
Index = [int]$disk.Number
Model = [string]$disk.FriendlyName
SizeBytes = [long]$disk.Size
SizeGB = [math]::Round(
([double]$disk.Size / 1GB), 1
)
BusType = [string]$disk.BusType
Status = $status
HealthStatus = [string]$disk.HealthStatus
}
}
}
catch {
$info.Errors += "Inventaire des disques : $($_.Exception.Message)"
}
# Retourner un objet exploitable par les autres modules
[PSCustomObject]$info
}
function Write-DeployInventory {
param(
[Parameter(Mandatory=$true)][string]$InventoryDir,
[Parameter(Mandatory=$true)][string]$InventoryFile,
[AllowNull()]$HardwareInfo,
[AllowNull()][string]$DeploymentIPv4,
[AllowNull()][string]$VncPassword,
[AllowNull()][scriptblock]$LogAction = $null
)
try {
if (-not (Test-Path -LiteralPath $InventoryDir -PathType Container)) {
New-Item -Path $InventoryDir -ItemType Directory -Force | Out-Null
}
$inventory = [ordered]@{
GeneratedAt = (Get-Date).ToString('o')
DeploymentIPv4 = $DeploymentIPv4
VncPassword = $VncPassword
Hardware = $HardwareInfo
}
$json = $inventory | ConvertTo-Json -Depth 8
Set-Content -LiteralPath $InventoryFile -Value $json -Encoding UTF8
# Nettoyer les anciens exports séparés après l'écriture de l'inventaire consolidé.
$legacyInventoryFiles = @(
(Join-Path $env:TEMP 'HardwareInfo.json'),
(Join-Path $InventoryDir 'hardware.json')
)
foreach ($legacyFile in $legacyInventoryFiles) {
if (Test-Path -LiteralPath $legacyFile -PathType Leaf) {
try {
Remove-Item -LiteralPath $legacyFile -Force -ErrorAction Stop
} catch {
if ($LogAction) { & $LogAction "Impossible de supprimer l'ancien export d'inventaire '$legacyFile' : $($_.Exception.Message)" 'WARNING' }
}
}
}
} catch {
if ($LogAction) { & $LogAction "Impossible d'écrire l'inventaire temporaire : $($_.Exception.Message)" 'WARNING' }
}
}
function Get-SelectedDeployDiskInfo {
param(
[AllowNull()]$HardwareInfo,
[Parameter(Mandatory=$true)][int]$DiskIndex
)
if ($null -eq $HardwareInfo) { return $null }
$matches = @($HardwareInfo.Disks | Where-Object { [int]$_.Index -eq $DiskIndex } | Select-Object -First 1)
if ($matches.Count -gt 0) { return $matches[0] }
return $null
}
Export-ModuleMember -Function Get-HardwareInfo, Write-DeployInventory, Get-SelectedDeployDiskInfo