Advertisement
Guest User

Untitled

a guest
Nov 11th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #transform inputfile to custom objectcollection
  2. $Employees = New-Object System.Collections.ArrayList
  3.  
  4. foreach ($element in $importfile){
  5.     $pscustom = New-Object -TypeName psobject
  6.    
  7.     $pscustom | Add-Member -MemberType NoteProperty -Name Folder -Value $element[0]
  8.     $pscustom | Add-Member -MemberType NoteProperty -Name ZugriffSoll -Value $($Admins + "; " + $element[1])
  9.     $pscustom | Add-Member -MemberType NoteProperty -Name Status -Value "pending"
  10.    
  11.     $Employees.add($pscustom)  
  12. }
  13.  
  14.  
  15. #Alle Ordner einlesen
  16.  
  17. #Ordnername --> Vorgesetzten rausfinden --> Berechtigung setzen
  18.  
  19. $Folders = Get-ChildItem $Directory
  20.  
  21. #Employees without a folder
  22. $Employees | ? {$_.Folder -NotIn $Folders.Name } | % {$_.Status = "missing folder"}
  23.  
  24. #Employees with a folder
  25. $Employees | ? {$_.Folder -In $Folders.Name } | % {$_.Status = "permission"}
  26.  
  27.  
  28. #Set Permissions for Employes with a $folder
  29.  
  30. foreach ($Employee in $Employees | ? Status -eq "permission"){
  31.    
  32.     $path = ($Directory + "\" + $Employee.folder)
  33.     $Rights = "FullControl"
  34.     $InheritSettings = "Containerinherit, ObjectInherit" #Controls how permissions are inherited by children
  35.     $PropogationSettings = "None" #Usually set to none but can setup rules that only apply to children.
  36.     $RuleType = "Allow" #Allow or Deny.
  37.    
  38.     $Users  = $Employee.ZugriffSoll.Split(";").trim()
  39.  
  40.     foreach ($User in $Users | ? $_.length -ne 0){
  41.         $acl = Get-Acl $path
  42.        
  43.         #Remove previous Access
  44.        ## not working... $acl.access | ? {$_.IdentityReference.value -notlike "IMRECYCLING\IMR_Administratoren"} | %{$acl.RemoveAccessRule($_)}
  45.  
  46.         $perm = $User, $Rights, $InheritSettings, $PropogationSettings, $RuleType
  47.         $rule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $perm
  48.         try {
  49.             $acl.SetAccessRule($rule)
  50.             $acl | Set-Acl -Path $path
  51.             $Employees | ? {$_.Folder -eq $Employee.Folder } | % {$_.Status = "ok"}
  52.         }
  53.         catch {
  54.             $Employees | ? {$_.Folder -eq $Employee.Folder } | % {$_.Status = "failed"}
  55.         }
  56.        
  57.     }
  58.    
  59. }
  60.  
  61. $Employees | ? Status -eq "failed"
  62.  
  63. Read-Host
  64. #Set read Permission for unique users
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement