Powershell 4中的递归深度

问题描述

我正在尝试在给定目录内的子文件夹中提取一小部分信息。我对Powershell还是很陌生,但是经过大约一个星期的摸索,我设法将它们放在一起,这对于我在Powershell 5上的目的来说是完美的。麻烦的是,我需要制作一个等效的脚本才能在Powershell 4中运行唯一似乎无效的是-Depth,这是必须的。

$FolderPath = dir -Directory -Path "D:\FILEPATH\" -Recurse -Force -Depth 3
$Report = @()
Foreach ($Folder in $FolderPath) {
    $Acl = Get-Acl  -Path $Folder.FullName
    foreach ($Access in $acl.Access)
        {
        if (!($Access.IdentityReference -in $Exclude)) {
            $Properties = [ordered]@{
            'Folder and Location'=$Folder.FullName;
            'AD Group or User'=$Access.IdentityReference; 
            'Permissions'=$Access.FileSystemRights;
            'Inherited'=$Access.IsInherited}

            $Report += New-Object -TypeName PSObject -Property $Properties
        }
    }
}
$Report | Export-Csv -path "FILEPATH\Test.csv"

我尝试在其中添加"\*\**\***"及其几个变体以获得所需的结果,但是它们要么从第3个子文件夹开始就给了我,要么什么都不返回。我还研究了Get-ChildItemToDepth设置,但似乎只限于功能,而且我不知道如何将其包含在当前内容中。任何指导将不胜感激!

解决方法

您可以做的是获取每个级别的非递归列表,根文件夹,然后是-depth 3中将包含的三个递归级别。您可以在PS5机器上运行此程序,并对照$FolderPath.count检查-Depth 3的使用次数,然后计数应该相同。

$FolderPath = $(dir -Directory -Path "D:\FILEPATH\*" -Force
dir -Directory -Path "D:\FILEPATH\*\*" -Force
dir -Directory -Path "D:\FILEPATH\*\*\*" -Force
dir -Directory -Path "D:\FILEPATH\*\*\*\*" -Force)