gpt4 book ai didi

powershell - 您可以在PowerShell属性中指定单位吗?

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

我正在使用PowerShell来衡量网络吞吐量。我程序的最后一行是:
[pscustomobject][ordered]@{Endpoint=$ip; Time=$timeInMilliseconds; Bytes=$totalBytes; Rate=[math]::Round(($totalBytes / $timeInMilliseconds))};
这将返回如下所示的输出:

Endpoint       Time   Bytes   Rate
-------- ---- ----- ----
127.0.0.1:9381 15 8388608 559241

有没有一种方法可以向用户提示时间和费率的单位?我认为答案是否定的,这可以解释为什么 Get-Date之类的命令具有带有直观名称的字段,例如 (Get-Date).Second

我不想传递很难键入的难看的属性名称。例如, [pscustomobject][ordered]@{Endpoint=$ip; Time=$timeInMilliseconds; Bytes=$totalBytes; "Rate (KB/sec)"=[math]::Round(($totalBytes / $timeInMilliseconds))};有效并产生所需的输出
Endpoint       Time   Bytes Rate (KB/sec)
-------- ---- ----- -------------
127.0.0.1:9405 9 8388608 932068

但是现在 SelectWhere-Object之类的操作变得混乱。如果要将自定义对象另存为 $x,则必须使用 $x.'Rate (KB/sec)'访问rate字段。同样,我认为没有办法做我想做的事,我应该重新考虑我的做法。

最佳答案

您可以使用Add-Member添加一个称为AliasPropertyRate,它指向更混乱的Rate (KB/sec):

$object = [PsCustomObject]@{
'Rate (KB/Sec)' = "1024"
}


$object | Add-Member -MemberType AliasProperty -Name Rate -Value 'Rate (KB/sec)'

然后,用户可以使用凌乱的版本进行显示,使用更简洁的版本进行脚本编写:
$object.Rate
1024

要查看常见cmdlet中的别名属性的示例,只需查看 Get-Process的属性:
Get-Process | Get-Member

Name MemberType Definition
---- ---------- ----------
Handles AliasProperty Handles = Handlecount
Name AliasProperty Name = ProcessName
NPM AliasProperty NPM = NonpagedSystemMemorySize64
PM AliasProperty PM = PagedMemorySize64
SI AliasProperty SI = SessionId
VM AliasProperty VM = VirtualMemorySize64
WS AliasProperty WS = WorkingSet64

关于powershell - 您可以在PowerShell属性中指定单位吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58905096/

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