Option Explicit
'On Error Resume Next
Dim strComputer, domain, currentUser, sSID, VerBig, sacct
Dim dateMonth, dateDay, dateYear, endDate
Dim ntaccount, strNTAccount
Dim objWMI, colItems, objItem, OSystem
Dim user
Dim storeToPath, batchStoreToPath, oldStoreToPath
Dim verifyUserInfo, objAccount
Dim fileSys, batchStorePath, exportBatch
Dim objWMIService, intProcessID
Dim objFSO
Dim objReg, strKeyPath, regedit, colOperatingSystems, objOperatingSystem
const HKEY_LOCAL_MACHINE = &H80000002
Dim restore
'*******************************************************************************
' Prompts agent for the machine name of the users computer (IP address and
' localhost are NOT supported [yet!])
' Returns: strComputer
'*******************************************************************************
strComputer = InputBox("Computer Name or IP Address: ")
'*******************************************************************************
' Pulls the variables strComputer, domain, currentUser, sSID, and OSystem from
' the function GetUserInfo() to use in the rest of the script
' Returns: N/A
'*******************************************************************************
GetUserInfo strComputer, domain, currentUser, sSID, OSystem
'*******************************************************************************
' Calculates the date and formats it for use in renaming the user profile
' Returns: endDate
'*******************************************************************************
If Month(Date) < 10 Then
dateMonth = "0" & Month(Date)
Else
dateMonth = Month(Date)
End If
If Day(Date) < 10 Then
dateDay = "0" & Day(Date)
Else
dateDay = Day(Date)
End If
dateYear = Right(Year(Date), 2)
endDate = dateMonth & dateDay & dateYear
'*******************************************************************************
' Verifies the users information is correct, if not, it'll prompt the agent to
' enter the users domain and account name
' Returns: endDate
'*******************************************************************************
verifyUserInfo = Msgbox("Is the below user information correct?" & vbCr & vbCr _
& "Computer: " & strComputer & vbCr _
& "System OS: " & OSystem & vbCr & vbCr _
& "Domain: " & domain & vbCr _
& "Username: " & currentUser & vbCr _
& "SID: " & sSID, vbYesNo)
If verifyUserInfo = vbYes Then
Msgbox "You answered yes."
Else
domain = InputBox("User's Domain: ")
currentUser = InputBox("Username: ")
Set objAccount = GetObject("winmgmts:root/cimv2:Win32_UserAccount.Domain='" & domain & "'" & ",Name='" & currentUser & "'")
sSID = objAccount.SID
pullSID()
Wscript.Echo "Version No: " & VerBig & vbCr _
& "OS System: " & OSystem & vbCr _
& "User: " & domain & "\" & currentUser & vbCr _
& "SID: " & sSID
End If
setStoragePaths()
WScript.Echo "Date (MMDDRR): " & endDate & vbCr & "storeToPath: " & storeToPath & vbCr & "batchStoreToPath: " & batchStoreToPath & vbCr & "oldStoreToPath: " & oldStoreToPath
'*******************************************************************************
' Creates, runs, and removes the remote batch to back-up the Network Drivers,
' Printers, and PST mappings for the current user
' Returns: N/A
'*******************************************************************************
Set fileSys = CreateObject("Scripting.fileSystemObject")
Set batchStorePath = fileSys.GetFolder(batchStoreToPath)
Set exportBatch = batchStorePath.CreateTextFile("export.bat", True)
exportBatch.WriteLine("@ECHO OFF && SETLOCAL EnableDelayedExpansion")
exportBatch.WriteLine("")
exportBatch.WriteLine("ECHO Exporting data for '" & currentUser & "' with SID '" & sSID & "' to '" & storeToPath & "'...")
exportBatch.WriteLine("REG EXPORT HKU\" & sSID & "\Network " & Chr(34) & storeToPath & "\1.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT HKU\" & sSID & "\Printers " & Chr(34) & storeToPath & "\2.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT " & Chr(34) & "HKU\" & sSID & "\Software\Microsoft\Windows NT\CurrentVersion\Devices" & Chr(34) & " " & Chr(34) & storeToPath & "\3.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT " & Chr(34) & "HKU\" & sSID & "\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts" & Chr(34) & " " & Chr(34) & storeToPath & "\4.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT " & Chr(34) & "HKU\" & sSID & "\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles" & Chr(34) & " " & Chr(34) & storeToPath & "\5.reg" & Chr(34))
exportBatch.WriteLine("type NUL >>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\1.reg" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\2.reg" & Chr(34) & " | find /v " & Chr(34) & "Windows Registry Editor Version 5.00" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\3.reg" & Chr(34) & " | find /v " & Chr(34) & "Windows Registry Editor Version 5.00" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\4.reg" & Chr(34) & " | find /v " & Chr(34) & "Windows Registry Editor Version 5.00" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\5.reg" & Chr(34) & " | find /v " & Chr(34) & "Windows Registry Editor Version 5.00" & Chr(34) & ">>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\1.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\2.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\3.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\4.reg" & Chr(34))
exportBatch.WriteLine("del " & Chr(34) & storeToPath & "\5.reg" & Chr(34))
exportBatch.WriteLine("")
exportBatch.WriteLine("PAUSE && GOTO :EOF")
exportBatch.Close
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2:Win32_Process")
objWMIService.Create batchStoreToPath & "\export.bat", Null, Null, intProcessID
WScript.Sleep 1500
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile(batchStoreToPath & "\export.bat")
Wscript.Echo "Instruct the customer to log out of them machine." & vbCr & vbCr _
& "Click 'OK' to rename the profile, and if Win7 to remove the SID from the registry and reboot."
objFSO.MoveFolder batchStoreToPath, oldStoreToPath
'*******************************************************************************
' Removes the SID registry entry and restarts the machine if it is running Win7
' Returns: N/A
'*******************************************************************************
Select Case VerBig
Case "6.1"
Set objReg = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & sSID
regedit = objReg.DeleteKey(HKEY_LOCAL_MACHINE, strKeyPath)
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colOperatingSystems = objWMI.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
' objOperatingSystem.Reboot()
Next
End Select
Wscript.Echo "Instruct the customer to log in and wait for them to acknowledge that the Desktop has finished loading." & vbCr & vbCr _
& "Click 'OK' to copy the files from the old to new profile, restore the network drives, network printers, and Microsoft Outlook PSTs."
Select Case VerBig
Case "6.1"
objFSO.CopyFolder oldStoreToPath & "\Contacts", batchStoreToPath & "\Contacts"
objFSO.CopyFolder oldStoreToPath & "\Desktop", batchStoreToPath & "\Desktop"
objFSO.CopyFolder oldStoreToPath & "\Downloads", batchStoreToPath & "\Downloads"
objFSO.CopyFolder oldStoreToPath & "\Favorites", batchStoreToPath & "\Favorites"
objFSO.CopyFolder oldStoreToPath & "\Links", batchStoreToPath & "\Links"
objFSO.CopyFolder oldStoreToPath & "\Documents", batchStoreToPath & "\Documents"
objFSO.CopyFolder oldStoreToPath & "\Music", batchStoreToPath & "\Music"
objFSO.CopyFolder oldStoreToPath & "\Pictures", batchStoreToPath & "\Pictures"
objFSO.CopyFolder oldStoreToPath & "\Videos", batchStoreToPath & "\Videos"
Case "5.1"1
objFSO.CopyFolder oldStoreToPath & "\Desktop", batchStoreToPath & "\Desktop"
objFSO.CopyFolder oldStoreToPath & "\Favorites", batchStoreToPath & "\Favorites"
objFSO.CopyFolder oldStoreToPath & "\My Documents", batchStoreToPath & "\My Documents"
End Select
objWMIService.Create "regedit /s " & Chr(34) & oldStoreToPath & "\Network Mappings.reg" & Chr(34), Null, Null, intProcessID
'objFSO.DeleteFile(oldStoreToPath & "\Network Mappings.reg")
'*******************************************************************************
' Function: GetUserInfo()
' Pulls the information for the current user of the defined machine either from
' the remote machine or the domain, and uses the version indicator to
' define if the OS and WinXP or Win7
' Returns: domain, currentUser, OSystem
'*******************************************************************************
Function GetUserInfo(strComputer, ByRef domain, ByRef currentUser, ByRef sSID, ByRef OSystem)
On Error Resume Next
'*******************************************************************************
' We're pulling the username of the current user from the defined machine and
' splitting it at the "\" to produce the domain name and the user name,
' then we call the pullSID() sub for the SID
' Returns: domain, currentUser
'*******************************************************************************
Set ntaccount = GetObject("winmgmts://" & strComputer & "/root/cimv2:Win32_ComputerSystem.Name='" & strComputer & "'")
strNTAccount = ntaccount.username
sacct = Split(strNTAccount,"\")
domain = sacct(0)
currentUser = sacct(1)
pullSID()
'*******************************************************************************
' We're pulling the operating system version from the defined machine
' Returns: OSystem
'*******************************************************************************
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)
For Each objItem in colItems
VerBig = Left(objItem.Version,3)
Next
Select Case VerBig
Case "6.1"
OSystem = "7"
Case "5.1"
OSystem = "XP"
Case Else OSystem = "Invalid OS"
End Select
End Function
'*******************************************************************************
' Sub: pullSID()
' Prompts the domain for the users SID
' Returns: sSID
'*******************************************************************************
Sub pullSID
Set user = GetObject("winmgmts:root/cimv2:Win32_UserAccount.Domain='" & domain & "',Name='" & currentUser & "'")
sSID = user.SID
End Sub
'*******************************************************************************
' Sub: setStoragePaths()
' Uses the OSystem to define the paths used for writing in, saving to, running
' from, and finally removing the remote batches. This sub also uses the
' strComputer name to create a variation, as needed, for
' Returns: storeToPath, batchStoreToPath, oldStoreToPath
'*******************************************************************************
Sub setStoragePaths
' Spot VerBig variable in previous section
' Note the output variable is called storeToPath
Select Case VerBig
Case "6.1"
storeToPath = "C:\Users\" & currentUser
Select Case strComputer
Case ".", "localhost", "127.0.0.1"
batchStoreToPath = storeToPath
oldStoreToPath = "C:\Users\old." & currentUser & "." & endDate
Case Else
batchStoreToPath = "\\" & strComputer & "\c$\Users\" & currentUser
oldStoreToPath = "\\" & strComputer & "\c$\Users\old." & currentUser & "." & endDate
End Select
Case "5.1"
storeToPath = "D:\Documents and Settings\" & currentUser
Select Case strComputer
Case ".", "localhost", "127.0.0.1"
batchStoreToPath = storeToPath
oldStoreToPath = "D:\Documents and Settings\old." & currentUser & "." & endDate
Case Else
batchStoreToPath = "\\" & strComputer & "\d$\Documents and Settings\" & currentUser
oldStoreToPath = "\\" & strComputer & "\d$\Documents and Settings\old." & currentUser & "." & endDate
End Select
End Select
End Sub