0 votes

I would like to generate a script that would copy the contents of a users home folder to their managers home folder when the deprovisioning script is fired off. my preference would be that it create a subfolder with the users name in the managers home folder for example

if not exist, create an "Old Users" folder in managers home directory
copy users home folder to managerhomefolder\old users\deprovisionedusername\contents

how would I do this in a script?

by (40 points)

1 Answer

0 votes
by (215k points)

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

Related questions

0 votes
1 answer

I am having an issue with home folder moves between servers. When I move a folder between servers, using Adaxes, the user permissions are lost. I know that this is expected ... to re-assign the user permissions so they can access their home folder again :?:

asked Sep 23, 2014 by rmedeiros (360 points)
0 votes
1 answer

Good Morning, I've been working through some of my processes and I'm not looking to make sure the deletion of Home directories (both remote and standard) as well as ... for user deletion. If there are any questions or clarification needed, please let me know.

asked Oct 16, 2015 by jtop (680 points)
0 votes
1 answer

Hi folks, I already have a great script (thank you) that monitors our HR system for adds/removes/changes of our staff and reflects those changes in AD via scheduled ... way of doing this? Could I possibly incorporate it into the existing script? Thanks Corey

asked Dec 11, 2014 by ckemp (170 points)
0 votes
1 answer

Hi, I'm new to this forum so I hope this question hasn't already been answered. The sceanrio is that the Adaxes servert is part of domain A and is used to ... , ObjectInherit", "None", "Allow") $acl.AddAccessRule($rule) #Commit Changes Set-Acl $userHome $acl

asked Dec 8, 2014 by niclas.eriksson (260 points)
0 votes
1 answer

How would a script be written that accomplishes the following task? In this scenario, UserA would be selected with the Modify user action, then UserB would be input to ... rule that runs the script to allow reviewer permissions on UserA's calendar for UserB

asked Dec 6, 2012 by mdeflice (350 points)
2,803 questions
2,535 answers
6,606 comments
62,610 users