PowerShell 5.1可运行的网络信息脚本在7.5版本无输出
解决PowerShell 7.5中网络信息函数无输出问题
问题背景
以下PowerShell函数在Windows PowerShell 5.1中可正常返回网络信息,但在PowerShell 7.5中运行无任何输出:
function Read-NetworkInfo { Get-NetAdapter | Where-Object -Property Status -eq -Value 'Up' } function Get-NetworkInfo { $nicList = Read-NetworkInfo $ipList = Get-NetIpAddress foreach ($nic in $nicList) { if ($nic.interfaceIndex -notin $ipList.InterfaceIndex) { continue } $macAddress = ($nic).MacAddress $prefixLength = (Get-NetIPAddress ` -InterfaceAlias $nic.Name ` -AddressFamily IPv4 ).PrefixLength $ipAddress = (Get-NetIPAddress ` -InterfaceIndex $nic.ifIndex ).IPv4Address $ipAddress = [string]$ipAddress $prefixLength = [int]$prefixLength $firstOctet = 255 # constant , first octet of net mask will always be 255 $secondOctet = [int](([version] $ipAddress).Minor) $thirdOctet = [int](([version] $ipAddress).Build) $fourthOctet = [int](([version] $ipAddress).Revision) if ($prefixLength -le 16) { $difference = $prefixLength - 8 $secondOctet = 256 - ([Math]:: Pow(2, $prefixLength - (2 * $difference))) $thirdOctet = 0 $fourthOctet = 0 } elseif ($prefixLength -le 24) { $difference = $prefixLength - 12 $secondOctet = 255 $thirdOctet = 256 - ([Math]:: Pow(2, $prefixLength - (2 * $difference))) $fourthOctet = 0 } else { $difference = $prefixLength - 16 $secondOctet = 255 $thirdOctet = 255 $fourthOctet = 256 - ([Math]:: Pow(2, $prefixLength - (2 * $difference))) } [string]$netmask = '0.0.0.0' $octets = $firstOctet, $secondOctet, $thirdOctet, $fourthOctet $netmask = $octets -join ('.') $netObj = @{ InterfaceAlias = $nic.InterfaceAlias InterfaceIndex = $nic.InterfaceIndex InterfaceDescription = $nic.InterfaceDescription NetProfileName = $nic.NetProfileName IPv4Address = $nic.IPv4Address IPv6DefaultGateway = $nic.IPv6DefaultGateway IPv4DefaultGateway = $nic.IPv4DefaultGateway DNSServer = $nic.DNSServer MacAddress = $macAddress NetMask = $netmask } $netJSON = $netObj | ConvertTo-Json Write-Host $netJSON if ($nicList.length -gt 1) { Write-Host $separator } $lineBreak Add-Content -Path "$HOME\Downloads\serverscript.json" -Value $netJSON }#end foreach nic }
问题原因
- 属性获取差异:PowerShell 7中
Get-NetAdapter默认不返回IPv4Address、DNSServer、NetProfileName等属性,这些属性在PS 5.1中是隐式关联其他模块数据返回的,PS7中需要显式获取。 - 空值处理错误:当
$ipAddress为空时,[version]$ipAddress会抛出异常,直接中断循环。 - 集合长度判断错误:
$nicList.length仅对数组有效,当$nicList是单个对象时该属性不存在,导致后续逻辑失效。 - 未定义变量:
$separator和$lineBreak未定义,会输出空值干扰执行流程。 - 子网掩码计算逻辑漏洞:原计算逻辑依赖IP地址的版本解析,且数学运算存在逻辑错误,在PS7中可能返回异常值。
修复后的函数代码
function Get-NetworkInfo { # 获取所有状态为Up的网卡及关联的IP配置 $nicConfigs = Get-NetAdapter -Status Up | Get-NetIPConfiguration # 定义分隔符和换行符 $separator = "------------------------" $lineBreak = "`n" foreach ($config in $nicConfigs) { # 获取IPv4地址信息,跳过无IPv4的网卡 $ipv4Addr = $config.IPv4Address | Where-Object { $null -ne $_.IPAddress } if (-not $ipv4Addr) { continue } # 计算子网掩码(更可靠的方法) $prefixLength = $ipv4Addr.PrefixLength $netmask = [System.Net.IPAddress]::Parse(([System.Net.IPAddress]::NetworkToHostOrder(-1 -shl (32 - $prefixLength)) -band 0xFFFFFFFF)).ToString() # 构建输出对象 $netObj = @{ InterfaceAlias = $config.InterfaceAlias InterfaceIndex = $config.InterfaceIndex InterfaceDescription = $config.InterfaceDescription NetProfileName = $config.NetProfile.Name IPv4Address = $ipv4Addr.IPAddress IPv4DefaultGateway = $config.IPv4DefaultGateway.NextHop IPv6DefaultGateway = $config.IPv6DefaultGateway.NextHop DNSServer = $config.DNSServer.ServerAddresses -join ', ' MacAddress = $config.MacAddress NetMask = $netmask } # 转换为JSON并输出 $netJSON = $netObj | ConvertTo-Json Write-Host $netJSON # 多网卡时输出分隔符 if ($nicConfigs.Count -gt 1) { Write-Host $separator } Write-Host $lineBreak # 写入文件 Add-Content -Path "$HOME\Downloads\serverscript.json" -Value $netJSON } }
关键改动说明
- 改用
Get-NetIPConfiguration:直接关联网卡与IP配置,确保在PS7中能获取到完整的IP、网关、DNS等属性。 - 空值检查:跳过无IPv4地址的网卡,避免空值转换异常。
- 子网掩码计算优化:使用.NET类库方法直接根据前缀长度生成子网掩码,逻辑更可靠,避免手动计算错误。
- 集合计数修正:用
$nicConfigs.Count替代$nicList.length,兼容单个对象和数组的情况。 - 定义缺失变量:明确
$separator和$lineBreak的值,避免输出空内容。 - 属性映射修正:调整
NetProfileName、DNSServer等属性的获取路径,适配PS7中的对象结构。
内容的提问来源于stack exchange,提问作者mimi m
相关产品推荐
相关产品推荐

