我想创建一个词典列表,以便我将其导出为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--
}
}
}
答案 0 :(得分:1)
如果你有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