Option Explicit
Dim strComputer, objWMI
Dim colItems, objItem, VerBig
Dim colItems2, objItem2, WSHShell
Dim OSystem, domain, currentUser, arrName
Dim objAccount, SID
Dim dateMonth, dateDay, dateYear, endDate
Dim storeToPath, batchStoreToPath, oldStoreToPath
Dim verifyUserInfo
Dim fileSys, batchStorePath, exportBatch
Dim objWMIService, intProcessID
Dim objFSO
Dim oldStorePath, renameBatch
' Here is where we interrogate the Operating System
' On Error Resume Next
' Get the computer name (ie . = localhost)
strComputer = InputBox("Computer Name or IP Address: ")
' Remote connection path
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
' This is where WMI interrogates the operating system
Set colItems = objWMI.ExecQuery("Select * from Win32_OperatingSystem",,48)
' Here we filter Version from the dozens of properties
For Each objItem in colItems
VerBig = Left(objItem.Version,3)
Next
' Spot VerBig variable in previous section
' Note the output variable is called OSystem
' This is where WMI interrogates the computer system
Set colItems2 = objWMI.ExecQuery("Select * from Win32_ComputerSystem",,48)
Set WSHShell = CreateObject("WScript.Shell")
Select Case VerBig
Case "6.1"
OSystem = "7"
domain = WshShell.ExpandEnvironmentStrings("%userdomain%")
currentUser = WshShell.ExpandEnvironmentStrings("%username%")
Case "5.1"
OSystem = "XP"
' Here we filter UserName from the dozens of properties
For Each objItem2 in colItems2
arrName = Split(objItem2.UserName, "\")
domain = arrName(0)
currentUser = arrName(1)
Next
Case Else OSystem = "Invalid OS"
End Select
Sub pullSID
' This is where WMI interrogates the user account
Set objAccount = GetObject("winmgmts:root/cimv2:Win32_UserAccount.Domain='" & domain & "'" & ",Name='" & currentUser & "'")
SID = objAccount.SID
End Sub
Call pullSID
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
Wscript.Echo endDate
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
Call setStoragePaths
verifyUserInfo = Msgbox("Is the below user information correct?" & vbCr & vbCr _
& "Version No: " & VerBig & vbCr _
& "OS System: " & OSystem & vbCr _
& "User: " & domain & "\" & currentUser & vbCr _
& "User SID: " & SID, vbYesNo)
If verifyUserInfo = vbYes Then
Msgbox "You answered yes."
Else
domain = InputBox("User's Domain: ")
currentUser = InputBox("Username: ")
Call pullSID
Call setStoragePaths
Wscript.Echo "Version No: " & VerBig & vbCr _
& "OS System: " & OSystem & vbCr _
& "User: " & domain & "\" & currentUser & vbCr _
& "User SID: " & SID
End If
' This is the creation of a batch file on the remote machine to export the registry entries for network mappings
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 '" & SID & "' to '" & storeToPath & "'...")
exportBatch.WriteLine("REG EXPORT HKU\" & SID & "\Network " & Chr(34) & storeToPath & "\1.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT HKU\" & SID & "\Printers " & Chr(34) & storeToPath & "\2.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT " & Chr(34) & "HKU\" & SID & "\Software\Microsoft\Windows NT\CurrentVersion\Devices" & Chr(34) & " " & Chr(34) & storeToPath & "\3.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT " & Chr(34) & "HKU\" & SID & "\Software\Microsoft\Windows NT\CurrentVersion\PrinterPorts" & Chr(34) & " " & Chr(34) & storeToPath & "\4.reg" & Chr(34))
exportBatch.WriteLine("REG EXPORT " & Chr(34) & "HKU\" & SID & "\Software\Microsoft\Windows NT\CurrentVersion\Windows Messaging Subsystem\Profiles" & Chr(34) & " " & Chr(34) & storeToPath & "\5.reg" & Chr(34))
exportBatch.WriteLine("echo.>>" & Chr(34) & storeToPath & "\Network Mappings.reg" & Chr(34))
exportBatch.WriteLine("type " & Chr(34) & storeToPath & "\1.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 & "\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.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."
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.MoveFolder batchStoreToPath, oldStoreToPath
Select Case VerBig
Case "6.1"
Set oldStorePath = fileSys.GetFolder(oldStoreToPath)
Set renameBatch = oldStorePath.CreateTextFile("rename.bat", True)
renameBatch.WriteLine("reg delete " & Chr(34) & "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & SID & Chr(34) & " /f")
renameBatch.WriteLine("shutdown -f -r -t 00")
renameBatch.Close
objWMIService.Create oldStorePath & "\rename.bat", Null, Null, intProcessID
End Select
WScript.Quit
' End of script