【问题标题】:Recursion depth in Powershell 4Powershell 4 中的递归深度
【发布时间】:2020-09-28 19:52:45
【问题描述】:

我正在尝试提取给定目录中子文件夹的少量信息。我对 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 设置,但这似乎仅限于功能,我不知道如何将其包含在我目前拥有的内容中。任何指导将不胜感激!

【问题讨论】:

  • 您可以计算深度并丢弃不符合您需要的深度。例如,('C:\Program Files\Common Files\microsoft shared\TextConv' -replace '[^\\]', '').Length 将给出 4 作为深度。

标签: powershell powershell-4.0


【解决方案1】:

您可以做的是获取每个级别的非递归列表、根文件夹,然后是 -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)

【讨论】:

  • 这非常有效!真是太感谢你了,这件事让我头疼了太久了。
猜你喜欢
  • 2017-02-20
  • 1970-01-01
  • 2021-10-28
  • 2015-02-28
  • 2010-10-25
  • 2020-08-06
  • 2016-07-12
  • 1970-01-01
  • 2012-03-21
相关资源
最近更新 更多