当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell - 递归设置文件权限

作者:精品下载站 日期:2024-12-14 22:02:35 浏览:13 分类:玩电脑

PowerShell - 递归设置文件权限


使用 PowerShell 中的 Set-Acl cmdlet 更改文件、文件夹或注册表项的安全描述符。它在多个文件或文件夹上递归地应用安全描述符。

ACL(访问控制列表)代表用户和用户组访问文件或注册表项的权限。它是访问控制条目 (ACE) 的列表。

在本文中,我们将讨论如何使用 Set-ACL cmdlet 递归设置文件或任何资源的权限。

使用 Set-ACL 将安全描述符应用于多个文件

使用 Get-ACL 命令获取文件的安全描述符,并使用 PowerShell 中的 Set-ACL 将安全描述符递归地应用到文件。

# Get the security descriptor for the file using Get-ACL
$newACL = Get-ACL D:\exported_ua.cer

# Use the Get-ChildItem to recusively to apply security descriptor using set-acl
Get-ChildItem -Path "D:\Certificates" -Recurse -Include "*.cer" -Force | Set-Acl -AclObject $newAcl

在上面的 PowerShell 脚本中,PowerShell 中的 Get-ACL cmdlet 获取安全描述符并将其存储在 $newACL 变量中。

Get-ChildItem 命令使用 Path 参数指定目录路径,并使用 Recurse 参数递归查找所有具有 .cer的子目录中的文件> 文件扩展名。

它将一个或多个检索到的文件传递给 Set-ACL cmdlet,该 cmdlet 将 AclObject 参数中的安全描述符递归地应用于所有文件。

酷提示:如何使用 PowerShell 解除阻止文件!

PowerShell 使用 Set-ACL 递归设置文件夹和文件的权限

使用Set-ACL cmdlet 可以递归地更改安全描述符或将安全描述符应用到文件夹和文件。

# Get the security descriptor for the folder
$newACL = Get-ACL -Path 'D:\Certificates\'

# Create the new rule
$fileSystemRule = New-Object System.Security.AccessControl.FileSystemAccessRule("BUILTIN\Administrators","FullControl","Allow")   

# Set the new rule
$newACL.SetAccessRule($fileSystemRule)

# Apply permission on folder recursively
Set-Acl -Path 'D:\Expiry_Certificates\' -AclObject $newACL 

在上面的 PowerShell 脚本中,Get-ACL cmdlet 获取文件夹的安全描述符并将其存储在 $newACL 中。

$fileSystemRule 变量包含“BUILTIN\Administrators”、“完全控制”和“允许”等权限规则。

使用 SetAccessRule() 方法将新的访问规则添加到现有的 $newACL 中。

Set-Acl 命令采用文件夹路径并递归地设置文件和文件夹的权限。

酷提示:如何在PowerShell中获取文件夹和子文件夹的权限!

结论

希望上面关于如何在PowerShell中使用Set-ACL cmdlet递归设置文件和文件夹权限的文章对您有所帮助。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯