3

有谁知道如何在 get-childitem 等函数多次运行时显示输出(数组管道输入)?我会尝试用一个例子来展示它。

PS C:\Users> ".\Graimer", ".\Public" | Get-ChildItem


    Directory: C:\Users\Graimer


Mode                LastWriteTime     Length Name                                                            
----                -------------     ------ ----                                                            
d-r--        20.12.2012     15:59            Contacts                                                        
d-r--        06.01.2013     01:23            Desktop                                                         
d-r--        02.01.2013     17:15            Documents                                                       
d-r--        05.01.2013     16:38            Downloads                                                       
d-r--        20.12.2012     15:59            Favorites                                                       
d-r--        20.12.2012     15:59            Links                                                           
d-r--        20.12.2012     15:59            Music                                                           
d-r--        20.12.2012     15:59            Pictures                                                        
d-r--        20.12.2012     15:59            Saved Games                                                     
d-r--        20.12.2012     15:59            Searches                                                        
d-r--        20.12.2012     15:59            Videos                                                          


    Directory: C:\Users\Public


Mode                LastWriteTime     Length Name                                                            
----                -------------     ------ ----                                                            
d-r--        20.12.2012     13:57            Documents                                                       
d-r--        26.07.2012     10:13            Downloads                                                       
d-r--        26.07.2012     10:13            Music                                                           
d-r--        26.07.2012     10:13            Pictures                                                        
d-r--        26.07.2012     10:13            Videos                                                          

在我的函数中,当我使用数组输入和处理 { } 块时,Write-output 会将所有结果放在一个表中,就像它应该的那样。但是我怎么能像这样格式化呢?显示时按目录分隔,同时存储为标准对象数组?好像找不到关于它的文章。是否只能使用已编译的 cmdlet 没关系,无论如何我都在寻找一些线索:)

编辑:添加了我的代码。只需创建 psobjects 并使用 Write-Object 来显示它们。

Function Get-RecurseFileCount
{
    [CmdletBinding()]
    Param
    (
        [Parameter(ValueFromPipeline=$true)]
        [String[]]$Path = "."
    )
    Begin
    {
    }
    Process
    {
        foreach ($rootpath in $Path)
        {
            Write-Verbose "Testing if path of rootdirectory exists"
            if(Test-Path $rootpath)
            {
                Write-Verbose "Getting all recursive subfolders"
                $folders = Get-ChildItem $rootpath -Recurse -ErrorAction SilentlyContinue | where {$_.PSIsContainer}

                Write-Verbose "Starting to count files in rootfolder"
                $folder = Get-Item $rootpath
                $fcount = (Get-ChildItem $rootpath -ErrorAction SilentlyContinue | where {!$_.PSIsContainer}).Count
                New-Object psobject -Property @{FolderName = $folder.Name; FolderPath = $folder.FullName; FileCount = $fcount} | Write-Output

                Write-Verbose "Starting to count files in subfolders"
                foreach($folder in $folders)
                {
                   $fname = $folder.Name
                   $fpath = $folder.FullName
                   $fcount = (Get-ChildItem $fpath -ErrorAction SilentlyContinue | where {!$_.PSIsContainer}).Count
                   New-Object psobject -Property @{FolderName = $fname; FolderPath = $fpath; FileCount = $fcount} | Write-Output
                }
                Write-Verbose "Finished with filecount"
            }
        }
    }
    End
    {
    }
}

而且Format-Table -GroupBy ..不是答案。一种解决方案是添加一个名为的属性root = $rootpath并添加一个按属性分组的默认视图,并将其root隐藏在表中,这样它就不会多次显示。问题只是..如何?:)

4

1 回答 1

1

您必须在文件中提供GroupBy视图中的信息format.ps1xml并更新格式数据以获得所需的行为。在您的会话中编写ps1xml和加载它是没有办法的。

http://technet.microsoft.com/en-us/library/dd315396.aspx

于 2013-01-06T16:12:02.290 回答