+ Post New Thread
Results 1 to 7 of 7
Scripts Thread, Script to move home directories to new location by OU or username in Coding and Web Development; Hi folks, I'm no scripting guru so please bear with me on this. I have an urgent need to relocate ...
  1. #1
    WTP
    WTP is offline

    Join Date
    Oct 2011
    Location
    Edinburgh
    Posts
    9
    Thank Post
    2
    Thanked 0 Times in 0 Posts
    Rep Power
    0

    Exclamation Script to move home directories to new location by OU or username

    Hi folks,

    I'm no scripting guru so please bear with me on this.

    I have an urgent need to relocate users home directories from one location to another.

    Currently our \\UNCPath\user shared folders\ area contains both pupils and staff folders which is a nono and isn't the best practice. I need to move all the pupils home folders to a new location \\UNCPath\pupils\

    I've generated a username_year_group_x.csv file containing a single column samaccountname for all users from each year group and I want to feed it into a script and use those usernames to move the corresponding folders to their new location.

    I know how to change the home folders from AD for all users but I just can't figure how to move the actual data!

    Hope someone can help me!

  2. #2
    ADMaster's Avatar
    Join Date
    May 2012
    Posts
    332
    Thank Post
    5
    Thanked 35 Times in 30 Posts
    Rep Power
    23
    I just modified my user script to move home directories if their year group changes for some reason. don’t know what language you plan to use, I used PowerShell.
    If you just want to use the csv file you can use the import-csv command and the move-item command.
    If you want to moved based on OU get quest active directory cmdlets and use the set-qaduser and get-qaduser commands.
    This is not exact syntax so don’t copy and paste, I can look at details when I get in tomorrow if you need them.
    Code:
    $users = Get-qaduser –seachscope domain/ou
    Foreach ($user in $users){
    $home  = $user.homepath
    $newhome = \\server\students\yeargroup\$user.samaccountname
    
    Move-item –path $home –destination  $newhome
    Set-qaduser –identify $user –homepath $newhome
    }
    That’s off the top of my head with no syntax checking etc., but should give you a good start.
    Good luck

  3. #3
    WTP
    WTP is offline

    Join Date
    Oct 2011
    Location
    Edinburgh
    Posts
    9
    Thank Post
    2
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Thanks Dana for your reply. Yes, I think I prefer powershell at this point. I tried doing a script around robocopy and richcopy but got nowhere.

    So my script is this, which may have a silly error in it as I wrote it at 2:30am.


    Code:
    $listPath = "d:\temp\csvs\test.csv"
     $oldhome = "D:\DFSRoots\Public\User Shared Folders\"
     $newhome = "D:\DFSRoots\Public\Users\Senior School\Pupils\test1\"
     $list = import-csv $listPath
     foreach($user in $list)
     {
         $path = Join-Path $oldhome -childpath $user.username
         move-item $path $newhome
     }
    I get this error:
    Move-Item : The process cannot access the file because it is being used by another process.
    At C:\Users\administrator.EA\Desktop\move_home_folder s.ps1:8 char:15
    + move-item <<<< $path $newhome
    + CategoryInfo : WriteError: (D:\DFSRoots\Public\User Shared Folders\: DirectoryInfo) [Move-Item], IOExcep
    tion
    + FullyQualifiedErrorId : MoveDirectoryItemIOError,Microsoft.PowerShell.Comm ands.MoveItemCommand

  4. #4

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,698
    Thank Post
    335
    Thanked 516 Times in 484 Posts
    Rep Power
    180
    Quote Originally Posted by WTP View Post
    Move-Item : The process cannot access the file because it is being used by another process.
    Pretty much as it says Either "someone" or "something" has the files/directory open.

    Could be back-up scripts, logged on users etc etc etc. If you're moving it you'll need to kick everyone out of it.

    Steve

  5. #5
    WTP
    WTP is offline

    Join Date
    Oct 2011
    Location
    Edinburgh
    Posts
    9
    Thank Post
    2
    Thanked 0 Times in 0 Posts
    Rep Power
    0
    Steve, not sure if there was anyone else attached to the server at 2:30am!!! However, I think DFSR might be the culprit here.

    If DFSR is the problem then I'll need to stop it before I perform the copy - which will create on heckuva a backlog!!!

    Thoughts?

  6. #6

    Steve21's Avatar
    Join Date
    Feb 2011
    Location
    Swindon
    Posts
    2,698
    Thank Post
    335
    Thanked 516 Times in 484 Posts
    Rep Power
    180
    Quote Originally Posted by WTP View Post
    Steve, not sure if there was anyone else attached to the server at 2:30am!!!
    AV clients running? Backup software? Any service accounts? Locked open files? Could be a lot of things

    You should be able to check what's got a file open (If you find a specific file it's failing on) in the File manager thingy within the server

    Steve

  7. #7
    ADMaster's Avatar
    Join Date
    May 2012
    Posts
    332
    Thank Post
    5
    Thanked 35 Times in 30 Posts
    Rep Power
    23
    The script looks OK to me, I was able to run it on my machine just changing the paths and it worked.
    I have two suggestions
    For your access error either reboot the server or go into the share session manager and kick everyone out. This will ensure no open files. Also check that your user has permission to the directories and all sub directories. I assume you’re using and admin account so this shouldn’t be the case, but never hurts to check.
    You will want to change the path in AD so they connect to the new share. If you do this outside of your script, what you have is fine.
    If you want to do it with in the script use UNC paths when moving their files so you can use the same newpath variable it the set-qaduser command.

    Hope this helps,
    Let us know if you get it working properly.

  8. Thanks to ADMaster from:

    WTP (23rd August 2012)

SHARE:
+ Post New Thread

Similar Threads

  1. Robocopy script to move old backups to archive share
    By fat_techie_virgin in forum Scripts
    Replies: 3
    Last Post: 19th October 2010, 01:48 PM
  2. Moving Users Home Directories to a file server - RM
    By LorenzosEar in forum Wireless Networks
    Replies: 0
    Last Post: 9th July 2009, 04:21 PM
  3. Giving user access to all home directories
    By paularmstrong in forum Windows
    Replies: 11
    Last Post: 7th November 2007, 08:27 AM
  4. Replies: 8
    Last Post: 1st February 2007, 08:42 AM
  5. Replies: 19
    Last Post: 20th June 2006, 03:53 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •