Hi Rob here again. I recently had a customer that needed the functionality of MoveUser.exe from the Windows 2000 Resource Kit available in Windows Vista. The customer had quite a few Windows Vista machines that were not joined to the domain but were now migrating to Active Directory. For their own business reasons they were previously unable to join the machines to the domain, instead all the users logged on with local user accounts. Since they had this new fancy Active Directory they created user accounts in AD for the users and joined the machines to the domain. They then found that they needed a way to attach the users’ existing local profile to their Active Directory user accounts so that the users would have their normal setup and desktop when they logged in-a seamless experience. Now if you have been around forever like most of us here in support in Windows 2000 and up we used a utility named MoveUser.exe to accomplish this.
Well, in Windows Vista moveuser.exe is no longer supported. However, now we expose this functionality with a new WMI provider called Win32_UserProfile, which is discussed in KB930955 . This is awesome because we expose things about user profiles in WMI now… and we can also move the profile to another user as well as delete user profiles. However, once I started looking at MSDN to understand what methods were available I quickly found that MSDN has not been updated as of yet for this new class. So we did some digging into the source code to find out how this works and what is supported.
I wrote a sample script that illustrates how you can leverage this provider to move an existing user profile to another user’s profile. I know that I could have made the script smaller by not listing out all the different properties available in the provider, but the different things exposed are just way too many and if you are planning on using this provider they are just way too cool.
Usage
Please keep in mind that this is a sample script-you will need to alter it and test it in your environment and for your needs. To use:
1. Copy the below script into Notepad then save as moveuser.vbs
2. You will need to modify the following variables within the script.
- strComputer : The computer name that this script needs to run against
- strSourceAcct : The user account that has the source profile on the system
- strSourceAcctDomain : The domain of the source user account that the profile belongs to. If the source account that you want to move the profile from is a local computer user you put in the computers name for the domain. If this is another domain then you type in the domain name.
- strTargetAcct : The user account that the source profile should be moved to.
- strTargetDomain : The domain of the target user account that the profile should be moved to. If the target account that you want to move the profile to is a local computer user you put in the computers name. If this is another domain then you type in the domain name.
- strDomainDN : The Target Account Domains Distinguished Name. This is done for the LDAP query to be built to find the target accounts SID. for example dc=contoso,dc=com
3. Run the script by typing cscript moveuser.vbs
Sample Script
' This script is provided "AS IS" with no warranties, and confers no rights.
' For more information please visit ' http://www.microsoft.com/info/cpyright.mspx to find terms of use. ' Option Explicit DIM strComputer, strSourceAcct, strSourceAcctDomain, strTargetAcct DIM strTargetAcctDomain, strTargetAcctSID DIM objProfile, objCommand, objRecordSet, objConnection, objWMIService, objSID DIM dtStart, colProfiles, oSID, oUsr DIM Revision, IssueAuthorities(11), strSDDL, subAuthorities DIM strDomainDN
CONST ADS_SCOPE_SUBTREE=2
' This script has hard coded variables in it that must be filled out.
strComputer ="."
' We need the proper Active Directory domain name where the user exists in a DN format. You can
objCommand.CommandText = _
objRecordSet.MoveNext
objConnection.Close
Set objWMIService = GetObject("winmgmts:\" & strComputer &"rootcimv2")
' Testing to verify that the current profile handle is for the Source Account that we want to
' Flag 1 = Change ownership of the source profile to target account
' Flag 2 = Delete the target account Profile and change ownership
' To use the ChangeOwner method, both the source and
ObjProfile.ChangeOwner strTargetAcctSID,1
end sub
function SDDL_SID ( oSID )
|
Please keep in mind if you have not installed Service Pack 1 for Vista, you will need to download the MSI installer to get the new
WMI Profile provider
since it was released after Vista shipped.
Well, I hope that you find this functionality helpful. Happy scripting.
- Rob Greene