2017-03-09 49 views
0

我做了一個簡短的「快速和骯髒的」PS腳本比較兩個文件夾,它們通常都有相同的文件。所以我使用Get-FileHash函數來比較所有文件。列出此PSCustomObject爲表

我有一個PSCustomObject,其中記錄了所有檢查。最後,我想將這個PSCustomObject作爲表格寫入控制檯。但我不明白這一點...

這是我的目標:

PS C:\Windows\System32\WindowsPowerShell\v1.0> $statushash | Get-Member 


    TypeName: System.Management.Automation.PSCustomObject 

Name  MemberType Definition      
----  ---------- ----------      
Equals  Method  bool Equals(System.Object obj) 
GetHashCode Method  int GetHashCode()     
GetType  Method  type GetType()     
ToString Method  string ToString()     
Check  NoteProperty Object[] Check=System.Object[] 
FileSrv1 NoteProperty Object[] FileSrv1=System.Object[] 
FileSrv2 NoteProperty Object[] FileSrv2=System.Object[] 
Hash1  NoteProperty Object[] Hash1=System.Object[] 
Hash2  NoteProperty Object[] Hash2=System.Object[] 

PS C:\Windows\System32\WindowsPowerShell\v1.0> $statushash 


FileSrv1 : {GPT.INI, comment.cmtx, Registry.pol, fdeploy.ini...} 
FileSrv2 : {GPT.INI, comment.cmtx, Registry.pol, fdeploy.ini...} 
Hash1 : {AC88A2F34AB6BE659F511ACB1B8AB74B61CC64F5DB4B5DE384D51C7ABBDBC959, E0A3EC4393A1EDD1A5EB8D1E640DCE2F1BE0C2C33B6753A82C937EDDEC0C75C7, 
      C5D6108E5CDE1823AA63D333F2D178C00E0F69613F71CFFB7D12779A5810B320, 5AD8F52071D25165E7E68064AB194EC27A074A3846149ED0689AF23E7F7F2D00...} 
Hash2 : {AC88A2F34AB6BE659F511ACB1B8AB74B61CC64F5DB4B5DE384D51C7ABBDBC959, E0A3EC4393A1EDD1A5EB8D1E640DCE2F1BE0C2C33B6753A82C937EDDEC0C75C7, 
      C5D6108E5CDE1823AA63D333F2D178C00E0F69613F71CFFB7D12779A5810B320, 5AD8F52071D25165E7E68064AB194EC27A074A3846149ED0689AF23E7F7F2D00...} 
Check : {True, True, True, True...} 

我要的是一個表像我可以使用GCI(例如):

PS C:\Windows\System32\WindowsPowerShell\v1.0> gci | Select Name, Mode, Extension, Attributes 

Name       Mode Extension Attributes 
----       ---- --------- ---------- 
de       d-----   Directory 
de-DE       d-----   Directory 
Examples      d-----   Directory 
Modules      d-----   Directory 
Schemas      d-----   Directory 
SessionConfig     d-----   Directory 
Certificate.format.ps1xml  -a---l .ps1xml  Archive 
Diagnostics.Format.ps1xml  -a---l .ps1xml  Archive 
DotNetTypes.format.ps1xml  -a---l .ps1xml  Archive 
Event.Format.ps1xml   -a---l .ps1xml  Archive 
FileSystem.format.ps1xml  -a---l .ps1xml  Archive 

我已經用$hashstatus | Format-Table$statushash | Select -expand *嘗試過了,但它不起作用。

對不起這個愚蠢的問題,但我不是一個PS專業版,我真的不明白它...先謝謝您的幫助!

編輯: 這裏是我初始化自定義對象的一部分:

$statushash = New-Object PSCustomObject 
    $statushash | add-member -MemberType NoteProperty -name FileSrv1 -Value @() 
    $statushash | add-member -MemberType NoteProperty -name FileSrv2 -Value @() 
    $statushash | add-member -MemberType NoteProperty -name Hash1 -Value @() 
    $statushash | add-member -MemberType NoteProperty -name Hash2 -Value @() 
    $statushash | add-member -MemberType NoteProperty -name Check -Value @() 
+0

你試過$ hashstatus |格式表*? –

+0

嗨大衛。是的,我嘗試過。我沒有得到任何結果,如果我這樣做:'PS C:\ Windows \ System32 \ WindowsPowerShell \ v1.0> $ hashstatus |格式表* PS C:\ Windows \ System32 \ WindowsPowerShell \ v1.0>'也許我使用了錯誤的數據類型來解決我的問題... – frupfrup

+1

@frupfrup你有屬性內的數組;您需要一個pscustom對象數組才能通過format-table正確顯示。你的腳本是什麼? –

回答

0

正如我改寫了這部分的評論中提到。正如Frode F.所說我製作了一系列PSCustomObject:

$statushash = @() 

function comparefile ($file1, $file2){ 

    $erg = New-Object PSCustomObject 
    $erg | add-member -MemberType NoteProperty -name FileSrv1 -Value $file1.Name 
    $erg | add-member -MemberType NoteProperty -name Hash1 -Value (Get-FileHash -Algorithm MD5 $file1.Fullname | select -Expand Hash) 
    $erg | add-member -MemberType NoteProperty -name FileSrv2 -Value $file2.Name 
    $erg | add-member -MemberType NoteProperty -name Hash2 -Value (Get-FileHash -Algorithm MD5 $file2.Fullname | select -Expand Hash) 
    $erg | add-member -MemberType NoteProperty -name Check -Value $false 

    if($erg.hash1 -eq $erg.hash2){ 
     $erg.check = $true 
    } 
    else{ 
     $erg.check = $false 
    } 
    return $erg  
} 

for ([...]){ 
    $statushash += comparefile $List1[$i] $List2[$i] 
} 

$statushash | Format-Table * 

現在就開始工作。謝謝。