PowerShell:删除特定用户对文件夹的所有权限

6
我需要一个脚本或简单的PowerShell代码,以便为特定用户从文件夹中删除所有权限,并将这些删除应用到所有子文件夹和文件 - 递归地... 谢谢!
3个回答

10
 $acl=get-acl c:\temp
 $accessrule = New-Object system.security.AccessControl.FileSystemAccessRule("domain\user","Read",,,"Allow")
 $acl.RemoveAccessRuleAll($accessrule)
 Set-Acl -Path "c:\temp" -AclObject $acl

这应该递归地清除c:\temp中用户的所有安全规则。


嗨Kayasax,再次感谢您的支持。它确实帮助了我最后的代码,但是没有递归删除?!您能否再次检查一下..提前致谢! - gaponte69
1
真的吗?这在我的工作站上有效了... 你可以尝试 ls c:\temp -recurse |set-acl -aclObject $acl - Loïc MICHEL
1
你的意思是像这样尝试吗: $acl=get-acl c:\temp $accessrule = New-Object system.security.AccessControl.FileSystemAccessRule("domain\user","Read",,,"Allow") $acl.RemoveAccessRuleAll($accessrule) ls C:\temp -recurse | set-acl -aclObject $acl - gaponte69
1
@AdityaBokade 在设置 ACL 之前尝试获取所有权(使用 takeown.exe)。 - Loïc MICHEL
@LoïcMICHEL 求助。我创建了一个 PowerShell 脚本,在本地 IIS 上创建 Web 应用程序。在 TFS 构建中,我创建了运行该脚本的步骤。构建时它显示脚本已成功执行,但实际上并没有。可能是什么原因呢? http://stackoverflow.com/questions/43384496/deploy-asp-net-web-app-on-local-or-remote-iis-using-powershell-dsc - Aditya Bokade
显示剩余3条评论

2

我认为更简单的方法是从具有正确权限的文件或文件夹复制访问控制列表(ACL),并将其应用于您想要特定访问权限的文件夹。 例如:

$acl= get-acl /path/to/file_with_correct acl 
$files = get-childItem c:\temp\*.* -recurce | set-acl -aclobject $acl -whatif

移除 -whatif 参数以有效地修改 ACL。

或者按照 这篇 Technet 文章 的指引,使用以下代码:

$Right = [System.Security.AccessControl.FileSystemRights]::Read
$InheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::None 
$PropagationFlag = [System.Security.AccessControl.PropagationFlags]::InheritOnly  
$objType = [System.Security.AccessControl.AccessControlType]::Allow 

$objUser = New-Object System.Security.Principal.NTAccount("domain\bob") 
$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule `
    ($objUser, $Right, $InheritanceFlag, $PropagationFlag, $objType) 
$objACL = Get-ACL "d:\test" 
$objACL.RemoveAccessRuleAll($objACE) 
Set-ACL "d:\test" -AclObject $objACL

谢谢Kayasax的回复,但是您能否举个例子呢?比如说,如果我需要从D:\Test路径中删除用户Bob的所有权限,该怎么做? - gaponte69
$acl = get-acl D:\Test $files = get-childItem $acl -recurse | set-acl -aclobject $acl -whatif我在哪里指定用户对象?我应该在哪里写上用户Bob,以及在哪里定义我要删除权限? - gaponte69
嗨,Kayasax,我在执行这个ps1时遇到了以下错误: - gaponte69
New-Object:找不到“FileSystemAccessRule”的重载和参数计数:“5”。 位于第7行字符21处:
  • $objACE = New-Object <<<< System.Security.AccessControl.FileSystemAccessRule `
    • CategoryInfo : InvalidOperation: (:) [New-Object], MethodException
    • FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
- gaponte69
$objACE = New-Object System.Security.AccessControl.FileSystemAccessRule($objUser, $Right, $InheritanceFlag, $PropagationFlag, $objType) - Loïc MICHEL
显示剩余2条评论

0
我创建的这个函数将在目标机器上调用脚本块,以删除用户的权限。
function Remove-OGRemoteACL (){
<#
.SYNOPSIS
Invoke a script block on a target to remove ACL permissions

.DESCRIPTION
Invoke a script block on a target to change ACL permissions to remove the crazy delay GET-ACL can encounter.

.PARAMETER serverFQDN
the server that the script block is run on

.PARAMETER remotePath
the UNC path of the share to remove the permisions for the user from.

.PARAMETER userName
the user name of the domain user.

.EXAMPLE
Remove-OGRemoteACL -serverFQDN "bigserver.awesomedomain.net" -remotePath "\\bigserver.awesomedomain.net\my\amazingShare" -userName "awesomedomain\myuser"

.NOTES
    Name:       Remove-OGRemoteACL
    Author:     Richie Schuster - SCCMOG.com
    GitHub:     https://github.com/SCCMOG/PS.SCCMOG.TOOLS
    Website:    https://www.sccmog.com
    Contact:    @RichieJSY
    Created:    2023-03-14
    Updated:    -

    Version history:
    1.0.0 - 2023-03-14 Function Created
#>
[cmdletbinding()]
param (
    [parameter(Mandatory=$True,ValueFromPipeline=$true,Position=0)]
    [string]$serverFQDN,
    [parameter(Mandatory=$True,ValueFromPipeline=$true,Position=1)]
    [string]$remotePath,
    [parameter(Mandatory=$True,ValueFromPipeline=$true,Position=2)]
    [string]$userName
)
try{
    Write-Verbose "Invoking ACL removal commmand [Server: $($serverFQDN)] [userName: $($userName)] [Server: $($remotePath)]"
    Invoke-Command -ComputerName "$($serverFQDN)" -ScriptBlock { 
        $acl = Get-Acl $using:remotePath
        $usersid = New-Object System.Security.Principal.Ntaccount("$using:userName")
        $acl.PurgeAccessRules($usersid)
        $acl | Set-Acl $using:remotePath
    }
    Write-Verbose "Success invoking ACL removal commmand [Server: $($serverFQDN)] [userName: $($userName)] [Server: $($remotePath)]"
}
catch{
    Write-Error "Error - Failed invoking ACL removal commmand [Server: $($serverFQDN)] [userName: $($userName)] [Server: $($remotePath)]. Error: $($_.Exception.Message)"
}

}

例子:

Remove-OGRemoteACL -serverFQDN "bigserver.awesomedomain.net" -remotePath "\\bigserver.awesomedomain.net\my\amazingShare" -userName "awesomedomain\myuser" -Verbose

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接