簡體   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