Script repository
The script moves a user home folder to another location. Unlike the built-in Move home folder action, it does not stop when unable to move a folder or file, and copies it instead. All locked items will be listed in the Execution log. To execute the script, create a business rule, custom command or scheduled task configured for the User object type.
In the script, the $destinationPath variable specifies a path to the folder to move user home folder to.
$destinationPath = "\\server\share\%username%" # TODO: modify me
function DeleteObject ($objectPath)
{
try
{
Remove-Item $objectPath -Force -ErrorAction Stop
}
catch
{
$Context.LogMessage("Error deleting the home directory of the user. Could not delete file or folder '$objectPath'. Error message: " + $_.Exception.Message, "Warning")
}
}
function GetChildItems ($objectPath)
{
$object = Get-Item $objectPath -Force
if ($object -isnot [System.IO.DirectoryInfo])
{
DeleteObject $object.FullName
return
}
$childItems = Get-ChildItem $object.FullName -Force
if($childItems -eq $NULL)
{
DeleteObject $object.FullName
return
}
foreach ($item in $childItems)
{
GetChildItems $item.FullName
}
DeleteObject $object.FullName
}
try
{
Copy-Item -Recurse -Path "%homeDirectory%" -Destination "$destinationPath" -Force -ErrorAction Stop
}
catch
{
$Context.LogMessage($_.Exception.Message, "Warning")
return
}
# Update user.
$Context.TargetObject.Put("homeDirectory", $destinationPath)
$Context.TargetObject.SetInfo()
# Remove old folder.
GetChildItems "%homeDirectory%"
Comments 0
You must be signed in to comment.