需要一点帮助。脚本根本不更改权限。
我想使文件夹的名称成为拥有完全权限的文件夹的所有者。
$foldernames = (Get-ChildItem \\knesmbmdc001\profiles).Name
$user = "$foldernames"
$usercount = 0
foreach($name in $foldernames)
{
If ($usercount -le 5)
{
Try
{
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule($user,"FullControl", "ContainerInherit, ObjectInherit", "None", "Allow")
$acl = get-acl $name
$acl.AddAccessRule($rule)
Set-acl $name $acl
}
catch
{
Add-Content C:\user_done.txt $name
}
}
} 发布于 2016-02-06 03:40:25
脚本当前的问题是:
New-Object System.Security.AccessControl.FileSystemAccessRule的第一个参数应该是$name,而不是$user。Add-Content调用不应该在catch子句中,因为只有当catch操作没有成功时,它才会进行日志记录。catch子句中没有其他语句,异常就会被有效地忽略。Get-Acl,Set-Acl)的cmdlet,只有在当前位置恰好是文件夹的父位置时才能工作。下面是您的脚本的重新设计,它应该按照预期工作:
$dir = '\\knesmbmdc001\profiles'
$logFile = 'C:\user_done.txt'
Get-ChildItem -Directory $dir | % {
$user = $_.Name
$acl = Get-Acl -LiteralPath $_
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule $user, FullControl, "ContainerInherit, ObjectInherit", None, Allow
$acl.AddAccessRule($rule)
Set-Acl -LiteralPath $_ -AclObject $acl
Add-Content $logFile $user
}但是,请注意,虽然此将向目标用户提供完全控制,但并不使他们成为文件夹的所有者。
要实际更改所有权,请尝试以下操作(替换$rule=...和$acl.AddAccessRule...命令):
$userIdentity = New-Object System.Security.Principal.NTAccount $user
$acl.SetOwner($userIdentity)这对我在使用提升的权限运行时对本地用户帐户起作用,但是YMMV。
https://stackoverflow.com/questions/35236816
复制相似问题