【问题标题】:PowerShell to get Folder Owner 3 Folders DeepPowerShell 获取文件夹所有者 3 个文件夹深度
【发布时间】:2015-03-25 02:17:29
【问题描述】:

我需要获取共享网络驱动器上所有文件夹所有者的列表。但是,我想将递归限制为只有 3 个文件夹深度(尽管我们告诉他们不要这样做,但我们的一些用户会创建几级深度的文件夹)。我找到了下面的脚本,并对其稍作修改以仅提供文件夹所有者(它最初为 ACL 返回了更多信息),但它仍然贯穿每个文件夹级别。如何修改它以仅返回 3 个文件夹级别?

$OutFile = "C:\temp\FolderOwner.csv" # indicates where to input your logfile#
$Header = "Folder Path;Owner"
Add-Content -Value $Header -Path $OutFile 

$RootPath = "G:\" # which directory/folder you would like to extract the acl permissions#

$Folders = dir $RootPath -recurse | where {$_.psiscontainer -eq $true}

foreach ($Folder in $Folders){
    $Owner = (get-acl $Folder.fullname).owner
    Foreach ($ACL in $Owner){
    $OutInfo = $Folder.Fullname + ";" + $owner
    Add-Content -Value $OutInfo -Path $OutFile
    }
}

【问题讨论】:

标签: powershell permissions acl powershell-3.0 directory


【解决方案1】:

您应该能够在每个级别的路径中添加一个“*”。例如,这应该返回 C:\Temp: 下三层深处的项目:

dir c:\temp\*\*\*

这是您可以使用的示例函数(它是为 PowerShell v3 或更高版本编写的,但可以修改为适用于版本 2):

function Get-FolderOwner {
    param(
        [string] $Path = "."
    )

    Get-ChildItem $Path -Directory | ForEach-Object {
        # Get-Acl throws terminating errors, so we need to wrap it in
        # a ForEach-Object block; included -ErrorAction Stop out of habit
        try {
            $Owner = $_ | Get-Acl -ErrorAction Stop | select -exp Owner
        }
        catch {
            $Owner = "Error: {0}" -f $_.Exception.Message
        }

        [PSCustomObject] @{
            Path = $_.FullName
            Owner = $Owner
        }
    }
}

那么你可以这样使用它:

Get-FolderOwner c:\temp\*\*\* | Export-Csv C:\temp\FolderOwner.csv

如果您的所有项目都达到并包括 3 级深度,您可以像这样修改函数:

function Get-FolderOwner {
    param(
        [string] $Path = ".",
        [int] $RecurseDepth = 1
    )

    $RecurseDepth--

    Get-ChildItem $Path -Directory | ForEach-Object {
        # Get-Acl throws terminating errors, so we need to wrap it in
        # a ForEach-Object block; included -ErrorAction Stop out of habit
        try {
            $Owner = $_ | Get-Acl -ErrorAction Stop | select -exp Owner
        }
        catch {
            $Owner = "Error: {0}" -f $_.Exception.Message
        }

        [PSCustomObject] @{
            Path = $_.FullName
            Owner = $Owner
        }

        if ($RecurseDepth -gt 0) {
            Get-FolderOwner -Path $_.FullName -RecurseDepth $RecurseDepth
        }
    }
}

并像这样使用它:

Get-FolderOwner c:\temp -RecurseDepth 3 | Export-Csv C:\temp\FolderOwner.csv

【讨论】:

  • 感谢您的回复。我试过这个,它几乎就在那里,但发现它只返回指定级别深度的文件夹的信息。 IE。 G:\*\*\* 将返回 G:\Folder1\Folder2\Folder3 的文件夹所有者,但不会返回父文件夹 G:\Folder1\Folder2 或 G:\Folder1。我想要 1、2 和 3 级的文件夹所有者,而不仅仅是 3 级!
  • 对不起,我误解了那部分。看看答案的补充是否有效。
  • 完美!奇迹般有效!我稍微修改了一下,所以你可以在开始时将路径、输出和递归指定为变量。 Get-FolderOwner $SharePath -RecurseDepth $RecurseLevel | Export-Csv $OutFile 这样我可以给我的团队并告诉他们只更新这些变量而不触及其他任何东西!
【解决方案2】:

有什么帮助吗?

resolve-path $RootPath\*\* |
 where { (Get-Item $_).PSIsContainer } -PipelineVariable Path |
 Get-Acl |
 Select @{l='Folder';e={$Path}},Owner

【讨论】:

  • 试图查看将其添加到我的代码的位置。我已经替换了 "$RootPath = G:\" 行下面的所有内容,但出现错误:A parameter cannot be found that matches parameter name 'PipelineVariable'.
  • 我很抱歉。 -PipeLineVariable 直到 V4 才引入。
猜你喜欢
  • 1970-01-01
  • 2017-12-30
  • 2017-09-02
  • 2018-04-02
  • 1970-01-01
  • 2016-11-11
  • 1970-01-01
  • 1970-01-01
  • 2011-09-02
相关资源
最近更新 更多