温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

POwershell 更改文件权限

发布时间:2020-07-11 18:13:16 来源:网络 阅读:8269 作者:beanxyz 栏目:开发技术

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

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



向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI