Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell字典或哈希表列表_Powershell_Dictionary_Scripting_Hashtable - Fatal编程技术网

Powershell字典或哈希表列表

Powershell字典或哈希表列表,powershell,dictionary,scripting,hashtable,Powershell,Dictionary,Scripting,Hashtable,我想创建一个字典列表,以便将其导出到CSV。正如您在PowerShell中可能知道的那样,如果要将填充字符串的数组导出到CSV文件,它将无法工作,因此我必须为此创建对象 #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 ',' $suc

我想创建一个字典列表,以便将其导出到CSV。正如您在PowerShell中可能知道的那样,如果要将填充字符串的数组导出到CSV文件,它将无法工作,因此我必须为此创建对象

#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
中收集日期、时间和状态。通过将第四个值赋给
$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

是的,我有带5个元素的行和带5+n个元素的行,所以我想要那个isolatedGetEnumerator()$PropertyHash=[PSCustomObject]@{}?为什么不直接将
for
导入
Export Csv
&{for…}导出Csv
),这样就不必附加?@PetSerAl,这将需要在子表达式或脚本块中运行
for
循环,我尽量避免,因为我觉得语法很难看。scriptblock还会使
$succeed
的处理稍微复杂一些。如果OP有PowerShell v2,我宁愿在变量(
$container=for(…){…}
)中收集循环的结果,并将其导出到CSV。