[英]Stripping value in PS and comparing if it is an integer value
I am running PS cmdlet get-customcmdlet
which is generating following output 我正在运行PS cmdlet get-customcmdlet
,它将生成以下输出
Name FreeSpaceGB
---- -----------
ABC-vol001 1,474.201
I have another variable $var=vol
Now, I want to strip out just 001
and want to check if it is an integer. 我还有另一个变量$var=vol
现在,我想只001
并检查它是否为整数。
I am using but getting null value 我正在使用但获取空值
$vdetails = get-customcmdlet | split($var)[1]
$vnum = $vdetails -replace '.*?(\d+)$','$1'
My result should be integer 001
我的结果应该是整数001
Assumption: get-customcmdlet
is returning a pscustomobject
object with a property Name
that is of type string
. 假设: get-customcmdlet
返回的pscustomobject
对象的属性Name
为string
类型。
$var = 'vol'
$null -ne ((get-customcmdlet).Name -split $var)[1] -as [int]
This expression will return $true
or $false
based on whether the cast is successful. 该表达式将根据转换是否成功返回$true
或$false
。
If your goal is to pad zeroes, you need to do that after-the-fact (in this case, I just captured the original string): 如果您的目标是填充零,则需要事后进行处理(在这种情况下,我只是捕获了原始字符串):
$var = 'vol'
$out = ((get-customcmdlet).Name -split $var)[1]
if ($null -ne $out -as [int])
{
$out
}
else
{
throw 'Failed to find appended numbers!'
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.