Guest User

Untitled

a guest
Jan 21st, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.17 KB | None | 0 0
  1. On Error Resume Next
  2. 'Save as a file with the .vbs extension such as printerConversion.vbs
  3. 'Script written by Daniel Westendorf - daniel at prowestech dot com. It is provided to be used and minipulated at
  4. 'will, but with any expressed warranty as to what it will or will not do. No support is provided with this script.
  5.  
  6. 'Usage:
  7. 'Save as a file with the .vbs extension such as printerConversion.vbs
  8. 'Replace PATHTTOPRINTERMAPPINGCSVFILE with the real path to your mapping file. It should be in a location accessible
  9. 'by any machine such as \\domanNAME\NETLOGON
  10.  
  11. Dim objNetwork
  12. Dim strComputer
  13. Dim objWMIService
  14. Dim objPrinter
  15. Dim colInstalledPrinters
  16. Dim defaultPrinter
  17. Dim fs
  18. Dim csvFile
  19. Dim logFile
  20. Dim printerArray
  21. Dim printerChange
  22. Dim i
  23.  
  24. printerArray = Array()
  25.  
  26. set fs = CreateObject("Scripting.FileSystemObject")
  27. set csvFile = fs.OpenTextFile("PATHTTOPRINTERMAPPINGCSVFILE") 'Path to CSV file with format of \\OLDPRINTSERVER\PRINTER, \\NEWPRINTSERVER\REPLACEMENTPRINTQUEUE
  28.  
  29.  
  30.  
  31. i = 0
  32. 'Read CSV file and create MultiDimensional Array
  33. Do Until csvFile.AtEndOfStream
  34. ReDim Preserve printerArray(i)
  35.  
  36. printerChange = split(csvFile.Readline, ",") 'array of old, new printers, split by the comma
  37.  
  38. printerArray(i) = Array(printerChange(0), printerChange(1)) 'add printerChange to printerArray array at the next open slot
  39. i = i + 1
  40. Loop
  41.  
  42. csvFile.Close
  43.  
  44. 'Create or open logfile, write date
  45. If fs.FileExists("C:\PrinterSwap.log") Then
  46. Set logFile = fs.OpenTextFile("C:\PrinterSwap.log", 8)
  47. Else
  48. Set logFile = fs.CreateTextFile("C:\PrinterSwap.log", 2)
  49. End If
  50. LogFile.WriteLine ("************************")
  51. logFile.WriteLine ("------------------------")
  52. logFile.WriteLine (Now)
  53. logFile.WriteLine ("------------------------")
  54.  
  55. strComputer = "."
  56.  
  57. Set objNetwork = CreateObject("WScript.Network")
  58.  
  59. Set objWMIService = GetObject("winmgmts:" _
  60. & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  61. Set colInstalledPrinters = objWMIService.ExecQuery _
  62. ("SELECT * FROM Win32_Printer")
  63. For Each objPrinter in colInstalledPrinters 'loop through printers installed on system
  64. For Each printerChange in printerArray 'for each printer, loop through the printerArray searching for a match
  65. if LCase(objPrinter.Name) = LCase(printerChange(0)) Then 'check for match
  66. objPrinter.Delete_
  67. If Err.Number <> 0 Then
  68. logFile.WriteLine ("Removed: " & objPrinter.Name)
  69. Else
  70. logFile.WriteLine ("Error removing printer: " & objPrinter.Name)
  71. End If
  72. Err.Clear
  73. objNetwork.AddWindowsPrinterConnection(printerChange(1)) 'create new printer connection
  74. If Err.Number <> 0 Then
  75. logFile.WriteLine ("Added: " & printerChange(1))
  76. Else
  77. logFIle.WriteLine ("Error adding printer: " & printerChange(1))
  78. End If
  79. Err.Clear
  80. if objPrinter.Default = "True" Then 'was that printer the default printer? Will only return true once.
  81. defaultPrinter = printerChange(1)
  82. End If
  83. Err.Clear
  84. End If
  85. Next
  86. Next
  87.  
  88. If defaultPrinter <> Empty Then
  89. objNetwork.SetDefaultPrinter(defaultPrinter) 'set the default printer
  90. If Err.Number <> 0 Then
  91. logFile.WriteLine ("Set Default printer: " & defaultPrinter)
  92. Else
  93. logFile.WriteLine ("!!!Couldn't set default printer!!!")
  94. End If
  95. End If
  96.  
  97. LogFile.WriteLine ("************************")
  98. logFile.Close
Add Comment
Please, Sign In to add comment