输出脚本显示额外的逗号
我有一个脚本检查NIC绑定顺序的脚本。输出脚本显示额外的逗号
$result = Invoke-Command -ComputerName $ComputerName -ScriptBlock { function Get-BindOrder {
$Binding = (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Linkage").Bind
$Return = New-Object PSobject
$BindingOrder = @()
foreach ($Bind in $Binding) {
$DeviceId = $Bind.Split("\")[2]
$Adapter = (Get-WmiObject Win32_Networkadapter | Where {$_.GUID -eq $DeviceId }).NetConnectionId
$BindingOrder += $Adapter
}
$BindingOrder
} #EndFunction
CLS
Get-BindOrder
}
$adapteresult = $result -join (",")
时,我赞同这个 $adapteresult = $result
,我得到的输出如下
PS C:\> $adapteresult
vEthernet (10.211.14.0_20)
storage
Ethernet 5
Ethernet 4
Ethernet 2
Ethernet 6
将同一个变量,当我加入$adapteresult = $result -join (",")
,我得到了投入与之间的额外命令。
vEthernet (10.241.24.0_21),storage,Ethernet 5,,Ethernet 4,Ethernet 2,Ethernet 6,,
我不希望输出中有任何额外的逗号。期待像下面的输出:
vEthernet (10.241.24.0_21),storage,Ethernet 5,Ethernet 4,Ethernet 2,Ethernet 6,
回答:
我已经纠正了自己和它的做工精细现在
$a = (Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Linkage").Bind | ForEach-Object { $guid = ($_ -split '\\')[2]
Get-WmiObject -Query "SELECT * FROM Win32_NetworkAdapter WHERE GUID='$guid'" |
Select-Object -ExpandProperty NetConnectionID
}
$b = $a -join","
回答:
(Get-WmiObject Win32_Networkadapter | Where {$_.GUID -eq $guid}).NetConnectionId
似乎回到你是在$BindingOrder
捕捉$空。在屏幕上显示他们不占用空间,但他们在那里无论如何。我的机器上运行你的代码的精简版本地...
$results = ((Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Linkage").Bind | Foreach-Object { $guid = $_.split("\")[2]
(Get-WmiObject Win32_Networkadapter | Where {$_.GUID -eq $guid}).NetConnectionId
})
使用让我们看看$results
:
$results $results.Count
$results -contains $null
Local Area Connection
6
True
在我的机器,我只对本地连接一个匹配。但$results
已存储6个项目,其中至少一个是$null
。您需要过滤掉它看起来在您的查询中的这些结果。
$Adapter = (Get-WmiObject Win32_Networkadapter | Where {$_.GUID -eq $DeviceId }).NetConnectionId if($Adapter){
$BindingOrder += $Adapter
}
应该这样做。如果$adapter
为空或空字符串,那么它将不会被添加到$bindingorder
以上是 输出脚本显示额外的逗号 的全部内容, 来源链接: utcz.com/qa/264613.html