Unlike the built-in Move Home Directory action that discards permissions when moving home folders to a different server, this script moves home folders preserving the permissions. You can use it in your business rules, custom commands and scheduled tasks by adding the Run a program or PowerShell script action.
Parameters:
- $targetPath - Specifies the UNC path to the new location of the user's home directory.
- $shareName - Specifying the name of the user's home directory share. If you don't want to share the home directory at the new location, specify $NULL.
Note: You can use value references in the folder path and share name. For example, if you specify %username% for the share name, the folder will be shared under the username of the user.
PowerShell
$targetPath = "\\Server\share\%username%" # TODO: modify me
$shareName = $NULL # TODO: modify me
# Get the current path to the home folder
try
{
$homeFolderPath = $Context.TargetObject.Get("homeDirectory")
}
catch
{
return
}
# Get current home folder ACL
try
{
$aclObject = Get-Acl -Path $homeFolderPath -ErrorAction Stop
}
catch
{
$Context.LogMessage("Cannot get permissions for folder '$homeFolderPath'. Error: " + $_.Exception.Message, "Warning")
return
}
# Move the home folder
try
{
$Context.TargetObject.MoveHomeDirectory($targetPath, $shareName)
}
catch
{
$Context.LogMessage("Cannot move folder '$homeFolderPath'. Error: " + $_.Exception.Message, "Warning")
return
}
# Re-apply the ACL to the folder at the new location
try
{
Set-Acl -Path $targetPath -AclObject $aclObject -Confirm:$False -ErrorAction Stop
}
catch
{
$Context.LogMessage("Cannot apply permissions to folder '$targetPath'. Error: " + $_.Exception.Message, "Warning")
}