PowerShell文件夹权限错误 - 无法转换部分或全部身份引用。

问题描述 投票:10回答:3

我已经阅读了很多关于此的帖子,但我仍然无法得到它。我正在运行此脚本作为管理员,它确实创建了所需的文件夹,只是没有设置适当的权限。任何帮助,将不胜感激。谢谢!

$Users = Get-Content "D:\New_Users.txt"
ForEach ($user in $users)
{
    $newPath = Join-Path "F:\Users" -childpath $user
    New-Item $newPath -type directory

    $UserObj = New-Object System.Security.Principal.NTAccount("DOMAIN",$user)

    $acl = Get-Acl $newpath
    $acl.SetAccessRuleProtection($True, $False)
    $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("O1OAK\$user","AppendData,CreateDirectories,CreateFiles,DeleteSubdirectoriesAndFiles,ExecuteFile,ListDirectory,Modify,Read,ReadAndExecute,ReadAttributes,ReadData,ReadExtendedAttributes,ReadPermissions,Synchronize,Traverse,Write,WriteAttributes,WriteData,WriteExtendedAttributes","ContainerInherit, ObjectInherit","None","Allow")
    $acl.SetAccessRule($accessRule)
    $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("NT AUTHORITY\SYSTEM","FullControl","ContainerInherit, ObjectInherit","None","Allow")
    $acl.SetAccessRule($accessRule)
    $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("BUILTIN\Administrators","FullControl","ContainerInherit, ObjectInherit","None","Allow")
    $acl.SetAccessRule($accessRule)
    $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("1OAK\$user","Delete","ContainerInherit, ObjectInherit","None","Allow")
    $acl.removeAccessRule($accessRule)
    $acl.SetOwner($UserObj)
    $acl | Set-Acl $newpath
}

我得到的3个字符串中的第一个错误如下。我认为这是最重要的,并将修复其他2。

Exception calling "SetAccessRule" with "1" argument(s): "Some or all identity references could not be translated."
At D:\DOMAIN\IT\IT Private\User Drives\user_folders.ps1:12 char:20
+     $acl.SetAccessRule <<<< ($accessRule)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

我希望这不是重复的,我很抱歉,如果是的话,我已经读了几个小时。谢谢!

windows powershell file-permissions
3个回答
19
投票

这个错误非常自我解释:Some or all identity references could not be translated.

这意味着无法找到该帐户。所以你要做的就是验证你的账户。由于您要添加4个ACE,因此您需要确定哪个无效。

最简单的方法是使用ISE或PowerGUI逐行调试。

我用“NT AUTHORITY \ SYSTEM”和“BUILTIN \ Administrators”尝试了你的代码,它的工作方式与"O1OAK\$user""1OAK\$user"有关。您的文本文件中可能包含无效帐户。


1
投票

具有用户ID的gotch是AD截断用户名,因此具有长名称“j_reallylongname”的用户将具有被截断的samid(安全帐户管理器(SAM)帐户名称)。 (j_reallylong)

因此,在获取用户名时,请确保在使用之前验证AD。

当我有了upns,所以我运行dsget查询来获取samid然后使用它来构建身份引用。


0
投票

添加这个以防任何C#/ ASP.NET开发人员得到这个(这是我的方案,我发现这篇文章)。

我在企业环境中使用.NET Core,我需要检查UserGroups作为安全性的一部分。代码就像(其中“user”是ClaimsPrincipal):

var windowsIdentity = user.Identity as WindowsIdentity;
if( windowsIdentity is null )
    throw new Exception( $"Invalid Windows Identity {user.Identity.Name}" );
return windowsIdentity.Groups
    .Select( g => g.Translate( typeof( NTAccount ) ).Value );

无论如何,负责组的人删除了我所属的组,并且AD复制延迟导致我在标题中得到错误。注销和/或重启工作得很好。

© www.soinside.com 2019 - 2024. All rights reserved.