# DeployCenter - Network detection # Compatible with Windows PowerShell 5.1 function Get-DeployNetworkIPv4 { [CmdletBinding()] param() # Prefer an interface with a default gateway; exclude loopback, # APIPA, and non-routable IPv4 addresses that are generally useless in 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 { # Network detection must not prevent DeployCenter from starting. if (Get-Command Write-Log -ErrorAction SilentlyContinue) { Write-Log (ConvertTo-DeployCenterLocalizedLogMessage -Message "Network detection: $($_.Exception.Message)") 'WARNING' } } $chosen = $candidates | Sort-Object @{Expression='HasGateway';Descending=$true}, Index | Select-Object -First 1 if ($chosen) { return [string]$chosen.Address } return $null }