49 lines
2.1 KiB
PowerShell
49 lines
2.1 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 ($interface in [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces()) {
|
|
if ($interface.NetworkInterfaceType -eq [System.Net.NetworkInformation.NetworkInterfaceType]::Loopback) { continue }
|
|
|
|
$properties = $interface.GetIPProperties()
|
|
$ipv4Properties = $properties.GetIPv4Properties()
|
|
if ($null -eq $ipv4Properties) { continue }
|
|
|
|
$hasGateway = @(
|
|
$properties.GatewayAddresses | Where-Object {
|
|
$_.Address.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork -and
|
|
$_.Address.ToString() -ne '0.0.0.0'
|
|
}
|
|
).Count -gt 0
|
|
|
|
foreach ($unicast in $properties.UnicastAddresses) {
|
|
$address = $unicast.Address
|
|
if ($address.AddressFamily -ne [System.Net.Sockets.AddressFamily]::InterNetwork) { continue }
|
|
$addressText = $address.ToString()
|
|
if ($addressText -eq '127.0.0.1' -or $addressText -like '169.254.*' -or $addressText -eq '0.0.0.0') { continue }
|
|
$candidates += [pscustomobject]@{
|
|
Address = $addressText
|
|
HasGateway = $hasGateway
|
|
Index = [int]$ipv4Properties.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 : $($_.Exception.Message)" 'WARNING'
|
|
}
|
|
}
|
|
|
|
$chosen = $candidates | Sort-Object @{Expression='HasGateway';Descending=$true}, Index | Select-Object -First 1
|
|
if ($chosen) { return [string]$chosen.Address }
|
|
return $null
|
|
}
|