POwershell更改文件权限-创新互联

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

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:域名申请网站空间、营销软件、网站建设、清水河网站维护、网站推广。
#网上找的现成的高级方法来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" 
}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


分享文章:POwershell更改文件权限-创新互联
浏览地址:http://pwwzsj.com/article/ddgodj.html