A Get-ChildItem 中的 Get-ChildItem

时间:2021-06-09 02:44:21

标签: powershell

我对正在编写的脚本有疑问。我需要一个可以在特定文件夹中找到所有文件夹的脚本(在本例中为 WRA),但我也想在 WRA 中找到子文件夹 例如: \Server01\WRA 包含 100 个文件夹 \Server01\WRA\文件夹 1 \Server01\WRA\Folder 2 ... ...

我想查看“文件夹 1”内部并获取“文件夹 1”中文件夹的报告并找到可以使用 30 天以上的文件夹

Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))}

这是我目前所拥有的

$FolderNames = Get-ChildItem "\\Server01\WRA\" -Directory | 
    Select-Object Name, LastWriteTime

$FolderNames

感谢大家的帮助

2 个答案:

答案 0 :(得分:1)

你可以使用这个:

Get-ChildItem "\\Server01\WRA\" -Directory |
Get-ChildItem -Directory |
Where-Object -Property LastWriteTime -LT ([datetime]::Now).AddDays(-30) |
Select-Object Name, LastWriteTime

编辑

$directories = Get-ChildItem "\\Server01\WRA\" -Directory
$targetDate = ([datetime]::Now).AddDays(-30)

foreach($parentDir in $directories)
{
    $subFolders = Get-ChildItem $parentDir -Directory |
    Where-Object -Property LastWriteTime -LT $targetDate
    
    if($subFolders)
    {
        "Parent Directory: {0}" -f $parentDir.FullName
    }

    foreach($childDir in $subFolders)
    {
        "Parent SubFolder: {0}" -f $childDir.FullName

        $childDir | Select-Object Name, LastWriteTime | Out-String
    }
}

答案 1 :(得分:0)

非常感谢您的 helo Santiago!

我使用了你写的脚本,但出现了以下错误:

Get-ChildItem : Cannot find path 'C:\Users\cubam1\Lonna' because it does not exist.
At line:6 char:19
+     $subFolders = Get-ChildItem $parentDir -Directory |
+                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\cubam1\Lonna:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

Get-ChildItem : Cannot find path 'C:\Users\cubam1\Miguel' because it does not exist.
At line:6 char:19
+     $subFolders = Get-ChildItem $parentDir -Directory |
+                   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Users\cubam1\Miguel:String) [Get-ChildItem], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand

它似乎没有抓取正确的 PATH,路径应该是 \Server01\WRA,但出于某种原因,它试图在“C:\Users\Cubam1”中找到它们 谢谢