簡體   English   中英

gci 和 gci -recurse 有不同類型的輸出

[英]gci and gci -recurse have different kind of outputs

我正在嘗試制作文件系統清單。

這有效,它給了我每個條目的 ower 和 ACL

Get-ChildItem \\Server\Share\* |  Select-Object @{n='Path';e={ (Get-Item $_.PSPath).FullName }}, PSIsContainer, @{n='Owner';e={ (Get-Acl $_).Owner}}, @{n='Accesstostring';e={ (Get-Acl $_).Accesstostring}}

但是使用-Recurse給了我空的 Owner 和 Accesstostring

Get-ChildItem \\Server\Share\ -Recurse |  Select-Object @{n='Path';e={ (Get-Item $_.PSPath).FullName }}, PSIsContainer, @{n='Owner';e={ (Get-Acl $_).Owner}}, @{n='Accesstostring';e={ (Get-Acl $_).Accesstostring}}

為什么gci在管道上發送不同的東西? 我該如何解決這個問題? (我不想創建一個數組,因為它不適合內存)

它們是不同的,因為一個數組包含一個文件列表,但在遞歸中它是一個目錄對象數組,每個目錄對象都包含一個文件列表。 下面的代碼會做你想要的。 請注意,如果路徑包含空格,則路徑需要用引號引起來。

Get-ChildItem \\Server\Share\ -Recurse | Select-Object @{n='Path';e={ $_.FullName }}, PSIsContainer, @{n='Owner';e={ (Get-Acl $_.FullName).Owner}}, @{n='Accesstostring';e={ (Get-Acl $_.FullName).Accesstostring}}

擴展 @Edjs-perkums answer ,這會調用一次Get-Acl並在管道中的第二個Select-Object中擴展它的兩個屬性。 (為了清楚起見,也重新格式化為多行,但它是一個單一的管道。)

Get-ChildItem \\Server\Share\ -Recurse `
    | Select-Object @{n='Path';e={ $_.FullName }}, 
                    @{n='ACL';e={ (Get-Acl $_.Fullname) }},
                    PSIsContainer `
    | Select-Object Path, PSIsContainer, 
                    @{n='Owner';e={ $_.ACL.Owner}}, 
                    @{n='Accesstostring';e={ $_.ACL.Accesstostring}}    

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM