[英]Powershell: Apply owner to multiple files and folders per username
我們有一台服務器為所有用戶提供“我的文檔”文件夾。 一些文件夾所有者已更改為管理員。 我正在嘗試設計一個PowerShell腳本,該腳本將轉到每個用戶的“我的文檔”文件夾的根目錄,並將該用戶用作其中所有子文件夾和文件的所有者。 這可能嗎?
我從以前的腳本中獲得了以下內容,該腳本試圖將用戶設置為每個我的文檔根文件夾的完全權限:
$FolderPath = "E:\mydocuredir\"
$MyDocsMain = Get-ChildItem -Path $FolderPath -Directory
Get-ChildItem -Path $FolderPath -Directory | ForEach-Object{
$HomeFolders = Get-ChildItem $FolderPath $_.Name -Directory
$Path = $HomeFolders.FullName
$Acl = (Get-Item $Path).GetAccessControl('Access')
$Username = $_.Name
$Ar = New-Object System.Security.AccessControl.FileSystemAccessRule($Username, 'FullControl', 'ObjectInherit', 'InheritOnly', 'Allow')
$Acl.SetAccessRule($Ar)
Set-Acl -path $Path -AclObject $Acl
}
首先,確保重定向文件夾的共享和根文件夾權限遵循最佳實踐 。
我將使用NTFSSecurity PS模塊( 有關其用法的博客 )。 該模塊具有易於理解的命令,因為它們遵循的方式與您通過GUI設置權限的方式相同。
$FolderPath = "E:\mydocuredir"
Get-ChildItem -Path $FolderPath -Directory | ForEach-Object{
Add-NTFSAccess -Path $_.FullName -Account "domain\$($_.Name)" -AccessRights FullControl -AppliesTo ThisFolderSubfoldersAndFiles
}
要設置所有者,請將Add-NTFSAccess
命令替換為:
Set-NTFSOwner -Path $_.FullName -Account "domain\$($_.Name)"
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.