【问题标题】:PowerShell Get-ACL with SamAccountName values具有 SamAccountName 值的 PowerShell Get-ACL
【发布时间】:2019-03-29 10:31:13
【问题描述】:

我正在尝试使用 Powershell 收集对 csv 文件的文件夹权限。我的问题是我需要包含 SamAccountName 和 FileSystemRights 的结果。

我尝试了两种不同的方法。我想出的第一个方法是一个简单的方法,它给了我 IdentityReference 和 FileSystemRights,但我找不到任何可以从 IdentityReference 获取 SamAccountName 的工作方法。 我在互联网上找到的第二个要复杂得多。它收集了所有有权访问该文件夹的帐户,但它没有显示 FileSystemRights,我不知道如何更改它。

我自己的解决方案

(Get-Acl "FolderPath").Access | Select-Object IdentityReference, FileSystemRights

我找到的解决方案

Get-Acl $UncPath | Select-Object -ExpandProperty Access | Where-Object { (-not $_.IsInherited) -and ('NT AUTHORITY\SYSTEM','BUILTIN\Administrators','CREATOR OWNER' -notcontains $_.IdentityReference) } | Select-Object -ExpandProperty IdentityReference | ForEach-Object { $_.Translate('System.Security.Principal.SecurityIdentifier').Value } | Get-ADGroup -ErrorAction SilentlyContinue | get-adgroupmember | select-object SamAccountName | Format-Table | Out-String

是否有任何工作方法可以让我看到 SamAccountName 和 FileSystemRights 的结果?

提前谢谢你。

【问题讨论】:

  • 查看通用参数-PipelineVariablethis mcpmag article 和/或阅读Get-Help about_CommonParameters
  • 我阅读了这篇文章,并承认我是 PowerShell 的新手,但我看不出这如何帮助我解决这个特殊问题。
  • 使用多个步骤进行管道化和扩展属性会让您无法访问以前的属性,本文展示了如何使用 -PipelineVariable 来规避此问题。
  • 嗯,对我来说,第一种方法可以将 IdentityReference 转换为 SamAccountName 就足够了。我只尝试了那个长流水线代码,因为我无法让第一个方法按我的意愿工作。

标签: powershell


【解决方案1】:
$UncPath = 'E:\temp\test'

$all = Get-Acl $UncPath |
            Select -ExpandProperty Access |
            Where-Object { (-not $_.IsInherited) -and ('NT AUTHORITY\SYSTEM','BUILTIN\Administrators','CREATOR OWNER' -notcontains $_.IdentityReference) } |
            Select-Object @{ Name = 'Identity'; Expression = { $_.IdentityReference -replace "\w+\\(.+)", '$1' } }, FileSystemRights

# Here you can get Users ACL
$distinct_users = $all | 
            Select-Object Identity, @{ Name = 'sAMAccountName'; Expression = { (Get-ADUser -Identity $_.Identity -ErrorAction SilentlyContinue).sAMAccountName }}, FileSystemRights |
            Where-Object sAMAccountName -ne $null
# Here we will expand group acls
 $groups = $all | 
            Select-Object Identity, @{ Name = 'sAMAccountName'; Expression = { (Get-ADGroup -Identity $_.Identity -ErrorAction SilentlyContinue).sAMAccountName }}, FileSystemRights |
            Where-Object sAMAccountName -ne $null            
# now we will get groups membership
$group_users = @()
Foreach($group in $groups){
    Get-ADGroupMember -Identity $group.Identity | ForEach-Object { $group_users += [PSCustomObject]@{ 
                                                                                        'Identity' = $group.Identity
                                                                                        'sAMAccountName' = $_.sAMAccountName
                                                                                        'FileSystemRights' = $group.FileSystemRights
                                                                                    } }

}
                 
$everyone = $distinct_users + $group_users
$everyone | Export-Csv -Path D:\example.csv

检查 $everyone 变量,它将包含 3 列:ACL 中的身份、sAMAccountName 和文件系统权限。

【讨论】:

  • 非常感谢。它比我想要的要多得多:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-23
  • 1970-01-01
  • 1970-01-01
  • 2020-05-23
相关资源
最近更新 更多