如何在表达式中增加变量的值?

Aws*_*saf 6 powershell

我有以下代码:

$b = 1
Import-Csv c:\Awsaf\powershell\Beforenew.csv | select name, CustomAttribute1, CustomAttribute2, @{n='Counter';e={$b}} | Export-Csv -NoTypeInformation c:\Awsaf\PowerShell\afternew.csv
Run Code Online (Sandbox Code Playgroud)

我想将$ b的值递增1.我已经尝试了$ b ++,$ b + =,for循环,do-while,似乎没有任何工作.我该怎么做?

我也尝试了以下附加代码,但我无法弄清楚如何增加$ b的值.

$b = 0
Import-CSV c:\Awsaf\powershell\afternew.csv -Delimiter ',' | `
ForEach-Object { $_.Counter = "$b"; return $_ } | `
Export-CSV c:\awsaf\powershell\afterX.csv -Delimiter ',' -NoTypeInformation
Run Code Online (Sandbox Code Playgroud)

Rom*_*min 11

对变量'b'使用显式脚本范围.增量(++)后不要忘记输出值.

这是适合我的代码(它是你的稍微改变版本):

# Prepare some data for the test
Get-ChildItem | Select-Object Name | Export-Csv test1.csv -NoTypeInformation

# 1) Use explicit script scope for the variable 'b'
# 2) After increment (++) do not forget to output it
$script:b = 0
Import-Csv test1.csv |
select Name, @{n='Counter'; e={$script:b++; $script:b}} |
Export-Csv -NoTypeInformation test2.csv
Run Code Online (Sandbox Code Playgroud)