Today you need to reconfigure a permission for a large file that is shared by a network. There are a lot of small, messy files under this folder, and many creators have even left the company. If the directory manually modify the owner permissions, and then open the inheritance relationship, this is more troublesome, this time is naturally more convenient to use the script.
#网上找的现成的高级方法来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. Setaccessruleprotection ($false, $false) } ' Disable ' { $ACL. Setaccessruleprotection ($true, $true) } } try { $ACL | Set-Acl -Passthru } catch { $ _. exception } } end { }} #自己调用一下上面的方法, basically three steps, the first to seize ownership; the second opens the inheritance relationship; The third one is set permissions 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 $perm 2= ':(OI) (CI) (M) ' write-host $path -ForegroundColor Cyan icacls $path / grant "$ ($group) $perm 2"} #最后调用函数即可 $parent = "\\syd02\creative track\client folders\westpac" get-childitem $parent | foreach {$_.fullnamechangepermission -path $_. fullname -group "SYDNEY&NBSP;TRACK&NBsp Creative "&NBSP;}
This article is from the "Mapo Tofu" blog, please be sure to keep this source http://beanxyz.blog.51cto.com/5570417/1901607
POwershell Change file permissions