VBScript to Remap your Outlook Profiles to a new server.

Recently recovered from an incident involving the slow and painful death of a MS Exchange Server. Right smack in the middle of the day, with some 800 mailboxes on that one host. We restored the mail stores to another Exchange Server, and ran a hack script to go and correct the homeMDB, homeMTA, and msExchMailServer attributes in the AD to point to the new host. That was the easy part. Then we had all these users with outlook profiles pointing to a dead server. Outlook may be resilient in picking the right server when you point it to the wrong server, but if the wrong server (to which is is pointed) is not available – as in if the server it’s pointed at is DEAD, then it doesn’t know to get the new info from the AD to point you to the correct one. This, of course, is exactly what happened to me an my ilk. We had some hundreds of users without profiles pointing to the right host. You’ve heard of Autoconfig for Outlook 2007, but that only works for NEW profiles, not existing ones suddenly pointing to a dead host. So what to do? Manually have all the users repoint their profiles by talking them through the Mail Control Panel widgets? NO! That’s expensive. Instead, write a script and deploy it as a login script.

Why a Login script? Well, because this script affects the HKEY_CURRENT_USER registry hive which only exists for a particular user when that user is logged on. At least for all intents and purposes that’s true. I think it may be possible to somehow enumerate all users on the HKEY_USERS hive, but I didn’t go that far. I apologize up front for not going the extra mile on this one, but hey, look! A free script.

NOTE: If you copy and paste it, and get a compile error, check for abberant the new-line characters caused by the squishing of my code by the text rendering on the page.

'--------------------------------
'Script RepointProfile.vbs
'Author: Dave Dolan
'Purpose: Repoints exchange profiles to use NewServer instead of OldServer
'--------------------------------
'(Editable Constants) Change OldServer and NewServer below as appropirate for the task
'----------------------------
CONST OldServer = "OldMailHostName"
CONST NewServer = "NewMailHostName"
'----------------------------
' Command Line Usage:
'--------------------
' RepointProfile.vbs [/print:true|false]
' -- (optional) print parameter when set to 'true' displays what it's doing, otherwise it prints nothing
' -----------------------------------------------------------
' NOTE: VERY IMPORTANT: Run this in the context of the current user to change the profile settings,
'         since it affects HKEY_CURRENT_USER!
'----------------------------------------------------------------------


'--------- Edit NOTHING Below this line ----------------------

'if they have non-exchange profiles, they're gonna throw (harmless if skipped) errors. I care not.
on error resume next 

CONST NetBiosValue = "001e6602"
CONST FQDNValue = "001e6608"
CONST xFiveHundredValue = "001e6612"

Const HKEY_CURRENT_USER = &H80000001
printParam = Wscript.Arguments.Named("print")

if len(printParam) >= 0 then
  if ucase(printParam) = "TRUE" then 
     printValues = 1
  else
     printValues = 0
  end if
else
  printValues = 0
end if

computerName = "."

const BASE_KEY = "Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles"

set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & computerName & "\root\default:StdRegProv")

objReg.EnumKey HKEY_CURRENT_USER, BASE_KEY, arrSubKeys

for each subkey in arrSubKeys

if printValues = 1 then
  Wscript.Echo "Subkey = " & subkey
end if

subKeyPath = BASE_KEY & "\" & subkey & "\" & "13dbb0c8aa05101a9bb000aa002fc45a"


objReg.GetStringValue HKEY_CURRENT_USER, subKeyPath, NetBiosValue, nbServerName
objReg.GetStringValue HKEY_CURRENT_USER, subKeyPath, FQDNValue, fqdn
objReg.GetStringValue HKEY_CURRENT_USER, subKeyPath, xFiveHundredValue, X500

fqdn = ucase(fqdn)

if printValues = 1 then
 Wscript.Echo "Current Values"
 Wscript.Echo "--------------"
 Wscript.Echo "Netbios Value: " & nbServerName
 Wscript.Echo "FQDN Value: " & lcase(fqdn)
 Wscript.Echo "X500 Value: " & X500
end if

if instr(nbServerName, OldServer) then

  nbServerName = Replace(ucase(nbServerName), OldServer, NewServer)
  fqdn = lcase(Replace(fqdn, OldServer, NewServer))
  x500 = Replace(X500, OldServer, NewServer)

  objReg.SetStringValue HKEY_CURRENT_USER, subKeyPath, NetBiosValue, nbServerName
  objReg.SetStringValue HKEY_CURRENT_USER, subKeyPath, FQDNValue, fqdn
  objReg.SetStringValue HKEY_CURRENT_USER, subKeyPath, xFiveHundredValue, X500

  if printValues = 1 then
   Wscript.Echo ""
   Wscript.Echo "New Values"
   Wscript.Echo "--------------"
   Wscript.Echo "Netbios Value: " & nbServerName
   Wscript.Echo "FQDN Value: " & fqdn 
   Wscript.Echo "X500 Value: " & X500
  end if


end if

next

set objReg = nothing


You can follow any responses to this entry through the RSS 2.0 feed. You can skip to the end and leave a response. Pinging is currently not allowed.

28 comments on “VBScript to Remap your Outlook Profiles to a new server.

  1. Farkhad on said:

    This is a great!! I’ve been looking all over the net for this!! Thank you!!

  2. mohtaw on said:

    good work and well done really . also you said a pointy of restoring Exchange on other server can you share the steps with me my mail is mohtaw2@hotmail.com

  3. If you want to do it the normal way do this

    If you’d like to upgrade 2003 to 2007 in the process, restore your exhange 2003 db’s to a dummy server from whatever backup tool you have. Mount the db’s, and then move the mailboxes using the ‘move mailbox’ option, of all things to plunk them all down on the separately configured exchange 2007 box. I know it takes a lot of hardware, but it just so happens we had that available for us.

    I actually used an ADSI script to go through and change all of the people’s entries in the LDAP itself to remap the homeMDB, homeMTA, and msExchMailServer attributes to the NEW server (using a similar Replace() command as above.) I actually am not anywhere near that script, and I honestly don’t feel like writing it over again.

    I believe in helping people, especially helping people learn, but not continuously doing things for people at my own expense ;)

    Haha. Good luck.

  4. mohtaw on said:

    thanks alot for the info sahring . my case is the server contain the PDC and exchange 2003 . im planning for a full disaster recovery . i could so far restore the AD and im now searching how to restore the exchange . i back up the info store . so i imagine in case of a disaster i will have that backup only . so can you help me in this

  5. Hello, this looks like what i have been looking for, I migrating mailboxes to a new exchange server in a new domain (different forest), i have run this script and can see that it is updating the registry values, however when looking in the mail profile it still has the old server name. I have gone so far as to search the registry for any instance of the old server name and manually changed it. anyone have any ideas where else this could exist?

  6. If you’re using 2007, this hasn’t been tested on that. Also a possibility, this script is meant to replace the Server Name part of the strings only, not the fully qualified domain name, nor the distinguished name, both of which would differ in another forest. It’ll probably need a little tweaking for that. I know for a fact though that this is the registry key where all the outlook profiles for 2003 and below are stored, and it does indeed change them. In fact, Exchange profiles are given a Guid, so there is no ‘hard and fast’ way to find them. The way I get around that in this script is to look at all of the profiles, and check them for the right sub-key, which is indicative of Exchange. Then under there, and for every one, I search and replace the server name in all three values. So if you happened to have 6 profiles pointing to different mailboxes on the same server, for example, it migrates all of them.

  7. Dmitry Martinov (greensleeve) on said:

    Thank you Dave
    By the way, your hack script for correcting the homeMDB, homeMTA, and msExchMailServer attributes in the AD presents here http://technet.microsoft.com/en-us/library/bb125087(EXCHG.65).aspx

  8. I’m tinkering with this to pull up the new mailserver name from LDAP details – Using:
    objUser.msExchHomeServerName

    will bring up the full path (ie: /o=Company/ou=Exchange Administrative Group (ABCetc)/cn=configuration/cn=Servers/cn=WSMSG1234X

    but I only want the last portion of this – does anyone know of a different property that contains only this value, or how I can remove from the above and put into a new (.vbs) variable?

  9. Keith Davis on said:

    I have tried this, but it doesn’t work with Cached Exchange mode. Even in Online mode, it finally connected, but it took a long time and in Accounts it still says the old server.

    Has anyone gotten this to work on Cached Exchange Mode?

  10. Dave on said:

    honestly I haven’t tried this in a while, I don’t know how it works with 2007, and cached mode, I have not investigated as to whether or not this affects things. It actually takes a hard coded registry key name, so if they have added a new one for cached or a different permutation for 2007, I don’t know. At the time I wrote this script there was no office 2007. *shrug* Perhaps just check your registry to see if the provider keys might be different. Chances are they will be using Unicode, not Ascii, so if you want to mod the script that’s what to look for. I have the ‘key’ values as constants declared at the top of the script. Maybe there is a new one?

  11. Dave on said:

    oh and I almost forgot to mention, you have to re-home the mailboxes first in the AD accounts. This doesn’t touch AD accounts. Only the local profile. See this: http://technet.microsoft.com/en-us/library/bb125087(EXCHG.65).aspx for info. (hat tip to Dmitry Martinov above)

  12. Keith Davis on said:

    I guess because of the timing you assumed I was talking about 2007, but I am not. This is with Outlook 2003 and Exchange 2003 that it does not work. I’ve tried other similar scripts, and even changing the registry keys manually, but nothing works. It’s like Outlook writes those values, but does not read them.

  13. Ok, you’re still missing half of what I said. The primary source of information for Outlook is not on the local host at all. It gets the mailbox location from the attributes of your AD account (omeMDB, homeMTA, and msExchMailServer), ie from the Domain Controller. First you have to run a script to change that (not included here on this post, but the link I posted above is actually talking about the attributes in question.) So yes, you’re right, it’s not reading it as the only source of information. I don’t know why they have to be changed locally and on the AD accounts, but when I didn’t run this script also, changing them in the AD wasn’t sufficient. Anyway, the primary reason I posted this script was to show how one might do a find/replace on a unicode string in a registry key that’s a binary value type. The exchange thing was just something I used it for.

  14. Hello, I am a newish net admin at a school district. They are currently using gaggle.net with their mx record pointing to such and the instructors log in using outlook 2007.

    They are interested in hosting their own exchange server but I really dread going to every system and editing the mail server settings. Everyone is either on xp pro or vista business. Would the above script work out with this situation? It would be a new exchange install and I just need to save some effort in changing everyones classroom computers to use the local server rather than gaggle.net.

    Thank you,
    Dan.

  15. The only way to find out is to test it on a machine you don’t care if you break! I know it will do the edits to the registry, but whether or not that will do the trick is uncertain. I haven’t tried or tested it on 2007, and I’m actually kinda dubious of it. See the thing that gets me is that they have these apparently random looking numbers as the ‘keys’ for each features, like Exchange vs whatever else. I think it’s probably easy for you to see the reason that procedures like that are unsupported. I would never recommend deploying my code without making sure it works for you first, and perhaps more importantly, without your understanding exactly how it works. Things are all Powershell these days now, I’m already feeling old, and I’m not yet 30.

  16. Great script. Is there a way to change the whole x500 path as opposed to just the server name?

  17. Sure just whack the whole string instead. I’m doing a find/replace. NO warranty, do it at your own risk. Test it on someone you love first.

  18. Allan on said:

    I did a Exchange 2003 swing migration and for whatever reason (probably because I’m using a smart host and external DNS on my connectors) my Outlook clients refused to update. All my public folders synced and migrated, mailboxes moved fine, but Outlook refused to work. Threw this script into my login and it worked great. Only issue I had is my server name was SERVEREmail01 and when I typed it into the script I put in ServerEmail01 and the script didn’t match the case difference so I changed ti If logic to:

    if instr(ucase(nbServerName), UCase(OldServer)) then

    just in case it got entered differently on other computers (SERVEREmail01, SERVEREMAIL01, etc).

    Thanks for this!

  19. Dave on said:

    Glad to hear it, and yeah I guess I didn’t think about case sensitivity. Good catch!

  20. Brent Kennedy on said:

    Wow, good stuff! We just migrated to exchange 2007, i left the exchange 2003 box up for 3 months then took it down Thursday. Not two hours into Friday, i get a request saying they cant open outlook.

    I have dealt with this before, but never had a good way to re-point folks. Put this script in place, tested it and bam, no more complaints!

    Also, double whammy, i was helping a colleague with a dead exchange 2003 restore. They built a new server because the failed one was a DC/Exchange box. He was having hell on wheel with this issue, script saved him a lot of work!

    If you don’t mind, i would like to point people on experts exchange to this post. This question is asked tons on there.

    Thanks again!

  21. Jermaine Gersch on said:

    does this really work???

  22. yes

  23. i have the same case , however im not used for script , can any one advice if i need to change any values at the script or just copy and past and execute ?

    thanks

  24. yes change the server name constants at the top of the file. sorry it took me so long to approve your comment. it’s in the middle of a pile of a thousand spam comments.

  25. Have you tested this against Outlook 2007? The reg values look the same and are in the same place. Above you stated to just change the server names. We will be migrating from a 2003 to a 2007 exchange server but the Outlook client will still be 2003. So do you think this script will work for that with only the server name edits?

  26. Only one way to find out! Try it! I haven’t used or looked at this script since the day I published it, but I still get email from ‘happy non-customers’ who are using it once a month or so ever since. Good luck.

  27. Dave, if a user has multiple Outlook 2003 profiles, and none are set to default, ie Outlook is set to “always prompt”, which profile will this scrpt update. I’m hopiong all of them ?

  28. yeah. it’s a “for each” loop so it’ll match any of the sub-keys that it can.

    EDIT: I’d suggest you try it though before deploying it, of course.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

261,278 Spam Comments Blocked so far by Spam Free Wordpress

HTML tags are not allowed.