Advertisement
duck__boy1981

Restore Nicknames File

Jun 13th, 2012
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '****************************************************************************
  2. '* Script for restoring a users 'Nickname File' file for MS Outlook         *
  3. '****************************************************************************
  4.  
  5. Option Explicit
  6. Dim user_name       ' The user name of the currently logged in user
  7. Dim objNetwork      ' The network
  8. Dim objFSO          ' The File System Object
  9. Dim i, j, k         ' Dummies for looping
  10.  
  11. Set objNetwork = CreateObject("WScript.Network")        ' Create the network object (used for getting the username)
  12. Set objFSO = CreateObject("Scripting.FileSystemObject") ' Create the FSO object
  13. user_name = objNetwork.UserName
  14.  
  15. Dim servers(3)                  ' The terminal servers to action
  16.  
  17. ' Folder path variables
  18. Dim old_nicknames_file_path     ' The file path of the old nicknames file for individuals
  19. Dim new_profile_file_path       ' The file path of the new profile for individuals
  20. Dim new_nicknames_file_path     ' The file path of the new nicknames file for individuals
  21.  
  22. ' Mapped drive variables
  23. Dim drive_letters(1)            ' The mapped drive letters to be used
  24. Dim shares(1)                   ' The shares that are to be mapped
  25. Dim location                    ' The server location of all shared drives
  26. Dim force, update_profile       ' Booleans for setting options for removing mapped drives
  27. Dim mapped_drives               ' The drives that are have been mapped
  28.  
  29. ' Server copy variables
  30. Dim server_copy_list            ' A list of the servers that a users nicknames files have been copied to
  31. Dim server_copy_count           ' A count of the servers that have had nicknames files copied to them
  32.  
  33. ' Text output variables
  34. Dim output_message              ' The text string to display to the user after the process is complete
  35. Dim debug_text                  ' Dummy for outputting which folders exist when a user attempts to restore their nicknames files
  36.  
  37. servers(0) = "ttsa"
  38. servers(1) = "ttsb"
  39. servers(2) = "ttsc"
  40. servers(3) = "ttsd"
  41.  
  42. server_copy_count = 0
  43. debug_text = "Nickname files debug check for user '" & user_name & "'" & VBCr & VBCr
  44.  
  45. ' Set the drive letters that are to be used for mapping (should match with the shared folder to be mapped)
  46. drive_letters(0) = "J:"
  47. drive_letters(1) = "K:"
  48.  
  49. force = true            ' Remove mapped drive even if still in use
  50. update_profile = true   ' Update the users profile so that the mapping is permanently removed, not just for this session
  51.  
  52. ' For each server, copy the users old nicknames file (if possible)
  53. for i = 0 to UBound(servers) step 1
  54.    
  55.     ' Set the server loaction of the shares
  56.     location = "\\" & servers(i) & "\"
  57.    
  58.     ' Set the shares that are to be mapped (should match with the letter to be mapped) (does replicate the function of mapping the full UNC paths)
  59.     shares(0) = location & "d$\__OLD-PROFILES"
  60.     shares(1) = location & "d$\Documents and Settings"
  61.    
  62.     ' Map some network drives so that we can make a connection with the relevent credentials
  63.     for j = 0 to UBound(drive_letters) step 1
  64.         if (objFSO.DriveExists(drive_letters(j)) = true) then ' Check to see if the network drive is mapped, and remove it if it is
  65.             objNetwork.RemoveNetworkDrive drive_letters(j), force, update_profile
  66.         end if     
  67.         objNetwork.MapNetworkDrive drive_letters(j), shares(j), false, "{my_username}", "{my_password}"
  68.     next
  69.    
  70.     ' Debugging
  71.     ' Loop through all of the the drives that are mapped and add them to the debug string
  72.     debug_text = debug_text & "Results for server '" & servers(i) & "'" & VBCr
  73.     Set mapped_drives = objNetwork.EnumNetworkDrives
  74.     For k = 0 To mapped_drives.Count - 1 Step 2
  75.         For j = 0 To UBound(drive_letters) Step 1
  76.             If UCase(drive_letters(j)) = UCase(mapped_drives.Item(k)) Then
  77.                 debug_text = debug_text & mapped_drives.Item(k) & "\ was mapped to " & mapped_drives.Item(k+1)  & VBCr
  78.             End If
  79.         Next
  80.     Next
  81.    
  82.     ' Set the paths to the old and new nicknames folders, and to the users new profile folder
  83.     old_nicknames_file_path = "J:\" & user_name & "\Application Data\Microsoft\Outlook"
  84.     new_profile_file_path = "K:\" & user_name & "\"
  85.     new_nicknames_file_path = "K:\" & user_name & "\Application Data\Microsoft\Outlook"
  86.    
  87.     ' Check to see if an old version of the folder containing the nickname file exists
  88.     if objFSO.FolderExists(old_nicknames_file_path) then
  89.    
  90.         ' Debugging
  91.         debug_text = debug_text & " - Old nicknames folder exists" & VBCr
  92.    
  93.         ' Check to see if a new profile on this server exists, if not, no logon to that server has yet occured, so the nicknames file will not be copied
  94.         if objFSO.FolderExists(new_profile_file_path) then
  95.        
  96.             ' Debugging
  97.             debug_text = debug_text & " - A new profile exists" & VBCr
  98.            
  99.             ' Check that the folder containing the nickname file exists in the new profile
  100.             if objFSO.FolderExists(new_nicknames_file_path) then
  101.                
  102.                 ' Debugging
  103.                 debug_text = debug_text & " - New nicknames folder exists" & VBCr
  104.                
  105.                 ' Check that the nicknames file exists in the users old profile and copy it across
  106.                 if objFSO.FileExists(old_nicknames_file_path & "\Outlook.NK2") then
  107.                
  108.                     ' Debugging
  109.                     debug_text = debug_text & " - New nicknames file exists" & VBCr
  110.                
  111.                     ' Update the server copy list and the count of servers updated
  112.                     server_copy_list = server_copy_list & servers(i) & VBCr
  113.                     server_copy_count = server_copy_count + 1
  114.                    
  115.                     ' Do the magic and copy the nicknames file
  116.                     objFSO.CopyFile old_nicknames_file_path & "\Outlook.NK2", new_nicknames_file_path, true
  117.                     objFSO.CopyFile old_nicknames_file_path & "\Outlook.NK2", old_nicknames_file_path & "\BACKUP_Outlook.NK2", true
  118.                
  119.                 end if
  120.            
  121.             end if
  122.            
  123.         end if
  124.        
  125.     end if
  126.            
  127.     ' Debugging
  128.     debug_text = debug_text & VBCr
  129.    
  130. next
  131.  
  132. ' Unmap the temporary drives that we have just mapped
  133. for j = 0 to UBound(drive_letters) step 1
  134.     if (objFSO.DriveExists(drive_letters(j)) = true) then ' Check to see if the network drive is mapped, and remove it if it is
  135.         objNetwork.RemoveNetworkDrive drive_letters(j), force, update_profile
  136.     end if
  137. next
  138.  
  139. ' Output a message to the user to confirm that their nicknames files have been copied
  140. if server_copy_count = 0 then ' No facourites copied as none exist on old profile
  141.     output_message = "No nicknames files were found on your old profile, so none could be copied."
  142. else
  143.     output_message = "Your Microsoft Outlook nicknames files have been copied to the" & VBCr _
  144.     & "following servers -" & VBCr & VBCr & server_copy_list
  145.     if server_copy_count <> 4 then ' Warn that not all nicknames files have been copied, but that they will replicate
  146.         output_message = output_message & VBCr & "Although your nicknames files could not be copied to all servers" & VBCr _
  147.         & "(because you have not logged on to all of them), they will be copied" & VBCr _
  148.         & "the next time you log on."
  149.     end if
  150. end if
  151.  
  152. Wscript.Echo output_message
  153.  
  154. Wscript.Echo debug_text
  155.  
  156. '****************************************************
  157. '* Reset the objects and exit                       *
  158. '****************************************************
  159.  
  160. set objNetwork = nothing
  161. set objFSO = nothing
  162.  
  163. WScript.Quit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement