字典或哈希表的 Powershell 列表

Powershell list of dictionaries or hashtable

我想创建一个词典列表,以便将其导出为 CSV。正如您在 PowerShell 中可能知道的那样,如果您想将充满字符串的数组导出到 CSV 文件,它不会起作用,所以我必须为此创建对象。

但是我这样做的方式是规避的,我不认为这是创建 dictionaries/hashtable 列表的正确方法,但是我没有为重复代码创建函数(抱歉为此)。

#add the log file.
$savegrp = Get-Content "J:\savegrp.dec.2015.log"
#filtering into a var just Succeeded/Failed data then replace  space with ';' and remove ','
$succeded = ($savegrp -cmatch "Succeeded:") -replace "\s+",";" -replace ",",""
$failed = ($savegrp -cmatch " Failed: ") -replace "\s+",";" -replace ",",""

#creating container where all data will be added
$container = @()
#just a date to save the csv file if this script will be scheduled 
$date =  Get-Date -format dd.MMMM.yyyy

#take each line of data with 'Succeeded' match and iterate it
for($i=0;$i -le ($succeded.count-1);$i++) {

    #split each line by ';' to see how many items are per one line
    $s1 = ($succeded[$i]).split(";")

    #if in one 'Succeeded' line are just 6 element then is just one server which is ok
    if ($s1.count -eq 6){

        $PropertyHash = @{}
        $PropertyHash =  @{
            "Date" = $s1[1] + " " + $s1[0]
            "Time" = $s1[2]
            "Client" = $s1[5]
            "Status" = $s1[4].Substring(0,9)
        }
        $container += New-Object -TypeName PSObject -Property $PropertyHash
    }
    #if in one 'Succeeded' line are more servers, then stick all sererers to the same info in the line
    else{
        $count = ($s1.count)

        for($a = $count-1;$a -ge 5){

            $PropertyHash = @{}
            $PropertyHash +=  @{
                "Date" = $s1[1] + " " + $s1[0]
                "Time" = $s1[2]
                "Client" = $s1[$a]
                "Status" = $s1[4].Substring(0,9)
            }
            $container += New-Object -TypeName PSObject -Property $PropertyHash
            $a--
        }
    }
}

如果您有 PowerShell v3 或更新版本,像这样的东西应该可以正常工作:

for ($i = 0; $i -lt $succeded.Count; $i++) {
  $date1, $date2, $time, $null, $status, $clients = $succeded[$i].Split(';')
  $clients | ForEach-Object {
    New-Object -Type PSObject -Property @{
      'Date'   = "$date2 $date1"
      'Time'   = $time
      'Client' = $_
      'Status' = $status.Substring(0,9)
    }
  } | Export-Csv 'C:\path\to\output.csv' -NoType -Append
}

PowerShell 允许您将数组分配给变量列表,每个变量都从数组中获取一个元素,但最后一个除外,它获取所有剩余的数组元素。声明

$date1, $date2, $time, $null, $status, $clients = $succeded[$i].Split(';')

在相应的变量$date1$date2$time$status 中收集日期、时间和状态。第 4 个值通过将其分配给 $null 而被丢弃。拆分操作产生的数组的其余部分(客户端列表)分配给 "catch-all" 变量 $clients。然后,您可以将该变量通过管道传输到 ForEach-Object 语句中,您在其中为每个客户端创建一个对象并将它们附加到您的 CSV 文件(请注意,您至少需要 PowerShell v3 才能附加到 CSV)。

编辑: 如果您受困于 PowerShell v2 或更早版本,您也可以在循环外进行导出。但是,要使其工作,您需要在子表达式中 运行 它:

(for ($i = 0; $i -lt $succeded.Count; $i++) {
  $date1, $date2, $time, $null, $status, $clients = $succeded[$i].Split(';')
  $clients | ForEach-Object {
    New-Object -Type PSObject -Property @{
      'Date'   = "$date2 $date1"
      'Time'   = $time
      'Client' = $_
      'Status' = $status.Substring(0,9)
    }
  }
}) | Export-Csv 'C:\path\to\output.csv' -NoType

或者先将结果收集到一个变量中:

$container = for ($i = 0; $i -lt $succeded.Count; $i++) {
  $date1, $date2, $time, $null, $status, $clients = $succeded[$i].Split(';')
  $clients | ForEach-Object {
    New-Object -Type PSObject -Property @{
      'Date'   = "$date2 $date1"
      'Time'   = $time
      'Client' = $_
      'Status' = $status.Substring(0,9)
    }
  }
}

$container | Export-Csv 'C:\path\to\output.csv' -NoType