将此PSCustomObject列为表

时间:2017-03-09 09:49:51

标签: powershell powershell-v4.0

我做了一个短暂的"快速和肮脏" PS-Script比较两个文件夹,它们通常具有相同的文件。所以我使用Get-FileHash函数来比较所有文件。

我有一个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 @()

1 个答案:

答案 0 :(得分: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 *

立即行动。谢谢。