简体   繁体   English

剥离PS中的值并比较是否为整数值

[英]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对象的属性Namestring类型。

$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.

相关问题 如何从 PS 表达式中捕获值? - How to capture value from a PS expression? ngModel不绑定整数值 - ngModel not binding integer value 用LEQ比较批处理文件中的数值继续返回假值 - Comparing Numeric Value in Batch File with LEQ Keep Returning False Value 如何将 PropertyItem(水平分辨率)值转换为整数? - How to convert PropertyItem(Horizontal resolution) value to integer? PDCURSES将分配值赋给整数make错误 - PDCURSES assignign value to integer make error Delphi DLL问题-错误的整数值和访问冲突问题 - Delphi DLL Issues - wrong integer value recevied and access violation issue 如何使用 Java 从 Windows 注册表读取 integer 值? - How to read integer value from Windows registry using Java? 为什么 usize::max_value() 在 64 位 Windows 上返回无符号 32 位整数的最大值? - Why does usize::max_value() return the maximum value of an unsigned 32 bit integer on 64 bit Windows? C群集无符号64位整数的计数,最大值为15 - C Population Count of unsigned 64-bit integer with a maximum value of 15 缺少将inspect.exe与UIAComWrapper.dll进行比较的子级返回的AE.RootElement.FindAll(TreeScope.Children,Condition.TrueCondition)值 - Missing child comparing inspect.exe to UIAComWrapper.dll returned value of AE.RootElement.FindAll(TreeScope.Children, Condition.TrueCondition)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM