任何自动恢复用户配置文件的方法

任何自动恢复用户配置文件的方法

我们在 VPS 主机上安装了 Windows 2012 R2 服务器,安装了终端服务,大约有 40 个活跃用户。最近我们遇到了系统崩溃,无法恢复,所以我们设法备份了所有用户数据,并决定重新安装系统。

有没有自动获取用户数据并创建用户配置文件的方法,而不是手动操作?一些 WSH 脚本、迁移工具等等。比如说,读取目录内容,使用默认密码为每个配置文件创建一个用户,然后复制他们的所有桌面环境、设置、文档等

我对Windows管理不太熟悉,感谢您的理解。

如果可能的话,我希望得到一些脚本代码片段

答案1

我自己解决了。抱歉脚本注释不充分,希望对大家有帮助

根据备份的用户目录创建用户(处理默认用户、管理员等例外情况),为他们创建主目录,迁移备份的用户目录。

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()

相关内容