您现在的位置是:首页 > 文章详情

POwershell 更改文件权限

日期:2017-02-27点击:663

今天需要给某个网络共享的大文件重新配置一个权限。这个文件夹下面有很多乱七八糟的小文件,很多创建人甚至已经离开公司了。如果一个个地目录手动修改所有者权限,再打开继承关系,这样比较麻烦,这个时候自然是用脚本比较方便了。

#网上找的现成的高级方法来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"  }



原文链接:https://blog.51cto.com/beanxyz/1901607
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章