17

我以管理员身份运行此脚本,它确实创建了所需的文件夹,只是没有设置适当的权限。

$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

4 回答 4

28

该错误非常不言自明:Some or all identity references could not be translated.

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

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

我用“NT AUTHORITY\SYSTEM”和“BUILTIN\Administrators”尝试了你的代码,它可以工作,所以问题出在"O1OAK\$user"or"1OAK\$user"上。您的文本文件中可能有一个无效帐户。

于 2012-07-12T08:35:16.507 回答
1

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

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

当我得到 upns 后,我运行 dsget 查询来获取 samid,然后使用它来构建身份参考。

于 2014-11-18T03:12:09.887 回答
1

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

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

于 2019-03-05T15:50:48.723 回答
0

对我来说,这是我验证脚本执行是否知道密码的情况$user = Get-Credential "username"。我不得不把我的$user变成$user.UserName给脚本参数他们期望的值

于 2019-09-06T09:43:09.403 回答