【问题标题】:List of permissions for a folder and it's subfolder文件夹及其子文件夹的权限列表
【发布时间】:2014-05-08 05:16:22
【问题描述】:

我在 Windows 7 上使用 PowerShell。我有以下代码 sn-p 并想知道

为什么我没有将 SID 转换为友好的用户名(在域上)?

$OutFile = "I:\Permissions.csv"
$RootPath = "K:\FolderName"

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

foreach ($Folder in $Folders)
{
    $ACLs = get-acl $Folder.fullname | ForEach-Object { $_.Access  }
    Foreach ($ACL in $ACLs)
    {

        $objSID = New-Object System.Security.Principal.SecurityIdentifier($ACL.IdentityReference.Value) 
        #$objUser = $objSID.Translate([System.Security.Principal.NTAccount]) 
        $objUser = $objSID.Translate([System.Security.Principal.SecurityIdentifier])
        $objUser.Value


        #Show User
        Write-Host “`r`nThe user mapped to SID $($objSID) is $($objUser.value)`r`n” -f “Red”

        $OutInfo = $Folder.Fullname + "," + $objUser.Value  + "," + $ACL.AccessControlType + "," + $ACL.IsInherited + "," + $ACL.InheritanceFlags + "," + $ACL.PropagationFlags
        Add-Content -Value $OutInfo -Path $OutFile
    }
}

所需的输出将是 SAM 帐户名称。 (不是显示名称)

John.Smith1
John.Smith

【问题讨论】:

  • 请提供工作样本。 $ACL 从未被声明过。
  • 给你。你怎么看?
  • 更好,仍然缺少$Dname$outfile,但我创建了一个虚拟对象来解决这个问题。查看更新的答案。
  • $DName 是我根据以下答案尝试的。
  • .IdentityReference.Value 应该已经提供了一个“友好的用户名”。您为什么要尝试将其转换为 SID,然后再转换回用户名?实际和期望输出的示例会有所帮助。

标签: windows powershell windows-7 powershell-2.0


【解决方案1】:

您可以使用相当简单的 ADSI 查找来提取用户的专有名称。试试这个:

$DName = ([adsi]"LDAP://<SID=$($ACL.IdentityReference.value)>").distinguishedName

$DName 现在应该包含一个类似于 'CN=JSmith,OU=Users,DC=something,DC=com' 的字符串

要从中获取用户名,您可以将字符串拆分几次,因为它是 =, 分隔的:

$strUser = $dname.split("=")[1].split(",")[0]

【讨论】:

  • 如何获得CN部分?
【解决方案2】:

IdentityReferenceSecurityIdentifier-object 或 NTAccount-object,而不是作为字符串的 SID 值,这是 SecurityIdentifier 构造函数所需要的。如果需要以字符串形式访问SID,则需要访问$ACL.IdentityReference.Value

试试这个:

$RootPath = "K:\FolderName"
#Define $OutFile
#Define $Dname

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

foreach ($Folder in $Folders)
{
    $ACLs = get-acl $Folder.fullname

    $ACLs.Access | ForEach-Object { 
        $ACL = $_

        #IdentityReference may already be a SID- or a NTAccount-object. 
        #Get SID-object
        $objSID = $ACL.IdentityReference.Translate([System.Security.Principal.SecurityIdentifier])
        #Translate to NTAccount-object
        $objUser = $objSID.Translate([System.Security.Principal.NTAccount]) 

        #Show User
        Write-Host "`r`nThe user mapped to SID $($objSID) is $($objUser.value)`r`n" -f "Red"

        $OutInfo = $Folder.Fullname + "," + $DName.Value  + "," + $ACL.AccessControlType + "," + $ACL.IsInherited + "," + $ACL.InheritanceFlags + "," + $ACL.PropagationFlags
        Add-Content -Value $OutInfo -Path $OutFile
    }
}

【讨论】:

  • 你是不是建议我把它改成 $objSID = New-Object System.Security.Principal.SecurityIdentifier($ACL.IdentityReference.Value)
  • 是的,如果 $ACL 包含该属性。您还没有向我们展示您是如何获得$ACL-object 的。
  • 刚刚试过,没有任何变化。我的 objUser.Value 不是 AD 中的友好名称。在调试器中它是空的。
【解决方案3】:

将 SID 字符串转换为 NTAccount:

$exampleSidString = 'S-1-5-21-768745588-123456789-987654321-500'
$objSID = New-Object System.Security.Principal.SecurityIdentifier $exampleSidString
$objUser = $objSID.Translate( [System.Security.Principal.NTAccount] )
$objUser.Value

但是,如果您已经有一个身份参考(SecurityIdentifier 和 NTAccount 都是其子类),您可以直接使用 transate 函数:

$objUser = $ACL.IdentityReference.Translate( [System.Security.Principal.NTAccount] )
$objUser.Value

查看this link 了解更多信息

【讨论】:

  • 上帝没有。这只是一个示例 SID。他想在 ACL ($ACL.IdentityReference) 中翻译 SID。
  • 转到blogs.technet.com 了解更多信息,还有更多来自实际 Microsoft 技术人员的示例
  • 我已经尝试过了,但我遇到了异常。像 >
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-13
  • 1970-01-01
  • 1970-01-01
  • 2019-06-19
  • 1970-01-01
  • 1970-01-01
  • 2019-01-02
相关资源
最近更新 更多