2016-06-10 2 views
0

비교 개체의 결과를 CSV로 내보내려고하지만 내보낼 때 오류가 발생합니다. 그냥 Excel에서 불러도 괜찮아 보입니다. 내 추측은 값 대신 오류가 하나 이상의 값으로 출력 될 때마다입니다. 여기결과를 CSV로 내보낼 때 시스템 개체 오류가 발생했습니다

VKEY 
V-12345 
V-23456 
V-1111 

current.csv

VKEY 
V-12345 
V-6789 
V-23456 
V-256 

나의 새로운 CSV는 내가 지금 무엇입니까 것은

Past, Current 
V-6789,V-1111 
V-256 

말을해야 내 CSV를 past.csv 있습니다

Past, Current 
System.Object[],@{vkey=V-1111} 

. System.Object[]$obj.Past는 열에 표시 그게

$Past = Import-CSV "past.csv" 
$Current = Import-CSV "Current.csv" 

$pastchange = Compare-Object $Past $Current -Property vkey | Where-Object {$_.SideIndicator -eq '=>'} | Select-Object VKEY 
$currentchange = Compare-Object $Past $Current -Property vkey | Where-Object {$_.SideIndicator -eq '<='} | Select-Object VKEY 

$obj = New-Object PSObject 
$obj | Add-Member NoteProperty Past $pastchange 
$obj | Add-Member NoteProperty Current $currentchange 
$obj | Export-Csv "ChangeResults.csv" -NoTypeInformation 

답변

1

는 단순히 사용자의 배열 $obj.Past 열에 @{vkey=V-1111} 유사한 개체. 증명는 :

$csvOutFile = "d:\test\ChangeResults.csv" # change to fit your circumstances 
$PastInFile = "d:\test\past.csv" 
$CurrInFile = "d:\test\curr.csv" 

$Past = Import-CSV $PastInFile 
$Curr = Import-CSV $CurrInFile 

# compare CSV files and convert results to arrays 
[email protected](,          <# always return an array   #> 
      $(Compare-Object $Past $Curr -Property vkey | 
       Where-Object { $_.SideIndicator -eq '=>' }) | 
      ForEach-Object { $_ | Select-Object -ExpandProperty vkey } 
     ) 
[email protected](,          <# even if no SideIndicator matches #> 
      $(Compare-Object $Past $Curr -Property vkey | 
       Where-Object { $_.SideIndicator -eq '<=' }) | 
      ForEach-Object { $_ | Select-Object -ExpandProperty vkey } 
     ) 

[System.Collections.ArrayList]$csvout = New-Object System.Collections.ArrayList($null) 
$auxHash = @{}          # an auxiliary hash object 

$max = ($CurrCh.Count, $PastCh.Count | Measure-Object -Maximum).Maximum 
for ($i=0; $i -lt $max; $i++) { 
    Try { $auxHash.Past = $PastCh.GetValue($i) } Catch { $auxHash.Past = '' } 
    Try { $auxHash.Curr = $CurrCh.GetValue($i) } Catch { $auxHash.Curr = '' } 
    $csvout.Add((New-Object PSObject -Property $auxHash)) > $null 
} 
$csvout | Format-Table -AutoSize  # show content: 'variable $csvout' 

$csvout | Export-Csv $csvOutFile -NoTypeInformation 
Get-Content $csvOutFile    # show content: "output file $csvOutFile" 

출력 : 여기

PS D:\PShell> D:\PShell\SO\37753277.ps1 

Past Curr 
---- ---- 
V-6789 V-1111 
V-256   


"Past","Curr" 
"V-6789","V-1111" 
"V-256","" 

PS D:\PShell> 

Try ... Catch 블록에 대한 대안 :

PS D:\PShell> $obj 
$obj.Past.Gettype() | Format-Table 
$obj.Current.Gettype() 
"---" 
$obj.Past | ForEach-Object { $_.Gettype() } 

Past          Current         
----          -------         
{@{vkey=V-6789}, @{vkey=V-256}}   @{vkey=V-1111}       

IsPublic IsSerial Name          BaseType     
-------- -------- ----          --------     
True  True  Object[]         System.Array    

IsPublic IsSerial Name          BaseType     
-------- -------- ----          --------     
True  False PSCustomObject       System.Object    
--- 
True  False PSCustomObject       System.Object    
True  False PSCustomObject       System.Object    

내 솔루션 ArrayList Class (.NET Framework)의 사용합니다

<# another approach instead of `Try..Catch`: 
    if ($i -lt $PastCh.Count) { $auxHash.Past = $PastCh.GetValue($i) 
         } else { $auxHash.Past = '' } 
    if ($i -lt $CurrCh.Count) { $auxHash.Curr = $CurrCh.GetValue($i) 
         } else { $auxHash.Curr = '' } 
    #> 
관련 문제