홈 폴더에 대한 권한을 새 Active Directory로 이동

Nov 23 2020

100 명 이상의 사용자를위한 홈 폴더를 한 AD에서 다른 AD로 이동해야합니다. 각 사용자의 samAccountName은 두 AD에서 동일하지만 사용자가 트러스트가 아닌 CSV 파일을 사용하여 내보내고 가져 왔기 때문에 ObjectGUID가 다릅니다.

복사는 실제 문제가 아닙니다. 로보 카피 라인이 작동하고 있다고 생각하지만 이동 후 폴더에 대한 권한 설정에 대해 더 걱정됩니다. 각 홈 폴더를 반복하고 samAccountName이 폴더 이름과 일치하는 사용자에게 소유권과 모든 권한을 할당하고 싶습니다.

그러나 Powershell Get-ACL 및 Set-ACL 명령에 완전히 익숙하지 않습니다. 올바르게 이해하면 먼저 폴더에서 변수로 ACL을 가져온 다음 변수에 대한 권한을 조작 한 다음 Set-ACL로 올바른 권한을 적용해야합니다.

내가 상상하는 방식 :

  1. 폴더에서 $ UserACL로 Get-ACL
  2. 사용자 폴더의 이름 가져 오기
  3. 폴더 이름을 samAccountName과 일치시킬 수 있는지 확인
  4. 그렇다면 $ UserACL에 사용자 권한을 추가하십시오.
  5. 사용자 폴더에서 Set-ACL을 실행하여 폴더에 대한 권한 설정
  6. 일치하지 않는 경우 관리자 만 폴더에 액세스 할 수 있도록 권한을 설정합니다 (삭제할 수없는 비활성 계정이 많이 있습니다).

의사 코드 :

$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
    }
  }
}

이것은 소유권이 아닌 권한을 설정하는 것이므로 powershell을 사용하여 즉시 가능한지 확실하지 않습니다. 아니면 cacls와 takeown을 살펴 봐야합니까?

답변

Laage Dec 09 2020 at 17:26

나는 약간의 조정으로 나를 위해 일한 해결책을 찾았습니다 : HALP! 리디렉션 된 폴더 / 홈 디렉터리에 대한 권한 악몽을 물려 받았습니다.

완성 된 스크립트는 대부분 다음과 같습니다.

#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
}