-1

We had a Windows 2012 R2 server on a VPS hosting with Terminal services installed and about 40 active users. Recently we encountered a system crash without any possibility to restore, so we managed to backup all user data and decided to reinstall the system.

Is there any automated way to grab user data and create user profiles not to do it manually? Some WSH script, migration tool, whatever. Let's say, read directory content, create a user per profile with default password, and then copy all their desktop environments, settings, documents etc

I'm not very familiar with the Windows administering, thank you for understanding.

If possible I'd appreciate some script code snippet

  • 3
    Possible duplicate of [Custom handling of domain user profile creation](http://serverfault.com/questions/254192/custom-handling-of-domain-user-profile-creation) – user692942 Sep 16 '16 at 09:44

1 Answers1

0

Solved for my own. Sorry for not well-commented script, hope help to somebody

Creates users according to backed up user directory (process exceptions like Default User, Administrator etc), create home directories for them, migrate backed up user directories.

Const WAIT_ON_RETURN = True
Const HIDE_WINDOW = 0
Const USER_ROOT_UNC = "C:\Users" 'Set Home Folder Location Here
On Error Resume Next

Function CreateUser(strUser, strPass)

    Set objShell = CreateObject("Wscript.Shell")
    Set objEnv = objShell.Environment("Process")
    strComputer = objEnv("COMPUTERNAME")

    Set colAccounts = GetObject("WinNT://" & strComputer & ",computer")

    Set objUser = colAccounts.Create("user", strUser)
    objPasswordNoChangeFlag = objUser.UserFlags XOR ADS_UF_PASSWD_CANT_CHANGE
    objUser.Put "userFlags", objPasswordNoChangeFlag 
    objUser.SetInfo

End Function

Function CreateCatalog(strUser)
    Dim WshShell, WshNetwork, objFS, objServer, objShare

    Set WshShell = Wscript.CreateObject("Wscript.Shell")
    Set WshNetwork = WScript.CreateObject("WScript.Network")
    Set objFS = CreateObject("Scripting.FileSystemObject")
    Call objFS.CreateFolder(USER_ROOT_UNC & "\" & strUser)
    Call WshShell.Run("cacls " & USER_ROOT_UNC & "\" & strUser & " /e /g Administrators:F", HIDE_WINDOW, WAIT_ON_RETURN)
    Call WshShell.Run("cacls " & USER_ROOT_UNC & "\" & strUser & " /e /g " & strUser & ":C", HIDE_WINDOW, WAIT_ON_RETURN)
End Function

Function CopyDirs(userName)
    arrCopyDirs = Array("Desktop", "Documents", "Downloads", "Links", "Pictures", "Videos", "Music")
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    For Each dirName in arrCopyDirs
        currentDirectory = objFSO.GetAbsolutePathName(".")
        copiedDirectory = currentDirectory & "\" & userName & "\" & dirName
        destDirectory = USER_ROOT_UNC & "\" & userName & "\" & dirName
        objFSO.CopyFolder copiedDirectory, destDirectory
    Next
End Function

Function EnumerateCatalog()
    Dim objFSO, objFolder
    Dim arrNotUsers, arrCopyDirs
    arrNotUsers = Array("Default User","MediaAdmin$","Administrator","MSSQL$MICROSOFT##WID","All Users","Plesk Administrator","Default","Public","ServerAdmin$")

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    For Each objFolder In objFSO.GetFolder(".").SubFolders

        If Not Ubound(Filter(arrNotUsers, objFolder.Name)) > -1 Then
            Call CreateUser(objFolder.Name, "12345qweasdzxc")
            Call CreateCatalog(objFolder.Name)
            Call CopyDirs(objFolder.Name)

        End If

    Next
End Function

Call EnumerateCatalog()