本文目录导读:

Windows PowerShell 脚本
批量修改文件夹权限(ICACLS)
# 批量修改文件夹权限脚本
# 使用 icacls 命令
# 配置参数
$targetFolder = "C:\Your\Target\Folder"
$newPermission = "Everyone:F" # 完全控制
$logFile = "C:\Logs\permission_changes.log"
# 创建日志文件
Start-Transcript -Path $logFile
# 获取所有子文件夹
$folders = Get-ChildItem -Path $targetFolder -Recurse -Directory
foreach ($folder in $folders) {
try {
# 修改文件夹权限
icacls $folder.FullName /grant "${newPermission}" /t
Write-Host "已修改权限: $($folder.FullName)" -ForegroundColor Green
}
catch {
Write-Host "错误: 无法修改 $($folder.FullName): $_" -ForegroundColor Red
}
}
Stop-Transcript
基于CSV文件的批量权限设置
# 使用CSV配置文件进行批量权限设置
# CSV文件格式:
# Path,UserOrGroup,Permission,Recurse
# C:\Folder1,Everyone,Read,Y
# C:\Folder2,Domain\Users,Modify,N
$csvPath = "C:\permissions.csv"
# 读取CSV文件
$permissions = Import-Csv -Path $csvPath
foreach ($perm in $permissions) {
$path = $perm.Path
$user = $perm.UserOrGroup
$permission = $perm.Permission
$recurse = [bool]($perm.Recurse -eq "Y")
try {
# 设置权限
$arguments = @(
$path
"/grant"
"${user}:${permission}"
)
if ($recurse) {
$arguments += "/t"
}
& icacls $arguments
Write-Host "成功: $path" -ForegroundColor Green
}
catch {
Write-Host "失败: $path - $_" -ForegroundColor Red
}
}
使用.NET类的高级权限管理
# 使用.NET类进行更精细的权限控制
$basePath = "C:\Your\Target\Path"
$accessRule = "Everyone"
$permission = [System.Security.AccessControl.FileSystemRights]::FullControl
$inheritanceFlag = [System.Security.AccessControl.InheritanceFlags]::ContainerInherit
$propagationFlag = [System.Security.AccessControl.PropagationFlags]::None
$accessControlType = [System.Security.AccessControl.AccessControlType]::Allow
function Set-FolderPermission {
param(
[string]$FolderPath,
[string]$Account,
[System.Security.AccessControl.FileSystemRights]$Rights,
[System.Security.AccessControl.AccessControlType]$Type
)
try {
# 获取当前权限
$acl = Get-Acl -Path $FolderPath
# 创建新的权限规则
$accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule(
$Account,
$Rights,
$inheritanceFlag,
$propagationFlag,
$Type
)
# 添加规则
$acl.AddAccessRule($accessRule)
# 应用权限
Set-Acl -Path $FolderPath -AclObject $acl
return $true
}
catch {
Write-Host "设置权限失败: $FolderPath - $_" -ForegroundColor Red
return $false
}
}
# 批量处理
$folders = Get-ChildItem -Path $basePath -Directory -Recurse
foreach ($folder in $folders) {
$result = Set-FolderPermission -FolderPath $folder.FullName `
-Account $accessRule `
-Rights $permission `
-Type $accessControlType
if ($result) {
Write-Host "已设置: $($folder.FullName)" -ForegroundColor Green
}
}
Linux Bash 脚本
批量修改文件和目录权限
#!/bin/bash
# 批量权限修改脚本
# 为所有目录设置755权限,文件设置644权限
TARGET_DIR="/path/to/target"
LOG_FILE="/var/log/permission_changes.log"
# 日志函数
log_message() {
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
}
# 检查目标目录是否存在
if [ ! -d "$TARGET_DIR" ]; then
log_message "错误: 目录 $TARGET_DIR 不存在"
exit 1
fi
# 查找并修改目录权限
log_message "开始修改目录权限为 755..."
find "$TARGET_DIR" -type d -exec chmod 755 {} \; -exec echo "修改目录权限: {}" \;
# 查找并修改文件权限
log_message "开始修改文件权限为 644..."
find "$TARGET_DIR" -type f -exec chmod 644 {} \; -exec echo "修改文件权限: {}" \;
log_message "权限修改完成"
基于模式的批量权限设置
#!/bin/bash
# 根据不同文件类型设置不同权限
TARGET_DIR="/path/to/target"
# 设置不同文件类型的权限
set_permissions_by_type() {
# 设置目录权限
find "$TARGET_DIR" -type d -exec chmod 755 {} \;
# 设置常规文件权限
find "$TARGET_DIR" -type f -exec chmod 644 {} \;
# 设置脚本文件权限
find "$TARGET_DIR" -type f \( -name "*.sh" -o -name "*.py" -o -name "*.pl" \) -exec chmod 755 {} \;
# 设置配置文件权限(只读)
find "$TARGET_DIR" -type f \( -name "*.conf" -o -name "*.config" -o -name "*.ini" \) -exec chmod 644 {} \;
# 设置密钥文件权限(严格限制)
find "$TARGET_DIR" -type f \( -name "*.pem" -o -name "*.key" -o -name "*.p12" \) -exec chmod 600 {} \;
}
# 执行权限设置
set_permissions_by_type
echo "权限设置完成"
交互式权限管理脚本
#!/bin/bash
# 交互式权限管理脚本
show_menu() {
clear
echo "===== 批量权限管理工具 ====="
echo "1. 设置所有目录为 755"
echo "2. 设置所有文件为 644"
echo "3. 设置脚本文件为 755"
echo "4. 设置指定用户/组权限"
echo "5. 批量更改所有者"
echo "0. 退出"
echo "============================"
}
set_recursive_permission() {
local target=$1
local permission=$2
local type=$3 # "file" 或 "directory"
if [ "$type" = "directory" ]; then
find "$target" -type d -exec chmod "$permission" {} \;
echo "已将目录权限设为 $permission"
else
find "$target" -type f -exec chmod "$permission" {} \;
echo "已将文件权限设为 $permission"
fi
}
change_owner() {
read -p "输入用户: " user
read -p "输入组: " group
read -p "输入目标路径: " path
if [ -z "$group" ]; then
chown -R "$user" "$path"
else
chown -R "$user:$group" "$path"
fi
echo "所有者已更改"
}
# 主循环
while true; do
show_menu
read -p "请选择操作: " choice
case $choice in
1)
read -p "输入目标路径: " path
set_recursive_permission "$path" 755 "directory"
;;
2)
read -p "输入目标路径: " path
set_recursive_permission "$path" 644 "file"
;;
3)
read -p "输入目标路径: " path
find "$path" -type f \( -name "*.sh" -o -name "*.py" -o -name "*.pl" \) -exec chmod 755 {} \;
echo "脚本文件权限已设置"
;;
4)
read -p "输入文件路径: " file
read -p "输入用户: " user
read -p "输入权限(如 rwx): " perm
setfacl -m u:"$user":"$perm" "$file"
echo "ACL已设置"
;;
5)
change_owner
;;
0)
echo "退出"
exit 0
;;
*)
echo "无效选项"
;;
esac
read -p "按回车继续..."
done
使用说明
Windows 环境:
- 管理员权限:以管理员身份运行 PowerShell
- 测试环境:先在测试目录上试用
- 查看当前权限:
icacls "文件路径"查看当前权限 - 备份权限:建议先备份原有权限
Linux 环境:
- 备份:使用
getfacl备份权限:getfacl -R /path/to/directory > permissions_backup.txt
- 恢复:使用
setfacl恢复:setfacl --restore=permissions_backup.txt
安全注意事项
- 最小权限原则:只给予必要的权限
- 先测试:在非生产环境先测试
- 记录日志:记录所有更改
- 备份权限:更改前备份
- 使用脚本:避免手动修改大量文件
常见权限值
Windows:
F- 完全控制M- 修改RX- 读取和执行R- 读取W- 写入
Linux:
755- rwxr-xr-x (目录标准)644- rw-r--r-- (文件标准)600- rw------- (密钥文件)700- rwx------ (私有脚本)
选择适合你环境的脚本,并根据实际需求调整参数。