有谁知道如何显示输出从像get-childitem它运行多次(阵列管道输入)的功能?我会试着用一个例子来展示它。像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
在我的功能,当我使用数组输入和处理{}块,写输出像它应该把所有的结果在一个表中。但是我怎样才能像这样格式化呢?在显示时按目录分开,同时作为标准对象数组存储?似乎无法找到它的文章。不要紧,如果它是唯一可能的编译cmdlet,我只是寻找一些线索,不管是什么:)
编辑:添加我的代码。只需创建psobjects并使用写入对象来显示它们即可。
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
组成,并隐藏在表中,因此不会多次显示。问题是......怎么样? :)
您能分享您的代码吗? –
立即添加。任何使用写输出的函数都会执行,因为我的代码的行为应该像它应该那样。它是Get-ChildItem(DirectoryInfo/FileInfo对象),它有一些特殊的东西:) –
我缩小了它的类型/视图定义(不知道区别),它们存储在ps1xml文件中。 FileInfo/DirectoryInfo类型有一个默认的视图定义,按目录(这是我想要的)按目录分组,但必须有一个更简单的方法,无需创建ac#类,两个xml文件并手动更新PS会话使用新的xml文件。任何人都可以简化这一切的工作? :) –