Hello,
You can use the following script to copy a user's home folder to his manager's home folder.
function CopyDirectory($source, $target)
{
# If the destination directory does not exist, create it.
if (-not([System.IO.Directory]::Exists($target)))
{
[System.IO.Directory]::CreateDirectory($target);
}
$sourceDir = New-Object "System.IO.DirectoryInfo" $source;
# Copy files
$files = $sourceDir.GetFiles();
foreach ($file in $files)
{
# Create the path to the new copy of the file.
$temppath = [System.IO.Path]::Combine($target, $file.Name);
$file.CopyTo($temppath, $false);
}
# Copy sub-directories
$subDirs = $sourceDir.GetDirectories();
foreach ($subDir in $subDirs)
{
# Create the subdirectory.
$temppath = [System.IO.Path]::Combine($target, $subDir.Name);
# Copy the subdirectories.
CopyDirectory $subDir.FullName $temppath;
}
}
# Get the user's home folder
$user = $Context.TargetObject
try
{
$userHomeDirectory = $user.Get("homeDirectory")
}
catch [System.Runtime.InteropServices.COMException]
{
# The user doesn't have home directory
return;
}
if (-not([System.IO.Directory]::Exists($userHomeDirectory)))
{
$Context.LogMessage("The user's home directory doesn't exist.", "Warning")
return;
}
# Get manager's home folder
try
{
$managerDN = $user.Get("manager")
}
catch [System.Runtime.InteropServices.COMException]
{
$Context.LogMessage("The user doesn't have any manager specified.", "Warning")
return;
}
$manager = $Context.BindToObjectByDN($managerDN)
try
{
$managerHomeDirectory = $manager.Get("homeDirectory")
}
catch [System.Runtime.InteropServices.COMException]
{
$Context.LogMessage("The user's manager doesn't have home directory.", "Warning")
return;
}
$targetDirectory = $managerHomeDirectory + "\Old Users\%username%"
# Copy user's home folder
CopyDirectory $userHomeDirectory $targetDirectory