在 PowerShell 中获取卷号 [英] Fetching volume number in PowerShell
问题描述
我正在运行 PS cmdlet get-customcmdlet
生成以下输出
I am running PS cmdlet get-customcmdlet
which is generating following output
Name FreeSpaceGB
---- -----------
ABC-vol001 1,474.201
ABC-vol002 2,345.437
ABC-vol003 3,147.135
random-value 4,147.135
我想从最高的 volume number
ABC-vol003
中捕获 003
我也想忽略 random-value
并且只想考虑其中包含 vol
的值
I want to capture 003
from the highest volume number
ABC-vol003
I also want to ignore random-value
and only want to consider the values which have vol
in it
get-customcmdlet | select Name
Name
----
ABC-vol001
ABC-vol001
ABC-vol001
random-value
在这里,我希望 003
根据最高音量而变化
Here, I want 003
to be variable based upon highest volume number
推荐答案
您可以进行自定义排序,然后选择最后一项,例如:
You could do a custom sort, and select the last item, like:
Get-CustomCmdlet | Sort {$_.Name -replace '.*?(\d+)$','$1'} | Select -Last 1
看起来您已经知道如何使用 Where
,因为您在编辑它之前在问题中已经有了它,但您只能使用它获取名称中包含vol"的卷,然后对它们进行排序...
It looked like you already knew how to use Where
, since you had that in your question before you edited it out, but you can use that to only get volumes that have 'vol' in the name, then sort those...
Get-CustomCmdlet | Where{$_.Name -match '-vol\d+'} | Sort {$_.Name -replace '.*?(\d+)$','$1'} | Select -Last 1
这篇关于在 PowerShell 中获取卷号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!