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

时间:2012-07-11 19:36:29

标签: windows powershell file-permissions

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

$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

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

4 个答案:

答案 0 :(得分:18)

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

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

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

我使用“NT AUTHORITY \ SYSTEM”和“BUILTIN \ Administrators”尝试了您的代码,它的工作正常,问题出在"O1OAK\$user""1OAK\$user"。您的文本文件中可能包含无效帐户。

答案 1 :(得分:1)

带有用户ID的gotch是AD截断用户名,因此具有长名称的用户&#34; j_reallylongname&#34;将有一个被截断的samid(安全帐户管理器(SAM)帐户名称)。 (j_reallylong)

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

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

答案 2 :(得分:0)

添加此代码以防任何C#/ ASP.NET开发人员收到此消息(这是我的情况,我找到了这篇文章)。

我在公司环境中使用.NET Core,作为安全性的一部分,我需要检查UserGroups。代码类似于(其中“用户”是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复制滞后导致我得到标题错误。注销和/或重新启动都可以。

答案 3 :(得分:0)

对我来说,这是我使用$user = Get-Credential "username"验证脚本执行是否知道密码的一种情况。我必须将$user转换为$user.UserName才能为脚本参数提供期望的值

相关问题