Przenieś uprawnienia do folderów domowych do nowej usługi Active Directory

Nov 23 2020

Muszę przenieść foldery domowe ponad 100 użytkowników z jednej AD do drugiej. SamAccountName dla każdego użytkownika jest taka sama w obu AD, ale ObjectGUID różni się, ponieważ użytkownicy zostali wyeksportowani i zaimportowani za pomocą pliku CSV, a nie zaufania.

Kopiowanie nie jest prawdziwym problemem - wydaje mi się, że mam działającą linię robocopy, ale bardziej martwię się ustawieniem uprawnień do folderów po przeniesieniu. Chciałbym iterować po każdym folderze domowym i przypisać własność oraz pełną kontrolę użytkownikowi, którego nazwa konta samAccountName odpowiada nazwie folderu.

Jednak nie czuję się komfortowo z poleceniami programu PowerShell Get-ACL i Set-ACL. Jeśli dobrze rozumiem, muszę najpierw pobrać listę ACL z folderu do zmiennej, a następnie manipulować uprawnieniami do zmiennej, a następnie zastosować odpowiednie uprawnienia za pomocą Set-ACL.

Tak, jak to sobie wyobrażam:

  1. Pobierz ACL z folderu do $ UserACL
  2. Uzyskaj nazwę folderu użytkownika
  3. Sprawdź, czy mogę dopasować nazwę folderu do nazwy konta samAccountName
  4. Jeśli tak, dodaj uprawnienia użytkownika do $ UserACL
  5. Ustaw uprawnienia do folderu, uruchamiając Set-ACL w folderze użytkownika
  6. Jeśli nie pasuje, ustaw uprawnienia tak, aby tylko administratorzy mieli dostęp do folderu (istnieje wiele nieaktywnych kont, których nie można po prostu usunąć)

Pseudo kod:

$baseACL = Get-ACL -Path [ExampleDir] $HomeFolders = Get-ChildItem [RootDir] | Where-Object {$_.PSIsContainer} | Foreach-Object {$_.Name}
$ADUsers=Import-csv 'UserCSV.csv' -Delimiter ';' Foreach ($Folder in $HomeFolders) { ForEach ($User in $ADUsers) { if ($Folder -eq $($User.samAccountName)) {
      # Set properties
      $useridentity = "[AD]\$Folder"
      $admidentity = "BUILTIN\Administrators" $fileSystemRights = "FullControl"
      $type = "Allow" # Create new rule $fileSystemAccessRuleArgumentList = $useridentity, $admidentity, $fileSystemRights, $type
      $fileSystemAccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $fileSystemAccessRuleArgumentList
      # Apply new rule
      $baseACL.SetAccessRule($fileSystemAccessRule)
      Set-Acl -Path "[Path]\$Folder" -AclObject $baseACL
    }
    Else {
      $admidentity = "BUILTIN\Administrators" $fileSystemRights = "FullControl"
      $type = "Allow" # Create new rule $fileSystemAccessRuleArgumentList = $admidentity, $fileSystemRights, $type $fileSystemAccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $fileSystemAccessRuleArgumentList # Apply new rule $baseACL.SetAccessRule($fileSystemAccessRule) Set-Acl -Path "[Path]\$Folder" -AclObject $baseACL
    }
  }
}

To tylko ustawianie uprawnień, a nie własności, więc nie jestem pewien, czy jest to nawet możliwe od razu dzięki PowerShell. A może będę musiał spojrzeć na CACL i Takeown?

Odpowiedzi

Laage Dec 09 2020 at 17:26

Znalazłem tutaj rozwiązanie, które przy odrobinie poprawek zadziałało: HALP! Odziedziczyłem koszmar uprawnień do przekierowanych folderów / katalogów domowych

Mój gotowy skrypt wygląda głównie tak:

#requires -PSEdition Desktop
#requires -version 5.1
#requires -Modules ActiveDirectory
#requires -RunAsAdministrator

[CmdLetBinding()]
Param ()

$AD = [DOMAIN CONTROLLER] $Root = [HOME DIRECTORY]

$Paths = Get-ChildItem $Root -Directory | Select-Object -Property Name,FullName

# Local Admin access rule
$LASID = Get-LocalGroup -Name 'Administrators' | Select-Object -ExpandProperty SID $LAAR = New-Object System.Security.AccessControl.FileSystemAccessRule($LASID, "FullControl", "ContainerInherit, ObjectInherit", "None", "Allow") # Domain Admin access rule $DASID = Get-ADGroup -Server $AD -Filter { Name -eq 'Domain Admins' } | Select-Object -ExpandProperty SID $DAAR = New-Object System.Security.AccessControl.FileSystemAccessRule($DASID, "FullControl", "ContainerInherit, ObjectInherit", "None", "Allow") # System Access rule $SysAR = New-Object System.Security.AccessControl.FileSystemAccessRule("SYSTEM", "FullControl", "ContainerInherit,ObjectInherit", "None", "Allow")

Try {
  foreach ($Directory in $Paths) {
    $samExists = $(try {Get-ADUser -Server $AD -Identity $($Directory.Name)} catch {$null})
    if ($samExists -ne $null) {
      # For error handling purposes - not all folders will map to a user of the exact same name
      Write-Output "Creating User ACL for $($Directory.FullName) ... "
      
      # Creates a blank ACL object to to add access rules into, also blanks out the ACL for each iteration of the loop
      $ACL = New-Object System.Security.AccessControl.DirectorySecurity # Creating the right type of user object to feed into our ACL and populating it with the user whose folder we're currently on $UserSID = Get-ADUser -Server $AD -Identity $($Directory.Name) | Select-Object -ExpandProperty SID # $objUser = New-Object System.Security.Principal.NTAccount($UserSID) # Access rule for the user whose folder we're dealing with this iteration $UserAR = New-Object System.Security.AccessControl.FileSystemAccessRule($UserSID, "FullControl","ContainerInherit,ObjectInherit", "None", "Allow") # Change the inheritance, propagation settings for the folder we're dealing with $ACL.SetOwner($UserSID) $ACL.SetAccessRuleProtection($true,$false)

      $ACL.SetAccessRule($UserAR)
      $ACL.SetAccessRule($LAAR)
      $ACL.SetAccessRule($DAAR)
      $ACL.SetAccessRule($SysAR)

      # For error handling purposes - not all folders will map to a user of the exact same name
      $ACL | Format-List Set-Acl -Path $Directory.FullName -AclObject $ACL } else { # For error handling purposes - not all folders will map to a user of the exact same name Write-Warning "Creating Admin ACL for $($Directory.FullName) ... " # Creates a blank ACL object to to add access rules into, also blanks out the ACL for each iteration of the loop $ACL = New-Object System.Security.AccessControl.DirectorySecurity

      # Creating the right type of user object to feed into our ACL and populating it with the user whose folder we're currently on
      # $objUser = New-Object System.Security.Principal.NTAccount($DASID)

      # Change the inheritance, propagation settings for the folder we're dealing with
      $ACL.SetOwner($DASID)
      $ACL.SetAccessRuleProtection($true,$false) $ACL.SetAccessRule($LAAR) $ACL.SetAccessRule($DAAR) $ACL.SetAccessRule($SysAR) # For error handling purposes - not all folders will map to a user of the exact same name $ACL | Format-List

      Set-Acl -Path $Directory.FullName -AclObject $ACL
    }
  }
}

Catch {
  Write-Host -BackgroundColor Red "Error: $($_.Exception)"
  Break
}