2013-05-14 33 views
0

我有成千上萬的文件夾,我需要用Fullcontrol訪問來改變用戶來修改訪問權限。下面是什麼,我有一個列表:用fullcontrol修改用戶的NTFS安全性來修改

$ ACL =獲取的ACL 「G:\文件夾」

  1. 改變NTFS權限的腳本 $ ACL |格式列表 $ acl.GetAccessRules($ true,$ true,[System.Security.Principal.NTAccount]) #second $ true在繼續執行以下行時,$ False關閉 $ acl.SetAccessRuleProtection($ True, $ True) $ acl.AddAccessRule($ rule) $ rule = New-Object System.Security.AccessControl.FileSystemAccessRule(「Administrators」,「FullControl」,「ContainerInherit,ObjectInherit」,「None」,「Allow」 $ rule = New-Object System.Security.AccessControl.FileSystemAccessRule(「My-ServerTeam」,「FullControl」,「ContainerInherit,ObjectInherit」,「None」,「Allow」) $ acl.AddAccessRule($ rule) $ rule = New-Object System.Security.AccessControl.FileSystemAccessRule(「Users」,「Read」,「ContainerInherit,ObjectInherit」,「None」,「Allow」) $ acl.AddAccessRu le($ rule) Set-Acl「G:\ Folder」$ acl Get-Acl「G:\ Folder」|格式列表

  2. 一個文本文件,包含需要從fullcontrol更改爲修改的目錄和用戶。

我總能創造路徑和/或用戶名變量,並創建一個foreach循環,但我不知道怎麼改中存在的ACL爲每個文件夾修改的用戶,但保留管理員帳戶爲完全控制。任何幫助,將不勝感激。

回答

0

去了另一條路線,並得到了我所需要的。我並不感到驚訝,沒有人試圖幫助我解決這個問題......這很艱難。我將爲有此問題的下一個人發佈腳本。 有兩個腳本。我第一次從互聯網上獲得並改變了一下。第二個腳本使用自動化所需的參數啓動第一個腳本。

的第一個Script命名SetFolderPermission.ps1:

param ([string]$Path, [string]$Access, [string]$Permission = ("Modify"), [switch]$help) 
function GetHelp() { 
$HelpText = @" 

DESCRIPTION: 
NAME: SetFolderPermission.ps1 
Sets FolderPermissions for User on a Folder. 
Creates folder if not exist. 

PARAMETERS: 
-Path   Folder to Create or Modify (Required) 
-User   User who should have access (Required) 
-Permission  Specify Permission for User, Default set to Modify (Optional) 
-help   Prints the HelpFile (Optional) 

SYNTAX: 
./SetFolderPermission.ps1 -Path C:\Folder\NewFolder -Access Domain\UserName -Permission FullControl 

Creates the folder C:\Folder\NewFolder if it doesn't exist. 
Sets Full Control for Domain\UserName 

./SetFolderPermission.ps1 -Path C:\Folder\NewFolder -Access Domain\UserName 

Creates the folder C:\Folder\NewFolder if it doesn't exist. 
Sets Modify (Default Value) for Domain\UserName 

./SetFolderPermission.ps1 -help 

Displays the help topic for the script 

Below Are Available Values for -Permission 

"@ 
$HelpText 

[system.enum]::getnames([System.Security.AccessControl.FileSystemRights]) 

} 

<# 
function CreateFolder ([string]$Path) { 

    # Check if the folder Exists 

    if (Test-Path $Path) { 
     Write-Host "Folder: $Path Already Exists" -ForeGroundColor Yellow 
    } else { 
     Write-Host "Creating $Path" -Foregroundcolor Green 
     New-Item -Path $Path -type directory | Out-Null 
    } 
} 
#> 

function SetAcl ([string]$Path, [string]$Access, [string]$Permission) { 

    # Get ACL on FOlder 

    $GetACL = Get-Acl $Path 

    # Set up AccessRule 

    $Allinherit = [system.security.accesscontrol.InheritanceFlags]"ContainerInherit, ObjectInherit" 
    $Allpropagation = [system.security.accesscontrol.PropagationFlags]"None" 
    $AccessRule = New-Object system.security.AccessControl.FileSystemAccessRule($Access, $Permission, $AllInherit, $Allpropagation, "Allow") 

    # Check if Access Already Exists 

    if ($GetACL.Access | Where {$_.IdentityReference -eq $Access}) { 

     Write-Host "Modifying Permissions For: $Access on directory: $Path" -ForeGroundColor Yellow 

     $AccessModification = New-Object system.security.AccessControl.AccessControlModification 
     $AccessModification.value__ = 2 
     $Modification = $False 
     $GetACL.ModifyAccessRule($AccessModification, $AccessRule, [ref]$Modification) | Out-Null 
    } else { 

     Write-Host "Adding Permission: $Permission For: $Access" 

     $GetACL.AddAccessRule($AccessRule) 
    } 

    Set-Acl -aclobject $GetACL -Path $Path 

    Write-Host "Permission: $Permission Set For: $Access on directory: $Path" -ForeGroundColor Green 
} 

if ($help) { GetHelp } 

if ($Access -AND $Permission) { 
    SetAcl $Path $Access $Permission 
} 

下一個腳本調用第一個腳本,並添加所需的參數。包含2列的CSV文件夾和用戶名完全控制。

$path = "C:\Scripts\scandata\TwoColumnCSVwithPathandUserwithFullControl.csv" 
$csv = Import-csv -path $path 
foreach($line in $csv){ 
$userN = $line.IdentityReference 
$PathN = $line.Path 
$dir = "$PathN" 
$DomUser = "$userN" 
$Perm = "Modify" 
$scriptPath = "C:\Scripts\SetFolderPermission.ps1" 
$argumentList1 = '-Path' 
$argumentList2 = "$dir" 
$argumentList3 = '-Access' 
$argumentList4 = "$DomUser" 
$argumentList5 = '-Permission' 
$argumentList6 = "$Perm" 
Invoke-Expression "$scriptPath $argumentList1 $argumentList2 $argumentList3 $argumentList4 $argumentList5 $argumentList6"