今天需要給某個網絡共享的大文件重新配置一個權限。這個文件夾下面有很多亂七八糟的小文件,很多創建人甚至已經離開公司了。如果一個個地目錄手動修改所有者權限,再打開繼承關系,這樣比較麻煩,這個時候自然是用腳本比較方便了。
#網上找的現成的高級方法來enable繼承關系
function Set-NTFSInheritance {
<#
.SYNOPSIS
Enable or Disable the NTFS permissions inheritance.
.DESCRIPTION
Enable or Disable the NTFS permissions inheritance on files and/or folders.
.EXAMPLE
$Folders = Get-Childitem -Path 'e:\homedirs' | Where-Object {$_.Attributes -eq 'Directory'}
$Folders | foreach {
$_ | Set-NTFSInheritance -Enable
}
.NOTES
Author : Jeff Wouters
Date : 8th of May 2014
#>
[cmdletbinding(defaultparametersetname='Enable')]
param (
[parameter(mandatory=$true,position=0,valuefrompipeline=$true,parametersetname='Enable')]
[parameter(mandatory=$true,position=0,valuefrompipeline=$true,parametersetname='Disable')]
$Path,
[parameter(mandatory=$false,parametersetname='Enable')][switch]$Enable,
[parameter(mandatory=$false,parametersetname='Disable')][switch]$Disable
)
begin {
} process {
$ACL = get-acl $_.FullName
switch ($PSCmdlet.ParameterSetName) {
'Enable' {
$ACL.SetAcce***uleProtection($false,$false)
}
'Disable' {
$ACL.SetAcce***uleProtection($true,$true)
}
}
try {
$ACL | Set-Acl -Passthru
} catch {
$_.Exception
}
} end {
}
}
#自己調用一下上面的方法,基本上就是三步走,第一個奪取所有權;第二打開繼承關系;第三在最上面設置權限
function ChangePermission {
[cmdletbinding(defaultparametersetname='Enable')]
param (
[Parameter(Mandatory=$true)]
[string]
$path,
[Parameter(Mandatory=$true)]
[string]
$group
)
#Step 1: take over ownership
takeown.exe /f $path /r /d Y
#Step 2: enable inheritance for all subfolders
$Folders = Get-Childitem -Path $path -Recurse
$Folders | foreach {
$_ | Set-NTFSInheritance -Enable
}
#Step3: setup NTFS Modify permission from the parent folder
$perm2=':(OI)(CI)(M)'
write-host $path -ForegroundColor Cyan
icacls $path /grant "$($group)$perm2"
}
#最后調用函數即可
$parent="\\syd02\Creative TRACK\CLIENT FOLDERS\WESTPAC"
Get-ChildItem $parent | foreach {
$_.fullname
ChangePermission -path $_.FullName -group "Sydney Track Creative"
}免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。