我想創建一個字典列表,以便將其導出爲CSV。正如你可能在PowerShell中所知道的那樣,如果你想將一個填充了字符串的數組導出爲CSV文件,它將不起作用,所以我必須爲此創建對象。Powershell字典或哈希表的列表
但是我這樣做的方式,如果迴避,我不覺得這是創建一個字典/散列表的正確方式,但我沒有創建一個函數重複的代碼(對不起) 。
#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--
}
}
}
是啊,我有5個元素,併線,5號線+ n個元素,所以我想要隔離 – Xao
GetEnumerator()? $ PropertyHash = [PSCustomObject] @ {}? –