[英]Powershell, combine output of a foreach loop with multiple commands
我需要获取多个服务器的属性列表,但我在循环中遇到了第二个命令的 output:
$(foreach ( $Net in $Nets ) {
Get-NetAdapterBinding -ComponentID ms_msclient,ms_server,ms_tcpip6 -ErrorAction SilentlyContinue | select Name,DisplayName,Enabled
Get-NetAdapterAdvancedProperty $Net -DisplayName "Speed & Duplex" | select DisplayValue
}) | Format-List
第一个 cmd 的 output 是正确的:
Name : LAN_Clients
DisplayName : Internet Protocol Version 6 (TCP/IPV6)
Enabled : False
Name : LAN_Clients
DisplayName : File and Print Sharing
Enabled : False
Name : LAN_Clients
DisplayName : Client for Microsoft Networks
Enabled : False
第二个 cmd 似乎被忽略了......如果我手动运行 cmd output 是正确的:
Get-NetAdapterAdvancedProperty "LAN_Clients" -DisplayName "Speed & Duplex" | select DisplayValue
DisplayValue
------------
Auto Negotiation
我究竟做错了什么?
您需要将两个输出组合成一个 object。
尝试
$(foreach ( $Net in $Nets ) {
$speed = (Get-NetAdapterAdvancedProperty $Net -DisplayName "Speed & Duplex").DisplayValue
Get-NetAdapterBinding -ComponentID ms_msclient,ms_server,ms_tcpip6 -ErrorAction SilentlyContinue |
Select-Object Name,DisplayName,Enabled,
@{Name = 'Speed & Duplex'; Expression = {$speed}}
}) | Format-List
出于故障排除目的,请尝试以下操作:
$Nets = Get-NetAdapterBinding -ComponentID ms_msclient,ms_server,ms_tcpip6 -ErrorAction SilentlyContinue
foreach ($Net in $Nets.name ) {
Get-NetAdapterAdvancedProperty $Net -DisplayName "Speed & Duplex" | select DisplayValue
}
您不需要调用Get-NetAdapter
,因为Get-NetAdapterBinding
已经返回所有适配器的所有绑定。
在 foreach 循环中,您没有将-Name
参数与Get-NetAdapterBinding
一起使用,因此它会在循环的每次迭代中返回所有适配器的所有绑定。
您可以使用Select-Object
中的表达式块来获取您所追求的附加双工属性,如下所示:
Get-NetAdapterBinding -ComponentID ms_msclient,ms_server,ms_tcpip6 -ErrorAction SilentlyContinue |
Select-Object Name,DisplayName,Enabled, @{Name = 'Speed & Duplex'; Expression={Get-NetAdapterAdvancedProperty -InterfaceAlias $_.InterfaceAlias -DisplayName 'Speed & Duplex' | select -ExpandProperty DisplayValue }} |
Format-List
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.