ホームフォルダーのアクセス許可を新しいActiveDirectoryに移動します

Nov 23 2020

100人以上のユーザーのホームフォルダーをあるADから別のADに移動する必要があります。各ユーザーのsamAccountNameは両方のADで同じですが、ユーザーが信頼ではなくCSVファイルでエクスポートおよびインポートされたため、ObjectGUIDは異なります。

コピーは実際の問題ではありません。robocopy行が機能していると思いますが、移動後にフォルダーにアクセス許可を設定することについてもっと心配しています。各ホームフォルダを繰り返し処理し、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
}