【发布时间】:2018-10-19 06:49:28
【问题描述】:
有什么办法可以将Select-Object或Format-Table的-property参数打掉。
以下面的例子为例,我希望创建一个自定义表达式的哈希表,而不是 @{N="Address"; E={$ping[0].Address}},但我没有成功。
是否有其他方法可以实现此(或类似)?
期望的结果
$param = @{
'Property' = @{
'Address' = $ping[0].Address
'IPV4 Address' = $ping[0].IPV4Address.IPAddressToString
'IPV6 Address' = $ping[0].IPV6Address.IPAddressToString
'Count' = $_.Count
'Average' = [Math]::Round($_.Average)
'Maximum' = $_.Maximum
'Minimum' = $_.Minimum
}
}
$ping | Measure-Object -Property ResponseTime -Average -Minimum -Maximum | Select-Object @param
$ping | Measure-Object -Property ResponseTime -Average -Minimum -Maximum | Format-Table @param
错误
Select-Object : The IPV6 Address key is not valid.
At line:15 char:76
+ ... operty ResponseTime -Average -Minimum -Maximum | Select-Object @param
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Select-Object], NotSupport
edException
+ FullyQualifiedErrorId : DictionaryKeyIllegal,Microsoft.PowerShell.Comman
ds.SelectObjectCommand
Format-Table : The IPV6 Address key is not valid.
At line:16 char:76
+ ... roperty ResponseTime -Average -Minimum -Maximum | Format-Table @param
+ ~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Format-Table], NotSupporte
dException
+ FullyQualifiedErrorId : DictionaryKeyIllegal,Microsoft.PowerShell.Comman
ds.FormatTableCommand
ds.SelectObjectCommand
以下是我通常如何创建自定义属性,但是对于较大的对象,它变得笨拙且更难以管理。
$ping | Measure-Object -Property ResponseTime -Average -Minimum -Maximum | Format-Table -Property `
@{N="Address";E={$ping[0].Address}},
@{N="IPV4 Address";E={$ping[0].IPV4Address.IPAddressToString}},
@{N="IPV6 Address";E={$ping[0].IPV6Address.IPAddressToString}},
@{N="Count";E={$_.Count}},
@{N="Average";E={[Math]::Round($_.Average)}},
@{N="Maximum";E={$_.Maximum}},
@{N="Minimum";E={$_.Minimum}}
期望的输出
Address IPV4 Address IPV6 Address Count Average Maximum Minimum
------- ------------ ------------ ----- ------- ------- -------
Google.co.uk 216.58.206.67 2a00:1450:4009:814::2003 10 12 13 12
【问题讨论】:
标签: powershell