将此 PSCustomObject 列为 Table
List this PSCustomObject as a Table
我制作了一个简短的 "quick and dirty" PS-脚本来比较两个通常应该有相同文件的文件夹。所以我使用Get-FileHash
函数来比较所有文件。
我有一个 PSCustomObject,其中记录了所有检查。最后,我想将此 PSCustomObject 作为 Table 写入控制台。但是我不明白...
这是我的对象:
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...}
我想要的是 table 就像我可以使用 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 @()
如评论中所述,我重写了这部分。正如 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 *
现在有效。谢谢。
我制作了一个简短的 "quick and dirty" PS-脚本来比较两个通常应该有相同文件的文件夹。所以我使用Get-FileHash
函数来比较所有文件。
我有一个 PSCustomObject,其中记录了所有检查。最后,我想将此 PSCustomObject 作为 Table 写入控制台。但是我不明白...
这是我的对象:
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...}
我想要的是 table 就像我可以使用 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 @()
如评论中所述,我重写了这部分。正如 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 *
现在有效。谢谢。