部分服务器上Get-NetIPAddress命令失效问题求助
PowerShell网卡信息脚本报错排查与解决
原脚本代码
Function Read-NetworkInfo # retrieves list of NICs that are running { Get-NetAdapter | Where-Object -Property Status -eq -Value 'Up' } Function Get-NetworkInfo { Write-Host "Network Information:" -ForegroundColor Green; $lineBreak $nicList = Read-NetworkInfo foreach($nic in $nicList) { $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 $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 # 此处缺少$符号,属于语法bug $thirdOctet = 255 $fourthOctet = 256 - ([Math]:: Pow(2, $prefixLength - (2 * $difference))) } [string]$netmask = '0.0.0.0' $octets = $firstOctet, $secondOctet, $thirdOctet, $fourthOctet $netmask = $octets -join('.') # print outs Get-NetIpConfiguration -InterfaceAlias $nic.Name Write-Host "Mac Address: " -ForegroundColor Green -NoNewline; Write-Host $macAddress Write-Host "Net Mask: " -ForegroundColor Green -NoNewline; Write-Host $netmask # formatting / organizing the NIC information when the array of NICs is greater than 1 if ($nicList.length -gt 1) { Write-Host $separator } $lineBreak } }
报错信息
Get-NetIPAddress : No matching MSFT_NetIPAddress objects found by CIM query for instances of the ROOT/StandardCimv2/MSFT_NetIPAddress class on the CIM server: SELECT * FROM MSFT_NetIPAddress WHERE ((InterfaceAlias LIKE 'NIC2')) AND ((AddressFamily = 2)). Verify query parameters and retry. At line:50 char:22 + ... ixLength = (Get-NetIPAddress -InterfaceAlias $nic.Name -AddressFamily ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (MSFT_NetIPAddress:String) [Get-NetIPAddress], CimJobException + FullyQualifiedErrorId : CmdletizationQuery_NotFound,Get-NetIPAddress
排查方向
- 网卡未配置IPv4地址:脚本只筛选状态为
Up的网卡,但这类网卡可能仅启用IPv6或未分配任何IPv4地址,导致Get-NetIPAddress查询不到匹配结果。 - 网卡名称匹配问题:部分服务器的网卡名称含空格、特殊字符,使用
-InterfaceAlias查询时可能出现匹配失败,相比之下InterfaceIndex是网卡的唯一标识,可靠性更高。 - CIM服务或PowerShell版本差异:不同服务器的PowerShell版本(如老版本PS5.1与新版本)或CIM服务异常,会导致
Get-NetIPAddress无法正常读取数据。 - 权限不足:执行脚本的用户没有读取网卡IP配置的权限,导致CIM查询失败。
解决方法
增加空值判断与错误处理
在调用Get-NetIPAddress时添加错误抑制,并判断结果是否为空,跳过无IPv4的网卡:# 获取前缀长度 $ipAddrObj = Get-NetIPAddress -InterfaceIndex $nic.ifIndex -AddressFamily IPv4 -ErrorAction SilentlyContinue if (-not $ipAddrObj) { Write-Host "网卡 $($nic.Name) 未配置IPv4地址,跳过" -ForegroundColor Yellow continue } $prefixLength = $ipAddrObj.PrefixLength $ipAddress = $ipAddrObj.IPv4Address改用InterfaceIndex查询
替换所有-InterfaceAlias为-InterfaceIndex,避免网卡名称匹配问题,同时提升查询准确性。修复子网掩码计算的语法bug
原脚本else块中的secondOctet = 255缺少$符号,应改为$secondOctet = 255;另外,用[version]解析IP地址的方法存在潜在问题(如IP含0段时可能解析异常),建议改用Split('.')分割IP:$ipOctets = $ipAddress.Split('.') | ForEach-Object { [int]$_ } $firstOctet = 255 $secondOctet = $ipOctets[1] $thirdOctet = $ipOctets[2] $fourthOctet = $ipOctets[3]检查并修复CIM服务
在报错服务器上执行以下命令验证CIM服务状态:Get-CimInstance -ClassName MSFT_NetIPAddress -Namespace ROOT/StandardCimv2若无法返回数据,尝试重启Winmgmt服务:
Restart-Service Winmgmt -Force验证执行权限
确保执行脚本的用户拥有读取网络配置的权限,建议以管理员身份运行PowerShell。
内容的提问来源于stack exchange,提问作者mimi m
相关产品推荐
相关产品推荐

