howto://bulk modify user home directory paths with dsmod

by Ed Fisher on 2010-04-14

in Infrastructure

 

We’re moving all of our file shares from single server physical paths to DFS paths to help with fault-tolerance and disaster recovery, and to make it easier to move things around if we ever want to rebalance our file servers or decommission older servers. One of the things that became necessary is modifying users’ home directory attributes to use the new path. Rather than modify each user by hand, I decided to use dsmod to accomplish this.

This i just a short post to remind me of what I did in case I need to do  it again. If you find it helpful, all the better. We need to generate a list of all users with their existing home directory paths, a list with all the users and their DFS home directory paths, a command sequence to modify all the accounts, and a command sequence to fall back just in case something goes wrong. We’ll use text files, dsquery, dsget, and dsmod to accomplish this.

 

First, we need to get the list of our users, along with their home directory paths. To do this, we will use dsquery to get the users, and pipe it into dsget to extract the two values we need, which the ds commands shorten as dn and hmdir. We’ll redirect the output of this to our first text file. By default, dsquery only returns the first 100 results, so we will make sure we get them all by increasing the –limit to a value greater than our number of users.

dsquery user –limit 500 "ou=user accounts,dc=example,dc=com" | dsget user –dn –hmdir >> c:\scratch\hmdirout.txt [enter]

We need to clean this file up. I like pspad but you can use any text editor to remove the header line, the last line, to enclose the username in quotes, to enclose the home directory path in quotes, and to separate them with a semicolon. Save this file as c:\scratch\undo.txt. It should look like this

  • "cn=Ed Fisher,ou=user accounts,dc=example,dc=com";"\\server\home\efisher"
    "cn=Malcolm Reynolds,ou=user accounts,dc=example,dc=com";"\\server\home\mreynolds"
    "cn=Zoe Washburn,ou=user accounts,dc=example,dc=com";"\\server\home\zwashburn"
    "cn=Jayne Cobb,ou=user accounts,dc=example,dc=com";"\\server\home\jcobb"
    "cn=Kaylee Frye,ou=user accounts,dc=example,dc=com";"\\server\home\kfrye"
    etc.

Then, do a search and replace to change the path to the home directories as required. Save this file as c:\scratch\do.txt. That should leave you a file that looks liked this.

  • "cn=Ed Fisher,ou=user accounts,dc=example,dc=com";"\\example.com\dfs\home\efisher"
    "cn=Malcolm Reynolds,ou=user accounts,dc=example,dc=com";"\\example.com\dfs\home\mreynolds"
    "cn=Zoe Washburn,ou=user accounts,dc=example,dc=com";"\\example.com\dfs\home\zwashburn"
    "cn=Jayne Cobb,ou=user accounts,dc=example,dc=com";"\\example.com\dfs\home\jcobb"
    "cn=Kaylee Frye,ou=user accounts,dc=example,dc=com";"\\example.com\dfs\home\kfrye"
    etc.

With that, we are ready to go. To make the change, the command sequence should look like this.

for /f "tokens=1,2 delims=;" %G in (do.txt) do dsmod user %G –hmdir %H [enter]

As each line executes, you will see "dsmod succeeded:CN=…" Should you find the need to revert the changes, all you’ll have to do is run this.

for /f "tokens=1,2 delims=;" %G in (undo.txt) do dsmod user %G –hmdir %H [enter]

which will restore the homeDirectory attribute to its original value. That’s all there is to this post. I hope you find it useful, and I remember I wrote it the next time I have to do this.

Speaking of home, today’s video from former New York Police Officer Eddie Money was released in 1986, reached #4 on the Billboard Hot 100, and includes Ronnie Spector reprising the chorus from The Ronnette’s hit "Be My Baby." It has nothing at all to do with home directories, but it does have ‘home’ in the title, was shot in black and white (always a win in my book,) remains one of my favourite songs, and let me play Casey Kasem for a moment. It’s sure to be on RetroHack‘s anthology album, coming someday to a YouTube playlist near you. Enjoy.

Direct link for rss and email subscribers…http://www.youtube.com/watch?v=NbhXmSBlS_U

You might also enjoy:

  1. Fixing 530 Home directory not accessible
  2. net user command reference and examples
  3. howto://create user mailboxes with powershell
  4. howto://rename a user’s profile

Leave a Comment

Previous post:

Next post: