gpt4 book ai didi

powershell - Select-Object Cmdlet 为 Get-NetIPConfiguration(不同类型)返回不同格式的结果

转载 作者:行者123 更新时间:2023-12-03 01:25:21 25 4
gpt4 key购买 nike

我试图获取我的物理网络适配器的 IP 配置详细信息,同时排除所有虚拟网络适配器。我的脚本如下:

$NetAdaptIndex = Get-NetAdapter -Physical | Select -Property InterfaceIndex -ExpandProperty InterfaceIndex

## Get Ip address, Gateway and DNS details as per configuration
$IPAddressConfig = foreach ($II in $NetAdaptIndex) {
Write-Host $II -ForegroundColor Yellow

Get-NetIPConfiguration |
Where-Object { $_.InterfaceIndex -eq $II} |
Select-Object InterfaceAlias, InterfaceDescription, InterfaceIndex, Ipv4Address, IPv4DefaultGateway, DNSServer
}
当我调用该变量时,我得到以下结果,其中 IPv4Address、IPv4DefaultGateway 和 DNSServer 未以字符串格式返回。
PS C:\WINDOWS\system32> $IPAddressConfig


InterfaceAlias : WiFi 2
InterfaceDescription : D-Link DWA-171 Wireless AC Dual Band Adapter
InterfaceIndex : 46
IPv4Address : {192.168.0.103}
IPv4DefaultGateway : {MSFT_NetRoute (InstanceID = ":8:8:8:9:55?@55;C?8;@B8:8;55;")}
DNSServer : {MSFT_DNSClientServerAddress (Name = "46", CreationClassName = "", SystemCreationClassName = "", SystemName = "23"), MSFT_DNSClientServerAddress (Name = "46", CreationClassName = "", SystemCreationClassName = "", SystemName = "2")}

InterfaceAlias : Ethernet
InterfaceDescription : Broadcom NetLink (TM) Gigabit Ethernet
InterfaceIndex : 10
IPv4Address : {169.254.149.208}
IPv4DefaultGateway :
DNSServer : {MSFT_DNSClientServerAddress (Name = "10", CreationClassName = "", SystemCreationClassName = "", SystemName = "23"), MSFT_DNSClientServerAddress (Name = "10", CreationClassName = "", SystemCreationClassName = "", SystemName = "2")}
但如果我要手动删除 Select-Object 代码,它会返回格式正确的结果。
Get-NetIPConfiguration | 
Where-Object { $_.InterfaceIndex -eq 46}
观察 IPv4Address、IPv4DefaultGateway 和 DNSServer 如何返回格式正确的结果。
InterfaceAlias       : WiFi 2
InterfaceIndex : 46
InterfaceDescription : D-Link DWA-171 Wireless AC Dual Band Adapter
NetProfile.Name : <removed>
IPv6Address : <removed>
IPv4Address : 192.168.0.103
IPv6DefaultGateway : <removed>
IPv4DefaultGateway : 192.168.0.1
DNSServer : 2001:f40:0:3::12:68
2001:4860:4860::8888
1.1.1.1
8.8.8.8
我的问题是为什么会这样?有什么方法可以用来将它们转换为正确的格式吗?后一种格式是我想要的格式。

最佳答案

PowerShell 中的所有内容都是一个对象。具有多个嵌套属性的对象可能会变得非常复杂。

PS > (Get-NetIPConfiguration | Select-Object DNSServer)[0]

DNSServer
---------
{MSFT_DNSClientServerAddress (Name = "11", CreationClassName = "", SystemCreationClassName = "", SystemName = "23"), MSFT_DNSClientServerAddress (Name = "11", CreationClassName = "", Syste...

PS > (Get-NetIPConfiguration | Select-Object -ExpandProperty DNSServer)[0]

InterfaceAlias Interface Address ServerAddresses
Index Family
-------------- --------- ------- ---------------
LAN 11 IPv6 {}
为了让您的生活更轻松,PowerShell 会在对象显示在屏幕上时对其进行格式化。格式化规则存储在 xml文件: Formatting File Overview .对于 NetTCPIP模块,文件将是 C:\Windows\System32\WindowsPowerShell\v1.0\Modules\NetTCPIP\Tcpip.Format.ps1xml .这里是 DNSServer项目格式代码:
<ListItem>
<Label>DNSServer</Label>
<ItemSelectionCondition>
<ScriptBlock>
($_.DNSServer.Count -ne 0) -and
(($_.NetIPv4Interface.ConnectionState -eq "Connected") -or
($_.NetIPv6Interface.ConnectionState -eq "Connected"))
</ScriptBlock>
</ItemSelectionCondition>
<ScriptBlock>
$output = "";
foreach($Server in $_.DNSServer) {
foreach($Address in $Server.ServerAddresses) {
$output += $Address + "`n";
}
};
$output.Trim("`n");
</ScriptBlock>
</ListItem>

应用的格式由 PSTypeNames 控制。存在于每个对象上的属性:
PS > (Get-NetIPConfiguration)[0].PSTypeNames
NetIPConfiguration
System.Object
当您使用 Select-Object ,它会修改该属性,因此生成的对象会丢失其格式:
PS > (Get-NetIPConfiguration | Select-Object DNSServer)[0].PSTypeNames
Selected.NetIPConfiguration
System.Management.Automation.PSCustomObject
System.Object
唉,简单地重新设置它是行不通的(对象没有正确的结构),但你可以使用 calculated property 来模拟它。 :
Get-NetIPConfiguration  | Select-Object InterfaceAlias, InterfaceDescription, InterfaceIndex, Ipv4Address, IPv4DefaultGateway, @{ n='DNSServer' ; e={$_.DNSServer.ServerAddresses -join "`n"}}
这将挤压 DNSServer串,所以你以后不能正确地重用它。也许,只是输出 ServerAddresses收集会更方便,看起来也一样:
Get-NetIPConfiguration  | Select-Object InterfaceAlias, InterfaceDescription, InterfaceIndex, Ipv4Address, IPv4DefaultGateway, @{ n='DNSServer' ; e={@($_.DNSServer.ServerAddresses)}}

关于powershell - Select-Object Cmdlet 为 Get-NetIPConfiguration(不同类型)返回不同格式的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63763611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com