此脚本获取文件夹的名称并使其成为文件夹的所有者。但它并没有给所有者完全访问权限。
$folder = "\\profileshare\profiles\"
$users = (Get-Childitem $folder)
foreach ($user in $users) {
$acl = Get-Acl $folder\$user
$current_owner = $acl.owner.split("\")
if ($user.Name.tolower() -ne ([string]$current_owner[1]).tolower()) {
Write-Host $user " folder needs owner changed, set to "$user " from " $acl.owner
$acl.SetOwner([System.Security.Principal.NTAccount]"$user")
Set-Acl $folder\$user $acl
} else {
Write-Host "Folder for $user set correctly"
}
}
有人可以帮助我让所有者完全访问吗?
答案 0 :(得分:1)
要让所有者自动获得完全访问权限,您需要在父文件夹(\\profileshare\profiles
)上设置ACE,授予对子文件夹和文件CREATOR OWNER
的完全控制权。
$folder = '\\profileshare\profiles'
$acl = Get-Acl -Path $folder
$ace = New-Object Security.AccessControl.FileSystemAccessRule(
'CREATOR OWNER',
'FullControl',
'ObjectInherit,ContainerInherit',
'InheritOnly',
'Allow'
)
$acl.AddAccessRule($ace)
Set-Acl -Path $folder -AclObject $acl
如果父文件夹中没有此设置,则需要手动撤消先前所有者的权限并授予对新所有者的访问权限。
答案 1 :(得分:0)
你找到了答案吗?我上周五发布了这个相同的代码给我非常肯定的人,它会完成你正在寻找的东西。这将为给定用户构造一个ACL以授予他们FullControl,并使子文件和文件夹也继承该访问权。
您只需设置用户对象ACE,并将ACE添加到循环内的ACL中,其他所有内容都可以在循环之前定义一次。
$Rights = [System.Security.AccessControl.FileSystemRights]"FullControl"
$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]"ObjectInherit,ContainerInherit"
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]::None
$objType =[System.Security.AccessControl.AccessControlType]::Allow
$folder = "\\profileshare\profiles\"
$users = (get-childitem $folder)
Foreach ($user in $users)
{
$acl = Get-Acl $folder\$user
$current_owner = $acl.owner.split("\")
if($user.Name.tolower() -ne ([string]$current_owner[1]).tolower())
{
write-host $user " folder needs owner changed, set to "$user " from " $acl.owner
$objUser = New-Object System.Security.Principal.NTAccount("IIS_IUSRS")
$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule($objUser, $Rights, $InheritanceFlag, $PropagationFlag, $objType)
$ACL.AddAccessRule($objACE)
$acl.SetOwner($objUser)
set-acl $folder\$user $acl
}
else
{
write-host "Folder for $user set correctly"
}
}