Windows 文件夹及其子文件夹的权限列表';s子文件夹

Windows 文件夹及其子文件夹的权限列表';s子文件夹,windows,powershell,windows-7,powershell-2.0,Windows,Powershell,Windows 7,Powershell 2.0,我正在Windows 7上使用PowerShell。我有下面的代码片段,我想知道 为什么我不能将SID转换为友好的用户名(在域上) 所需的输出将是SAM帐户名。(不是显示名称) 要将SID字符串转换为NTAccount,请执行以下操作: $exampleSidString = 'S-1-5-21-768745588-123456789-987654321-500' $objSID = New-Object System.Security.Principal.SecurityIdentifier

我正在Windows 7上使用PowerShell。我有下面的代码片段,我想知道

为什么我不能将SID转换为友好的用户名(在域上)

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


要将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

查看以了解更多信息

IdentityReference
是一个
SecurityIdentifier
-对象或
NTAccount
-对象,而不是作为字符串的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
    }
}

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

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

上帝不,这只是一个例子。他想转换ACL(
$ACL.IdentityReference
)中的SID。有关更多信息,请转到,这里有更多来自实际Microsoft技术的示例。我尝试过这一点,但遇到了异常。您是否建议我将其更改为$objSID=New Object System.Security.Principal.SecurityIdentifier($ACL.IdentityReference.Value)是,如果
$ACL
包含该属性。您没有向我们展示如何获得
$ACL
-object。只是尝试了一下,没有做任何更改。我的objUser.Value不是广告中的友好名称。它在调试器中是空的。如何获取CN部分?请提供一个工作示例<代码>$ACL从未声明过。给你。你觉得怎么样?更好的是,仍然缺少
$Dname
$outfile
,但我创建了一个虚拟对象来绕过它。请参阅更新的答案。$DName是我根据下面的答案尝试的东西。
.IdentityReference.Value
应该已经提供了一个“友好的用户名”。为什么要将其转换为SID,然后再转换回用户名?举一个实际和期望输出的例子会很有帮助。
$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
    }
}
$DName = ([adsi]"LDAP://<SID=$($ACL.IdentityReference.value)>").distinguishedName
$strUser = $dname.split("=")[1].split(",")[0]