在 PowerShell 中从 2 个变量创建递增变量

Create an incrementing variable from 2 variables in PowerShell

好的,首先,我认为自己是新手,关于 PowerShell 有很多东西要学习,这是我第一次 post。 我试图遍历一些数据并将其放入自定义对象中,然后将它们放入单独的数组中以备后用。问题是我想通过使用计数器 $i 创建一个表示 $week_data1 的变量,这样我就可以减少所需的代码量。我确实有一个串联变量被写出: write-host '$week++ ='$week$i 但我认为它被表示为一个字符串? 我怎样才能得到 $week_data$i 来表示要插入数据的数组?

Input data. Each week ends on Saturday.
    $week1=@('2021-05-01')
    $week2=@('2021-05-02', '2021-05-03', '2021-05-04', '2021-05-05', '2021-05-06', '2021-05-07', '2021-05-08')
    $week3=@('2021-05-09', '2021-05-10', '2021-05-11', '2021-05-12', '2021-05-13', '2021-05-14', '2021-05-15')
    $week4=@('2021-05-16', '2021-05-17', '2021-05-18', '2021-05-19', '2021-05-20', '2021-05-21', '2021-05-22')
    $week5=@('2021-05-23', '2021-05-24', '2021-05-25', '2021-05-26', '2021-05-27', '2021-05-28', '2021-05-29')
    $week6=@('2021-05-30', '2021-05-31')
    $month =@($week1, $week2, $week3, $week4, $week5, $week6) 
    
Create the output structures to be populated.
    $week_data1=@()
    $week_data2=@()
    $week_data3=@()
    $week_data4=@()
    $week_data5=@()
    $week_data6=@()
    $month_data =@($week_data1, $week_data2, $week_data3, $week_data4, $week_data5, $week_data6)
Loop through the array and count the week number that is being processed.

    $i = 0
    foreach($week in $month)
    { $i++
    $n=0

Here I can write out a Variable and it concatenates properly.
    **write-host '$week++ ='$week$i**
        foreach($day in $week)
        {$n++
        write-host '$day ='$day
Pull in data from a .csv file to populate the custom object.
        foreach($line in $csv) 
         {
          if($line -match $day)
Match the line in the CSV file that has the correct Date in it. One line in the file per date in the month.
          { #write-host '$line.Day = ' $line.Day
            # custom object to be used later 
            $date_data = [PSCustomObject] @{
             week_numb = $i
             date = $line.Day
             attempts = $line.Attempts
             connects = $line.Connects
            }
I have tried different syntax versions but it does not work here? I want to put the custom object data into the new array for the week being processed. 
          #write-host '$week_data[$i]='$week_data[$i]
          $week_data$i += $date_data # Add data from csv file into a 
          #$week_data[$i] += $date_data
          }
         }
    
        }
    }

使用 $week_data$i 作为变量的问题我得到一个错误:

在 line:38 char:17

表达式或语句中出现意外标记“$i”。 + 类别信息:ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken

您正在寻找 变量 间接 , 即通过存储在另一个变量中的名称间接引用变量的能力或从表达式返回。

但是请注意,通常有更好的选择,例如使用数组或 hashtables as multi-value containers - see 作为示例。

如果确实需要使用变量间接寻址,请使用 Get-Variable and Set-Variable:

$week_data1 = 'foo', 'bar'

$i = 1

# Same as: $week_data1
# Note that "$" must NOT be specified as part of the name.
Get-Variable "week_data$i" -ValueOnly

# Same as: $week_data1 = 'baz', 'quux'
Set-Variable "week_data$i" baz, quux

# Updating an existing value requires nesting the two calls:
# Same as: $week_data1 += 'quuz'
Set-Variable "week_data$i" ((Get-Variable "week_data$i" -ValueOnly) + 'quuz')

顺便说一句:用 +=“扩展”数组很方便,但效率低下:每次都必须在幕后创建一个 new 数组 - 请参阅 .

同样,与直接赋值和变量引用相比,调用 cmdlets 设置和获取变量的性能较差。


至于你试过的

  • $week_data$i = ...是赋值表达式,解释为直接并列两个变量,$week_data$i,这会导致您看到的 语法错误

  • 相比之下,Write-Output $week_data$i是一个命令,而$week_data$i也被解释为两个变量引用,作为命令参数它在语法上是有效的,并且会简单地传递两个连接变量值;换句话说:$week_data$i 就好像它被双引号一样,即 expandable string,因此该命令等同于 Write-Output "$week_data$i"

与答案无关,但可能对您有帮助,我有一个函数可以确定给定日期是一个月中的第几周。

Function Get-Week{
[cmdletbinding()]
param([parameter(ValueFromPipeline)][string[]]$Date)
process{
    ForEach($Day in $Date){
        $DTDay=[datetime]$Day
        $Buffer = ([datetime]("{0}-01-{1}" -f $DTDay.Month,$DTDay.Year)).dayofweek.value__ -1 
        [math]::Truncate(($DTDay.Day+$Buffer)/7)+1
    }
}
}

因此,您输入一个可以转换为日期的字符串,例如:

'5-13-2021' | Get-Week

Get-Week '5-13-2021'

然后您会得到一个数字,指示该天所在月份的第几周(周六结束)。