Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Param(
- [string]$muUsername,
- [string]$importfile,
- [string]$accesscsv,
- [string]$folderroot,
- [string]$domain
- )
- function allowAccess ($file,$user,$levelOfAccess,$inheritance,$propagation,$type)
- {
- $aclFile = Get-Acl -Path $file
- $allowAccess = New-Object System.Security.AccessControl.FileSystemAccessRule ($user, $levelOfAccess, $inheritance, $propagation,$type)
- $aclFile.SetAccessRule($allowAccess)
- Set-Acl -Path $file -AclObject $aclFile
- }
- #import user list
- $folderList = Import-Csv -Path $importfile
- #import ACLs
- $accessList = Import-Csv -Path $accesscsv
- #start folder creation loop
- $folderList|
- ForEach-Object{
- #pull user display name
- $folderName = $_.name
- #sets samaccountname for ACL addition
- $accountName = $domain + "\" + $_.samaccountname
- #creates folder path
- $folderPath = $folderroot + "\" + $folderName
- #sets inheritance to allow ACL to propigate to children of new folder
- $inheritanceFlags = "ContainerInherit, ObjectInherit"
- $propagationFlags = "none"
- $folderAccess = "Allow"
- #test if folder exists
- $exist = Test-Path -Path $folderPath
- #create folder loop
- if ($exist -ne $true)
- {
- #create folder
- New-Item -ItemType "directory" -Path $folderPath
- $accessList|
- ForEach-Object{
- $username = $_.name
- $access = $_.access
- $type = $_.type
- #creates ACL rule
- allowAccess -file $folderPath -user $username -levelOfAccess $access -inheritance $inheritanceFlags -propagation $propagationFlags -type $type
- }
- #creates ACL for myself *MAY NOT NEED*
- allowAccess -file $folderPath -user $myusername -levelOfAccess "FullControl" -inheritance $inheritanceFlags -propagation $propagationFlags -type $folderAccess
- #creates ACL for user
- allowAccess -file $folderPath -user $accountName -levelOfAccess "FullControl" -inheritance $inheritanceFlags -propagation $propagationFlags -type $folderAccess
- #disables inheritance and removes inherited rights
- $acl = Get-Acl $folderPath
- $acl.SetAccessRuleProtection(1,0)
- #saves inheritance rule
- Set-Acl -Path $folderPath -AclObject $acl
- }
- elseif ($exist -eq $true)
- {
- Write-Host "Folder already exists for $folderName"
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement