繁体   English   中英

A Get-ChildItem 内的 Get-ChildItem

[英]Get-ChildItem inside the A Get-ChildItem

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

我想查看“文件夹 1”并获取“文件夹 1”中文件夹的报告并找到 30 天以上的文件夹,这可以通过

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

这是我到目前为止所拥有的

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

$FolderNames

谢谢你的帮助

你可以使用这个:

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
    }
}

非常感谢您的直升机圣地亚哥!

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

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”中找到它们谢谢

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM