2

下面的 PowerShell 脚本将列出所有共享文件夹(不包括隐藏的共享文件夹),然后列出所有子文件夹,最后获取每个子文件夹的 ACL 信息并导出为 CSV 文件。

但是,我正在尝试设置它可以钻入的子文件夹的限制。例如,如果我将其设置为 3,则脚本将获取前三个子文件夹的 ACL 信息。我怎样才能做到这一点?

输入:

path=\\server\sharefolder0\subfolder01\subfolder02
path=\\server\sharefolder1\subfolder11\subfolder12\subfolder13\subfolder14
path=\\server\sharefolder2

预期结果:

path=\\server\sharefolder0
path=\\server\sharefolder0\subfolder01
path=\\server\sharefolder0\subfolder01\subfolder02
path=\\server\sharefolder1
path=\\server\sharefolder1\subfolder11
path=\\server\sharefolder1\subfolder11\subfolder12
path=\\server\sharefolder2

这是代码:

$getSRVlist = Get-Content .\server.txt
$outputDirPath=".\DirPathList.txt"
$outputACLInfo=".\ACLInfo.CSV"
$header="FolderPath,IdentityReference,Rights"
Del $outputACLInfo
add-content -value $header -path $outputACLInfo
foreach ($readSRVlist in $getSRVlist)
{
   foreach ($readShareInfoList in $getShareInfoList=Get-WmiObject Win32_Share
   -computerName $readSRVlist | Where {$_.name -notlike "*$"} | %{$_.Name})
   {
      foreach ($readDirPathList in
      $getDirPathList=get-childitem \\$readSRVlist\$readShareInfoList -recurse
      | where {$_.PSIsContainer})# | %{$_.fullname})
      {
         $getACLList=get-ACL $readDirPathList.fullname | ForEach-Object
         {$_.Access}
         foreach ($readACLList in $getACLList)
         {
            $a = $readDirPathList.fullname + "," +
            $readACLList.IdentityReference + "," +  $readACLList.FileSystemRights
            add-content -value $a -path $outputACLInfo
         }
      }
   }
}
4

2 回答 2

8

递归是你的朋友。试试这个:

$maxDepth = 3

function TraverseFolders($folder, $remainingDepth) {
  Get-ChildItem $folder | Where-Object { $_.PSIsContainer } | ForEach-Object {
    if ($remainingDepth -gt 1) {
      TraverseFolders $_.FullName ($remainingDepth - 1)
    }
  }
}

TraverseFolders "C:\BASE\PATH" $maxDepth

编辑:现在我明白你的意思了。要检查给定路径的前三个父文件夹,请尝试以下操作:

$server   = "\\server\"
$path = ($args[0] -replace [regex]::escape($server), "").Split("\\")[0..2]
for ($i = 0; $i -lt $path.Length; $i++) {
  Get-ACL ($server + [string]::join("\", $path[0..$i])
}
于 2012-09-13T18:44:45.367 回答
5

在较新版本的 powershell 中,可以使用-DEPTH参数,One liner 可以帮助-

 get-childitem -path \\server\folder -Depth 2 -Directory | Select-object -Property Name, Fullname

它将搜索 2 个嵌套文件夹,并提供该特定文件夹的文件夹名称和完整路径。在版本-PSVersion 5.1.17134.858中测试

于 2019-08-30T14:46:38.457 回答