我以管理员身份运行此脚本,它确实创建了所需的文件夹,只是没有设置适当的权限。
$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 AUTHORITYSYSTEM","FullControl","ContainerInherit, ObjectInherit","None","Allow")
$acl.SetAccessRule($accessRule)
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule("BUILTINAdministrators","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:DOMAINITIT PrivateUser Drivesuser_folders.ps1:12 char:20
+ $acl.SetAccessRule <<<< ($accessRule)
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : DotNetMethodException
这个错误不言自明:Some or all identity references could not be translated.
这意味着找不到该帐户。所以你要做的就是验证你的账户。由于您添加了4个ACE,因此需要确定哪个是无效的。
最简单的方法是使用ISE或PowerGUI逐行调试。
我用"NT AUTHORITY\SYSTEM"one_answers"BUILTIN\Administrators"尝试了您的代码,它可以工作,所以问题出在"O1OAK$user"
或"1OAK$user"
上。您的文本文件中可能有一个无效帐户。
用户ID的一个问题是AD截断用户名,因此具有长名称"j_re烯丙基名称"的用户将具有一个被截断的samid(安全帐户管理器(SAM)帐户名)。(j_re烯丙基)
因此,在获取用户名时,请确保在使用之前根据AD进行验证。
当我得到upns时,我运行一个dsget查询来获得samid,然后使用它来构建身份引用。
添加它以防任何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复制滞后导致我在标题中出现错误。注销和/或重新启动运行正常。
对我来说,这是一个我使用$user = Get-Credential "username"
验证脚本执行是否知道密码的例子。我不得不把我的$user
变成$user.UserName
给脚本参数他们期望的值