powershell - 将 PowerShell 中的整数初始化为负值后递增

标签 powershell powershell-2.0

我有以下代码将值初始化为 -1:

Set-Variable -Name ID -Value -1 -Scope local

当我这样做时,
$local:ID++

我收到以下错误:

The '++' operator works only on numbers. The operand is a 'System.String'.



我认为它会隐含地将其视为 int。但是,我必须使用一种不整洁的解决方法:

解决方法:
$intVal = [int]$local:ID
$intval ++

还有另一种方法吗?

最佳答案

$id 是一个字符串(键入 $id.gettype() 以查找其类型)。要将其初始化为整数,请将其放在括号中:

PS> Set-Variable -Name ID -Value (-1) -Scope local
PS> ($local:ID++) #increment and print the variable 
0

关于powershell - 将 PowerShell 中的整数初始化为负值后递增,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10583986/

相关文章:

PowerShell、Web 请求和代理

powershell - 将 Powershell 对象的类型名称从 PSCustomObject 更改为我选择的名称?

powershell - 为什么命令Add-ADGroupMember不再与参数-Member一起使用?

powershell - 选择对象选择多个对象,但拆分结果并对其进行修整,然后导出为CSV

Powershell 功能不起作用

powershell - 如何添加多线程?

arrays - 如何使用PSv2在PSobjects数组中搜索特定对象键

powershell - 连接 2 个变量 (Powershell)

winforms - 将输入变量从 PowerShell 表单传递到 cmdlet 参数会导致空白数据

excel - 如何通过 PowerShell 添加 Excel 辅助轴标签?