Jump to content

Migrating Home Directory data from one folder to another


Recommended Posts

Posted

We're wanting to migrate our pupil AD accounts so their home directory is their new username. Before the summer holidays we gave them their new accounts but their home directories were pointing to their old username as that's where all their work is however we've now got them pointing to their new home directory which is empty. What I want is a powershell script that reads a CSV file I've created with their old and new username in that moves the files in their old directory to their new one based on the data in the spreadsheet. Something like:

 

$Users = Import-Csv .\Filename.csv
$robocopy = Foreach ($user in $users)
{
robocopy \\fileserver\share\$($user.sOld) \\fileserver\share$\$($users.New) /MIR /MOV
}

 

For example 16ABC123's documents would be moved to 16SmithA as that will be their new username.

 

Seems simple enough but just can't get it working. Any suggestions? The script runs but doesn't output anything and doesn't move any data.

Posted
Looks like a small typo in your script in your new directory - $($users.New) should probably be $($user.New) - you have your file variable rather than the user loop.
Posted
Looks like a small typo in your script in your new directory - $($users.New) should probably be $($user.New) - you have your file variable rather than the user loop.

 

Ah yeah well spotted.

 

Do you share the folder or share each users home folder?

 

The folder is shared not the users home folder. However even using:

 

robocopy \\fileserver\share$\($user.Old) \\fileserver\share$\($user.New)

doesn't do anything.

 

Neither does:

 

robocopy \\fileserver\share$\$user.Old \\fileserver\share$\$user.New

 

I'm probably doing really simple wrong as not used Powershell much.

Posted

Jus a thought that you could jus rename the users home folders, to change the permissions you can bulk select in Active Directory and change the home path to \\fileserver\share$\%username% . Test on some test accounts first to make sure results are expected.

 

At my previous employment we renamed all the user accounts and then renamed all the home folders to suit.

Posted

I'd check it's not a robocopy problem first, if you manually do a user using your robocopy command from a command prompt does it work?

 

If it does, is the entirity of your script in the first post or does it have something else?

 

I start my scripts now with

Start-Transcript -path c:\scriptname.txt -Append

and then that way I can go through and read the output to see if there's anything odd going on afterwards.

  • Thanks 1
Posted

I would use the following, haven't tested it.

 

#Import columns sOld and New from csv file (must be stored in location PS script is run from).
$usernames = Import-Csv usernames.csv
foreach ($u in $usernames) {
#Take ownership to avoid permission issues
takeown /f $u.sOld /R /D Y /A
#Copy the files in Mirror mode (swap /MIR for /E if you want keep destination files if any exist)
robocopy \\fileserver\share\$u.sOld \\fileserver\share$\$u.New /MIR /R:1 /W:5 /Z
#Set student as Owner again
$domuser = "domainname\$u.New"
               $accnt = New-Object System.Security.Principal.NTAccount($domuser)
			$acl.SetOwner($accnt)
               Set-Acl \\fileserver\share$\$u.New $acl
			dir -r \\fileserver\share$\$u.New | Set-Acl -AclObject $acl
}

  • Thanks 1
Posted (edited)
I'd check it's not a robocopy problem first, if you manually do a user using your robocopy command from a command prompt does it work?

 

If it does, is the entirity of your script in the first post or does it have something else?

 

I start my scripts now with

Start-Transcript -path c:\scriptname.txt -Append

and then that way I can go through and read the output to see if there's anything odd going on afterwards.

 

This is my code in its entirety which does work if I manually specify the usernames to copy from and to:

 

$Users = Import-Csv .\Users.csv
$robocopy = Foreach ($user in $users)
{
robocopy "\\fileserver\share$\" "\\fileserver\share$\" /MIR /R:1 /W:5 /Z
}
exit

 

This would just take ages as I'd have to manually add each user each time I ran the script. Problem seems to be trying to read and execute data from a .csv file.

 

I would use the following, haven't tested it.

 

#Import columns sOld and New from csv file (must be stored in location PS script is run from).
$usernames = Import-Csv usernames.csv
foreach ($u in $usernames) {
#Take ownership to avoid permission issues
takeown /f $u.sOld /R /D Y /A
#Copy the files in Mirror mode (swap /MIR for /E if you want keep destination files if any exist)
robocopy \\fileserver\share\$u.sOld \\fileserver\share$\$u.New /MIR /R:1 /W:5 /Z
#Set student as Owner again
$domuser = "domainname\$u.New"
               $accnt = New-Object System.Security.Principal.NTAccount($domuser)
			$acl.SetOwner($accnt)
               Set-Acl \\fileserver\share$\$u.New $acl
			dir -r \\fileserver\share$\$u.New | Set-Acl -AclObject $acl
}

 

Thanks for taking the time to post that. I've given it a try and changed the details so it matches our network here but get the following errors which I've summarised:

 

ERROR 2 (0x00000002) Accessing Source Directory

The system cannot find the file specified.

You cannot call a method on a null-valued expression. At .ps1:11 char:5

 

Set-Acl : Cannot bind argument to parameter 'AclObject' because it is null.

At .ps1:12 char:46

 

I'm running this from my local machine rather than on the file server. Will that many any difference? I'm right clicking the file and running with powershell which should be running it as admin. I'll see if I can get it working though but at least I've got something to work with.

Edited by protocol
Posted

Think I've managed to get a script working that does what I want.

 

$Users = Import-Csv .\Users.csv
Foreach ($user in $users) {
$source = '\\fileserver\share$\' + $user.Old
$destination = '\\fileserver\share$\' + $user.New
robocopy $source $destination /COPYALL /E /R:1 /W:5 /Z
}

 

It seems pupil's folders are actually owned by Administrators so have left those permissions alone as I've inherited this network. Could do with it saving to a log file but one thing at a time.

 

Thanks for everyone's help to get me to this stage.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...