[PowerShell] اضافه کردن دسته ای مجوزهای مرتبط بر اساس محتوای متنی
![[PowerShell] اضافه کردن دسته ای مجوزهای مرتبط بر اساس محتوای متنی [PowerShell] اضافه کردن دسته ای مجوزهای مرتبط بر اساس محتوای متنی](https://nabfollower.com/blog/wp-content/uploads/2025/01/PowerShell-اضافه-کردن-دسته-ای-مجوزهای-مرتبط-بر-اساس-محتوای-780x470.png)
پس زمینه
مورد نیاز: برای افزودن دسته ای مجوزهای مرتبط بر اساس محتوای یک فایل متنی. نام واقعی پوشه های به اشتراک گذاشته شده اندکی با نام های ارائه شده در متن متفاوت است. با این حال، هدف افزودن دسته ای مجوز برای چنین پوشه هایی بر اساس متن است.
نتیجه مطلوب
ابتدا صفحه گسترده اکسل زیر را که در تصویر نشان داده شده است:
سپس، این محتوا را در یک فایل متنی کپی کنید.
دسته ای مجوزهای مرتبط را بر اساس محتوای متن اضافه کنید.
روش اجرا
محتوای متن با استفاده از فاصله، نقطه میانی، کاما یا کاراکترهای برگه به عنوان جداکننده تقسیم می شود. سپس، بر اساس تطبیق کلمات کلیدی، مانند “دسترسی” یا “ویرایش”، مجوزهای مختلفی بر این اساس اختصاص داده می شود.
کد منبع و URL مخزن:
# Define the main directory path
$baseFolderPath = "C:\SharedFolders" # Base path
$userFile = "C:\Users\Administrator\Desktop\PermissionAssignmentList.txt" # Replace with the actual path of the TXT file
# Read each line of the user file
Get-Content $userFile | ForEach-Object {
# Split the content of each line
# $parts = $_ -split '\s+'
$parts = $_ -split '[\s,\t、]+' # Regular expression includes spaces, tabs, commas, and middots
if ($parts.Length -ge 8) {
$username = $parts[0]
$folder2 = $parts[5] # Group folder
$folder3 = $parts[6] # Public directory folder
$permissionType = $parts[7]
# Set NTFS permission type
$ntfsPermission = if ($permissionType -like "*Access*") {
"(OI)(CI)(R)" # Read-only permission
} elseif ($permissionType -like "*Edit*" -or $permissionType -like "*ReadWrite*" -or $permissionType -like "*Save*") {
"(OI)(CI)(M)" # Modify permission
} else {
"(OI)(CI)(R)" # Default to read-only permission
}
# Set share permission type
$sharePermission = if ($permissionType -like "*Access*") {
"Read" # Shared read-only permission
} elseif ($permissionType -like "*Edit*" -or $permissionType -like "*ReadWrite*" -or $permissionType -like "*Save*") {
"Change" # Shared change permission
} else {
"Read" # Default to shared read-only permission
}
# Process folder2 (group folder) path by removing the word "Group"
$folder2WithoutGroup = if ($folder2 -like "*Group") {
$folder2 -replace "Group$", "" # Remove "Group" (at the end)
} else {
$folder2 # Keep it as is if "Group" is not present
}
# Construct the full folder paths
$fullPath1 = Join-Path -Path $baseFolderPath -ChildPath $folder2 # Original group folder path
$fullPath2 = Join-Path -Path $baseFolderPath -ChildPath $folder3 # Public directory folder path
$fullPath3 = Join-Path -Path $baseFolderPath -ChildPath $folder2WithoutGroup # Path without "Group"
# Assign permissions to each folder path
$folders = @($fullPath1, $fullPath2, $fullPath3)
foreach ($folderPath in $folders) {
# Check if the folder path exists
if (-Not (Test-Path $folderPath)) {
Write-Output "Path $folderPath does not exist, skipping this path."
continue
}
# Use icacls to set NTFS permissions
icacls "$folderPath" /grant ${username}:$ntfsPermission /t
Write-Host "Assigned NTFS $ntfsPermission permission to user $username for folder $folderPath." -ForegroundColor Yellow
# Check if the share exists
$netShareName = (Get-Item $folderPath).Name # Use the folder name as the share name
if (Get-SmbShare -Name $netShareName -ErrorAction SilentlyContinue) {
# If the share exists, add share permissions
Grant-SmbShareAccess -Name $netShareName -AccountName "$username" -AccessRight $sharePermission -Force
Write-Host "Assigned shared $sharePermission permission to user $username for share $netShareName." -ForegroundColor Yellow
} else {
Write-Output "Share $netShareName does not exist, skipping share permission assignment."
}
}
}
else {
Write-Output "Line format does not match, skipping: $_"
}
}
Write-Output "All user permissions have been successfully added."
پس از اتمام این اسکریپت، وظایف اصلی مجوزهای اضافه کردن دسته ای اساساً کامل می شود. نیازی به نوشتن اسکریپت های اضافی نیست. هر نیاز آینده احتمالا فقط شامل پردازش دسته ای ساده خواهد بود.