[green]' =======================================================================
'
' NAME: UpdatePrintQueues-v1b.vbs
'
' AUTHOR: Paul S. Chapman
' DATE : 7-5-2006
'
' COMMENT: This script was built to allow administrators to migrate print
' queues from one server to another with no disruption to the end user.
' Uses RegistryItemExists function FAQ'd by djhawthorne on tek-tips:
' FAQ329-5864
'==========================================================================[/green]
Option Explicit
Dim strFlagsOn, arrDefault, strDefaultPrinter
Dim objShell, objNetwork, objDict
Const strDPKey = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
[green]' Check if command line argument "1" is passed to script. Enable verbose
' mode if argument is present.[/green]
If WScript.Arguments.Count = 0 Then
strFlagsOn = False
Elseif WScript.Arguments.Item(0) = "1" Then
strFlagsOn = True
End If
Set objNetwork = CreateObject("WScript.Network")
Set objShell = CreateObject("WScript.Shell")
Set objDict = CreateObject("Scripting.Dictionary")
[green]' Log script start time[/green]
objShell.LogEvent 4,"Update Printers Script Starting"
[green]' Check if Default printer registry key exists. If not then quit
' because this is the first time the user has logged in.[/green]
If RegItmExist(strDPKey) = False Then
If strFlagsOn = True Then
WScript.Echo "New User Profile. Quitting..."
End If
WScript.Quit
End If
[green]' Read Registry key that contains default printer into an array[/green]
arrDefault = Split(objShell.RegRead(strDPKey), ",")
[green]' Verify that an array was returned, then check if default printer is network printer[/green]
If IsArray(arrDefault) Then
If Left(arrDefault(0), 2) = "\\" Then
strDefaultPrinter = arrDefault(0)
If strFlagsOn = True Then
WScript.Echo "Default Printer: " & strDefaultPrinter
End If
End If
End If
[green]' Populate Dictionary object with old and new printer names. Old printer
' is the key, and new printer is the item (or definition).
' Syntax:
' .Add "\\<server>\<Old Queue Name>", "\\<server>\<New Queue Name>"
' ********** BEGIN EDIT AREA **********[/green]
With objDict
.CompareMode = vbtextcompare
.Add "\\OldServer\OldQueue1", "\\NewServer\NewQueue1"
.Add "\\OldServer\OldQueue2", "\\NewServer\NewQueue2"
End With
[green]' ********** END EDIT AREA **********
[green]' Call sub to fix printers that are currently installed[/green]
FixInstalled
[green]' Reset Default printer, if it was replaced by "FixInstalled" sub.[/green]
If objDict.Exists(strDefaultPrinter) Then
objNetwork.SetDefaultPrinter objDict.Item(strDefaultPrinter)
End If
[green]' Log script end time[/green]
objShell.LogEvent 4,"Update Printers Script Finished"
Sub FixInstalled
[green]' This sub enumerates the currently installed printers, then checks
' each one in turn against the Dictionary object to see if it has been
' replaced. If printer exists in dictionary, delete it and install the
' replacement printer.[/green]
Dim ctr, objPrinters, strPrinter
Set objPrinters = objNetwork.EnumPrinterConnections
[green]' Check if printer is installed before attempting to delete.[/green]
For Ctr = 0 To objPrinters.Count - 1 Step 2
If objDict.Exists(objPrinters.Item(ctr + 1)) Then
strPrinter = objPrinters.Item(ctr + 1)
If strFlagsOn = True Then
WScript.Echo " Removing dead printer:" & strPrinter
End If
objNetwork.RemovePrinterConnection strPrinter
If strFlagsOn = True Then
WScript.Echo " Installing: " & objDict.Item(strPrinter)
End If
objNetwork.AddWindowsPrinterConnection objDict.Item(strPrinter)
End If
Next
End Sub
Function RegItmExist (RegistryItem)
' This function checks to see if registry key/value passed to it exists,
' and returns true if it does.
If strFlagsOn = True Then
WScript.Echo " Checking registry for: " & RegistryItem
End If
Dim ErrDescription
On Error Resume Next
'Find out if we are looking for a key or a value
If (Right(RegistryItem, 1) = "\") Then
'It's a registry key we are looking for. Try reading the key.
objShell.RegRead RegistryItem
Select Case Err
Case 0:
RegItmExist = True
Case &h80070002:
ErrDescription = Replace(Err.description, RegistryItem, "")
Err.clear
objShell.RegRead "HKEY_ERROR\"
If (ErrDescription <> Replace(Err.description, "HKEY_ERROR\", "")) Then
RegItmExist = True
Else
RegItmExist = False
End If
Case Else:
RegItmExist = False
End Select
Else
'It's a registry value we are looking for. Try reading the value
objShell.RegRead RegistryItem
'Catch the Error
Select Case Err
Case 0:
RegItmExist = True
Case Else
RegItmExist = False
End Select
End If
'Turn error reporting back On
On Error Goto 0
If strFlagsOn = True Then
WScript.Echo " Item exists: " & RegItmExist
End If
End Function