以类似Get-ChildItem的类别显示输出

时间:2013-01-06 11:53:02

标签: powershell

有人知道如何在多次运行(数组管道输入)时显示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分组,并将其隐藏在表中,这样就不会多次显示。问题只是......怎么样? :)

1 个答案:

答案 0 :(得分:1)

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

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