Files
DeployCenter/Modules/Network.psm1
T
2026-10-15 21:15:33 +02:00

30 lines
1.4 KiB
PowerShell

# DeployCenter - Détection réseau
# Compatible Windows PowerShell 5.1
function Get-DeployNetworkIPv4 {
[CmdletBinding()]
param()
# Priorité à une interface avec passerelle par défaut ; exclut loopback,
# APIPA et adresses IPv4 non routables couramment inutiles en WinPE.
$candidates = @()
try {
foreach ($cfg in (Get-CimInstance Win32_NetworkAdapterConfiguration -Filter 'IPEnabled=True' -ErrorAction Stop)) {
$hasGateway = @($cfg.DefaultIPGateway | Where-Object { $_ -match '^\d{1,3}(\.\d{1,3}){3}$' }).Count -gt 0
foreach ($address in @($cfg.IPAddress | Where-Object { $_ -match '^\d{1,3}(\.\d{1,3}){3}$' })) {
if ($address -eq '127.0.0.1' -or $address -like '169.254.*' -or $address -eq '0.0.0.0') { continue }
$candidates += [pscustomobject]@{ Address = $address; HasGateway = $hasGateway; Index = [int]$cfg.Index }
}
}
} catch {
# La détection réseau ne doit pas empêcher le démarrage de DeployCenter.
if (Get-Command Write-Log -ErrorAction SilentlyContinue) {
Write-Log "Détection réseau WMI : $($_.Exception.Message)" 'WARNING'
}
}
$chosen = $candidates | Sort-Object @{Expression='HasGateway';Descending=$true}, Index | Select-Object -First 1
if ($chosen) { return [string]$chosen.Address }
return $null
}