Please upgrade your web browser now. Internet Explorer 6 is no longer supported.
Zac Smith
SharePoint, WSS and MOSS development.

Migrating SharePoint Users with PowerShell

by Zac Smith 29-May-07, 0 Comments

So I have finally jumped on the PowerShell bandwagon and created my first (useful) script. I had a need to migrate a bunch of users from local machine accounts to domain accounts. So I had a look at what stsadm commands were available and noticed enumusers and migrateusers. I figured I would be able to create a little PowerShell script that went through all the users and migrated each one. I did have the advantage of knowing that the logins were the same on the local machine as they were on the domain. A more sophisticated migration might have some xml look-up file to migrate to new login names.

The command .exe -o enumusers -url http://wsstestsite will return an xml string of users from the site http://wsstestsite.
The xml looks something like:

<Users>
  <User>
     <Login>domain\user</Login>
     <Email>user@domain.com</Email>
     <Name>SharePoint User</Name>
  </User>
  <User>
  ...
  ...
</Users>

And the script I wrote looks something like this:

# extract all users into an xml file
[xml] $users = c:\"program files\common files\microsoft shared\web server extensions\12\bin\"stsadm.exe -o enumusers -url http://wsstestsite

#iterate through each user (note the dot notation for accessing nodes)
foreach($login in $users.Users.User)
{
# determine old and new login (using regular .Net string func)
$oldlogin = $login.Login
$newlogin = $login.Login.Replace("LocalMachine", "Domain")

# migrate user
c:\"program files\common files\microsoft shared\web server extensions\12\bin\"stsadm.exe -o migrateuser -oldlogin $oldlogin -newlogin $newlogin
}

So the code basically converts each user login from the machinename\login to domainname\login.
I realise that you could probably write this a lot cleaner and simpler, but it was just thrown together in 15mins and I wasn't too worried about being elegant.

If you haven't had a chance to try out PowerShell I highly recommend you have a look. You can download it from here: http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx
There are HEAPS of resources around the web on PowerShell so it's really easy to get started.

Categories:
0 responses so far:

     

Post a Comment:
Name:
URL:
Email:
Comments: