POwershell Change file permissions

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.