[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 412
  • Last Modified:

VBSCript to Set Default printer to New Server but same queue

I am planning on migration our print cluster which host 600+ printer. ALl printers are mapped via GPP an item-level targeting based on group membership.

Long story short I already have a script . Which was created with the help of E.E experts to delete old server mapping reads the old default printer then randomly sets a new network default printer.

How can i add a line to check the print queue value of the old default printer and try to match or partially match it to the new cluster and if none came be found one will be selected at random.

Here is my old posting regarding the orginal creation of this script
0
compdigit44
Asked:
compdigit44
  • 12
  • 6
  • 2
1 Solution
 
compdigit44Author Commented:
I forgot to mention the new print queues names are slightly different and . " I did not come up with the naming convention...

For example
old printer HP 4350 Rm432
New Rm 432 4350

or
old Xerox Rm123
new Rm123 Office
0
 
RobSampsonCommented:
Hi, unfortunately the most reliable way to do this is for you to build a "migration map", and have the script use that. I say unfortunate because it means you have to write a list of 600+ printers that replaced others. We did this as well, and it took a while, but was worth it for a seamless transition. What you would have in the "mapping" file is
\\oldserver\oldqueue1;\\newserver\newqueue1
\\oldserver\oldqueue 2;\\newserver\newqueue2
Etc;etc

And then have script read that, and the current default printer, then set the new matching default printer.

Rob.
0
 
Robberbaron (robr)Commented:
unless there is some consistency with renaming, then RobS method is only way.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
compdigit44Author Commented:
Wow the removal process for my old printer cluster and default printer selection is taking on a list of its own. Since I am not a VB coder by a long shot any good reference I could you to build a map file?
0
 
Robberbaron (robr)Commented:
it's just 2 lists... built by hand.

do you have the 2 lists or the two clusters that you could export the printer names from ?
0
 
compdigit44Author Commented:
When I was doing the migration in order to clean things up I did not import any printer to the new cluster but built all 620 one-by-one... In order to keep track of my process I created a spreadsheet list all and new queue names servers etc.... But I guess I could also export a list for both cluster since both are online. How would I associated one queue with another .Also I would want the script to fall back to selecting any network printer mapped for the user on the new cluster is for some reason a new queue does not match their old default
0
 
RobSampsonCommented:
In terms of associating one queue with another, that's up to your discretion, based on the knowledge you have of which printer replaces which.

For example, you know that you have replaced
\\OldServer1\HP 4350 Rm432
with
\\NewServer1\New Rm 432 4350

 or
\\OldServer1\Old Xerox Rm123
with
\\NewServer1\new Rm123 Office

So you have to map that, which you can in Excel, then save as a CSV, so it looks like this
[OLD PRINTER],[NEW PRINTER]
\\OldServer1\HP 4350 Rm432,\\NewServer1\New Rm 432 4350
\\OldServer1\Old Xerox Rm123,\NewServer1\new Rm123 Office

Then, we just use code like that below to read that CSV file, check the current default printer, and set the corresponding new one as the default.

Regards,

Rob.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
strMapFile = "C:\Temp\Scripts\PrinterMap.csv"
Set dctPrinters = CreateObject("Scripting.Dictionary")
dctPrinters.CompareMode = vbTextCompare
If objFSO.FileExists(strMapFile) = False Then
	WScript.Echo "File not found: " & strMapFile
Else
	Set objMapFile = objFSO.OpenTextFile(strMapFile, 1, False)
	While Not objMapFile.AtEndOfStream
		strLine = objMapFile.ReadLine
		If InStr(strLine, "\\") > 0 Then
			strOldPrinter = Split(strLine, ",")(0)
			strNewPrinter = Split(strLine, ",")(1)
			If dctPrinters.Exists(strOldPrinter) = False Then dctPrinters.Add strOldPrinter, strNewPrinter
		End If
	Wend
	objMapFile.Close
	Set objShell = CreateObject("WScript.Shell")
	strDefault = objShell.RegRead("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device")
	If strDefault <> "" Then
		strDefault = Left(strDefault, InStr(strDefault, ",") - 1)
		WScript.Echo strDefault
		If dctPrinters.Exists(strDefault) = True Then
			WScript.Echo "Changing default printer from " & strDefault & " to " & dctPrinters(strDefault)
			On Error Resume Next
			objNetwork.SetDefaultPrinter dctPrinters(strDefault)
			If Err.Number <> 0 Then WScript.Echo Err.Description
			Err.Clear
			On Error GoTo 0
		Else
			WScript.Echo "Unable to find default printer replacement for " & strDefault
		End If
	Else
		WScript.Echo "Unable to determine default printer."
	End If
End If

Open in new window

0
 
compdigit44Author Commented:
Thanks..

For the CSV does the new and old printer queue names need to be in different columns? Also if my queue names have spaces should I use quotes?
0
 
RobSampsonCommented:
Yes, they need to be in different columns.  If you list them in Excel, then do "Save As" CSV format, it will write the CSV with the comma in the middle, which is what you want.
You don't need quotes if you have spaces, but Excel WILL put quotes ONLY IF one of your share names has a comma in it.  Hopefully that's not the case, because I will need to change the script a bit to account for the quotes as the text qualifier.
For example, if you have
\\NewServer1\new Rm123 Office
in column B, it will be fine, but if you have
\\NewServer1\new, Rm123 Office
that has a comma after "new", let me know, because I'll need to change the script a bit.

Rob.
0
 
compdigit44Author Commented:
Thanks for the update. I am in the process of creating the reference CSV file now for all 650 printers!!!! ;o(

I am hoping this will work and if a default printer can not be found the script will restore to it current behavior of mapping and network printer on the new print cluster
0
 
compdigit44Author Commented:
I have a feeling there is no other easier way to create the reference CSV file besides manually interest each UNC path in to each column..

And all of this is because the end users where I work do not tolerate change of any type. If something as a default print.

102 printers down 492 to go... :o(
0
 
RobSampsonCommented:
OK, I've changed up the code a little, just added a bit to the end if the default printer could not be identified.  What is does, is if you specify your new print server on line 35
strNewServer = "\\server2\"

then within the user's existing connections on that server, it will default the first one found.

Regards,

Rob.

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
strMapFile = "C:\Temp\Scripts\PrinterMap.csv"
Set dctPrinters = CreateObject("Scripting.Dictionary")
dctPrinters.CompareMode = vbTextCompare
If objFSO.FileExists(strMapFile) = False Then
	WScript.Echo "File not found: " & strMapFile
Else
	Set objMapFile = objFSO.OpenTextFile(strMapFile, 1, False)
	While Not objMapFile.AtEndOfStream
		strLine = objMapFile.ReadLine
		If InStr(strLine, "\\") > 0 Then
			strOldPrinter = Split(strLine, ",")(0)
			strNewPrinter = Split(strLine, ",")(1)
			If dctPrinters.Exists(strOldPrinter) = False Then dctPrinters.Add strOldPrinter, strNewPrinter
		End If
	Wend
	objMapFile.Close
	Set objShell = CreateObject("WScript.Shell")
	strDefault = objShell.RegRead("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device")
	If strDefault <> "" Then
		strDefault = Left(strDefault, InStr(strDefault, ",") - 1)
		WScript.Echo strDefault
		If dctPrinters.Exists(strDefault) = True Then
			WScript.Echo "Changing default printer from " & strDefault & " to " & dctPrinters(strDefault)
			On Error Resume Next
			objNetwork.SetDefaultPrinter dctPrinters(strDefault)
			If Err.Number <> 0 Then WScript.Echo Err.Description
			Err.Clear
			On Error GoTo 0
		Else
			WScript.Echo "Unable to find default printer replacement for " & strDefault
		End If
	Else
		strNewServer = "\\server2\"
		If Right(strNewServer, 1) <> "\" Then strNewServer = strNewServer + 1
		Set objPrinters = objNetwork.EnumPrinterConnections
		For i = 0 to objPrinters.Count - 1 Step 2
			If LCase(Left(objPrinters.Item(i+1), InStrRev(objPrinters.Item(i+1), "\"))) = LCase(strNewServer) Then
				strDefault = objPrinters.Item(i+1)
				Exit For
			End If
		Next
		WScript.Echo "Unable to determine default printer. Setting default printer to " & strDefault
		On Error Resume Next
		objNetwork.SetDefaultPrinter dctPrinters(strDefault)
		If Err.Number <> 0 Then WScript.Echo Err.Description
		Err.Clear
		On Error GoTo 0
	End If
End If

Open in new window


And yes, it is definitely a tedious task to map out which printers have replaced which, but it will also help your support staff who need to know which printers are where now.

Regards,

Rob.
0
 
compdigit44Author Commented:
Thanks!!!!

Give me a couple of days to build the reference spreadsheet for I can start testing things.

I am build the spreadsheet as an XLSX first with old an new queue names in separate column then going to save it as a CSV when ready. I have noticed that some of my UNC paths so has hyperlinks while others do no . Please note that all my queue names have spaces. When I am ready to deploy the script I will be setting it a a group policy log in script should I place the reference file in the sysvol location as well?
0
 
RobSampsonCommented:
The hyperlinks won't matter, since when you save it as a CSV, they will be removed, because a CSV is just plain text.  The spaces won't matter either, as I mentioned before, as long as the queue names do not have commas in them, there will be only one comma per line in the CSV, and that is the delimiter.

When you deploy the script, put the CSV in the NETLOGON folder as well, and change line 3 from
strMapFile = "C:\Temp\Scripts\PrinterMap.csv"
to
strMapFile = "\\domain.com\SysVol\Domain.com\Scripts\PrinterMap.csv"

where Domain.Com is the name of your domain.

Regards,

Rob.
0
 
compdigit44Author Commented:
OK I am finishing up my reference file today. This was on top of my normal daily admin work... ;-)

Anyway in the script you make reference to listing my new printer server but I have a cluster which breaks out the printer resource into to separate names... \\serverA and \\ServerB original we had this set so a default printer on the new cluster would be select at random. I want this behavior to still exist if the default printer is so now not mapped from the spreadsheet. Here if what my full code looks like:
strSrvName1 = "\\serverA\HP"
strSrvName2 = "\\serverB\Dell"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
strMapFile = "C:\PrinterMapp.csv"
Set dctPrinters = CreateObject("Scripting.Dictionary")
dctPrinters.CompareMode = vbTextCompare
If objFSO.FileExists(strMapFile) = False Then
	WScript.Echo "File not found: " & strMapFile
Else
	Set objMapFile = objFSO.OpenTextFile(strMapFile, 1, False)
	While Not objMapFile.AtEndOfStream
		strLine = objMapFile.ReadLine
		If InStr(strLine, "\\") > 0 Then
			strOldPrinter = Split(strLine, ",")(0)
			strNewPrinter = Split(strLine, ",")(1)
			If dctPrinters.Exists(strOldPrinter) = False Then dctPrinters.Add strOldPrinter, strNewPrinter
		End If
	Wend
	objMapFile.Close
	Set objShell = CreateObject("WScript.Shell")
	strDefault = objShell.RegRead("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device")
	If strDefault <> "" Then
		strDefault = Left(strDefault, InStr(strDefault, ",") - 1)
		WScript.Echo strDefault
		If dctPrinters.Exists(strDefault) = True Then
			WScript.Echo "Changing default printer from " & strDefault & " to " & dctPrinters(strDefault)
			On Error Resume Next
			objNetwork.SetDefaultPrinter dctPrinters(strDefault)
			If Err.Number <> 0 Then WScript.Echo Err.Description
			Err.Clear
			On Error GoTo 0
		Else
			WScript.Echo "Unable to find default printer replacement for " & strDefault
		End If
	Else
		strNewServer = "\\server2\"
		If Right(strNewServer, 1) <> "\" Then strNewServer = strNewServer + 1
		Set objPrinters = objNetwork.EnumPrinterConnections
		For i = 0 to objPrinters.Count - 1 Step 2
			If LCase(Left(objPrinters.Item(i+1), InStrRev(objPrinters.Item(i+1), "\"))) = LCase(strNewServer) Then
				strDefault = objPrinters.Item(i+1)
				Exit For
			End If
		Next
		WScript.Echo "Unable to determine default printer. Setting default printer to " & strDefault
		On Error Resume Next
		objNetwork.SetDefaultPrinter dctPrinters(strDefault)
		If Err.Number <> 0 Then WScript.Echo Err.Description
		Err.Clear
		On Error GoTo 0
	End If
End If
                                          


Set oWsh = WScript.CreateObject("WScript.Shell")
     strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
     Set oFSO = CreateObject("Scripting.FileSystemObject")
     Set oFile = oFSO.CreateTextFile(strUserProfile & "\file.txt")
     oFile.Close
     Set oFile = oFSO.OpenTextFile(strUserProfile & "\file.txt", 8, True,0)


strNewServer1 = "\\NewServer1\"
strNewServer2 = "\\NewServer2\"
Set oShell = CreateObject("WScript.Shell")
strValue1 = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
strDefaultPRNOld = oShell.RegRead(strValue1)
LogEntry (strDefaultPRNOld) & "          ****OLD DEFAULT PRINTER****                 "


Set objNetwork = CreateObject("WScript.Network")
Set objPrinters = objNetwork.EnumPrinterConnections
For intPrinter = 0 To objPrinters.Count - 1 Step 2
	strPort = objPrinters(intPrinter)
	strPrinter = objPrinters(intPrinter + 1)
	If UCase(Left(strPrinter, Len(strNewServer1))) = UCase(strNewServer1) Or UCase(Left(strPrinter, Len(strNewServer2))) = UCase(strNewServer2) Then strDefaultPrinter = strPrinter
Next
If strDefaultPrinter <> "" Then
	objNetwork.SetDefaultPrinter strDefaultPrinter

End If
strValue2 = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
strDefaultPRNNew = oShell.RegRead(strValue2)
LogEntry (strDefaultPRNNew) & "          ****NEW DEFAULT PRINTER****              "


     Set WshNetwork = WScript.CreateObject("WScript.Network")
Set colPrinters = WshNetwork.EnumPrinterConnections
For intPrinter = 0 To colPrinters.Count - 1 Step 2
    strPrinterPort = colPrinters.Item(intPrinter)
    strPrinterName = colPrinters.Item(intPrinter + 1)
    LogEntry strPrinterName & " - " & strPrinterPort

    If Left(strPrinterName, Len(strSrvName1)) = strSrvName1 Then
        LogEntry "  Printer matches target name.  Printer will be deleted"
        On Error Resume Next
        WshNetwork.RemovePrinterConnection strPrinterName
        If Err.Number = 0 Then
            LogEntry "  Printer was succesfully deleted"
        Else
            LogEntry "  An error occured while deleting the printer.  Error Number: " & Err.Number & " Error Description: " & Err.Description
        End If
    Else
        LogEntry "  Printer does not match target name.  No action required"
    End If

    If Left(strPrinterName, Len(strSrvName2)) = strSrvName2 Then
        LogEntry "  Printer matches target name.  Printer will be deleted"
        On Error Resume Next
        WshNetwork.RemovePrinterConnection strPrinterName
        If Err.Number = 0 Then
            LogEntry "  Printer was succesfully deleted"
        Else
            LogEntry "  An error occured while deleting the printer.  Error Number: " & Err.Number & " Error Description: " & Err.Description
        End If
    Else
        LogEntry "  Printer does not match target name.  No action required"
    End If
Next

Function LogEntry(LogEntryText)
     oFile.WriteLine "<![LOG[" &      LogEntryText       & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date, "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file=""" & WScript.Application & " " & WScript.Version & """>"
End Function
                                          

Open in new window

0
 
compdigit44Author Commented:
OK on my first pass with the reference file the script runs and does move the default to the correct one but then displays a message> "Unable to find default printer replacement for..."  ALso the script does not delete the printers I set the printer to delete. I am not sure if this anything do to with the fact them printers start with numbers since print queues with letter names seem to be getting deleted. ALos I noticed when the default printer is moved it never shows a green check mark.

Is there anyway to make the script not case sensitive? It seems to be ignoring  LCase
0
 
compdigit44Author Commented:
Any thoughts Or suggestions???

 I am grateful for even the smallest bit of advice... :o)
0
 
RobSampsonCommented:
Hi, sorry for my delay.  Yesterday I was too busy to look at it. So I'm cleaning up the code a bit, to see if we can get it working better.  As far as deleting printers goes, you did not have anything assigned to strSrvName1 and strSrvName2. I have added a line for you to fill those in.  I assume those should be the old print servers.

strNewServer1 = "\\NewServer1\"
strNewServer2 = "\\NewServer2\"
strSrvName1 = "\\OldServer1\"
strSrvName2 = "\\OldServer2\"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objNetwork = CreateObject("WScript.Network")
strMapFile = "C:\PrinterMapp.csv"
Set dctPrinters = CreateObject("Scripting.Dictionary")
dctPrinters.CompareMode = vbTextCompare
If objFSO.FileExists(strMapFile) = False Then
	WScript.Echo "File not found: " & strMapFile
Else
	Set objMapFile = objFSO.OpenTextFile(strMapFile, 1, False)
	While Not objMapFile.AtEndOfStream
		strLine = objMapFile.ReadLine
		If InStr(strLine, "\\") > 0 Then
			strOldPrinter = Split(strLine, ",")(0)
			strNewPrinter = Split(strLine, ",")(1)
			If dctPrinters.Exists(strOldPrinter) = False Then dctPrinters.Add strOldPrinter, strNewPrinter
		End If
	Wend
	objMapFile.Close
	Set objShell = CreateObject("WScript.Shell")
	strDefault = objShell.RegRead("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device")
	If strDefault <> "" Then
		strDefault = Left(strDefault, InStr(strDefault, ",") - 1)
		'WScript.Echo "Current default printer is: " & strDefault
		If dctPrinters.Exists(strDefault) = True Then
			WScript.Echo "Changing default printer from " & strDefault & " to " & dctPrinters(strDefault)
			On Error Resume Next
			objNetwork.SetDefaultPrinter dctPrinters(strDefault)
			If Err.Number <> 0 Then WScript.Echo Err.Description
			Err.Clear
			On Error GoTo 0
		Else
			WScript.Echo "Unable to find default printer replacement for " & strDefault
		End If
	Else
		If Right(strNewServer1, 1) <> "\" Then strNewServer1 = strNewServer1 + 1
		If Right(strNewServer2, 1) <> "\" Then strNewServer2 = strNewServer2 + 1
		Set objPrinters = objNetwork.EnumPrinterConnections
		For i = 0 to objPrinters.Count - 1 Step 2
			If LCase(Left(objPrinters.Item(i+1), InStrRev(objPrinters.Item(i+1), "\"))) = LCase(strNewServer1) Or LCase(Left(objPrinters.Item(i+1), InStrRev(objPrinters.Item(i+1), "\"))) = LCase(strNewServer2) Then
				strDefault = objPrinters.Item(i+1)
				Exit For
			End If
		Next
		WScript.Echo "Unable to determine default printer. Setting default printer to " & strDefault
		On Error Resume Next
		objNetwork.SetDefaultPrinter dctPrinters(strDefault)
		If Err.Number <> 0 Then WScript.Echo Err.Description
		Err.Clear
		On Error GoTo 0
	End If
End If
                                          


Set oWsh = CreateObject("WScript.Shell")
strUserProfile = oWsh.ExpandEnvironmentStrings("%userprofile%")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFile = oFSO.CreateTextFile(strUserProfile & "\file.txt", True)

strDefaultOld = objShell.RegRead("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device")
LogEntry (strDefaultOld) & "          ****OLD DEFAULT PRINTER****                 "

Set objNetwork = CreateObject("WScript.Network")
Set objPrinters = objNetwork.EnumPrinterConnections
For intPrinter = 0 To objPrinters.Count - 1 Step 2
	strPort = objPrinters(intPrinter)
	strPrinter = objPrinters(intPrinter + 1)
	If UCase(Left(strPrinter, Len(strNewServer1))) = UCase(strNewServer1) Or UCase(Left(strPrinter, Len(strNewServer2))) = UCase(strNewServer2) Then strDefaultPrinter = strPrinter
Next
If strDefaultPrinter <> "" Then
	objNetwork.SetDefaultPrinter strDefaultPrinter
End If

strDefaultNew = objShell.RegRead("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device")
LogEntry (strDefaultNew) & "          ****NEW DEFAULT PRINTER****              "

Set colPrinters = objNetwork.EnumPrinterConnections
For intPrinter = 0 To colPrinters.Count - 1 Step 2
    strPrinterPort = colPrinters.Item(intPrinter)
    strPrinterName = colPrinters.Item(intPrinter + 1)
    LogEntry strPrinterName & " - " & strPrinterPort

    If Left(strPrinterName, Len(strSrvName1)) = strSrvName1 Then
        LogEntry "  Printer matches target name.  Printer will be deleted"
        On Error Resume Next
        WshNetwork.RemovePrinterConnection strPrinterName
        If Err.Number = 0 Then
            LogEntry "  Printer was succesfully deleted"
        Else
            LogEntry "  An error occured while deleting the printer.  Error Number: " & Err.Number & " Error Description: " & Err.Description
        End If
    Else
        LogEntry "  Printer does not match target name.  No action required"
    End If

    If Left(strPrinterName, Len(strSrvName2)) = strSrvName2 Then
        LogEntry "  Printer matches target name.  Printer will be deleted"
        On Error Resume Next
        WshNetwork.RemovePrinterConnection strPrinterName
        If Err.Number = 0 Then
            LogEntry "  Printer was succesfully deleted"
        Else
            LogEntry "  An error occured while deleting the printer.  Error Number: " & Err.Number & " Error Description: " & Err.Description
        End If
    Else
        LogEntry "  Printer does not match target name.  No action required"
    End If
Next

Function LogEntry(LogEntryText)
     oFile.WriteLine "<![LOG[" &      LogEntryText       & "]LOG]!><time=""" & Hour(Now) & ":" & Minute(Now) & ":" & Second(Now) & ".001+240"" date=""" & Replace(Date, "/", "-") & """ component=""PrintMapDelete"" context="""" type=""1"" thread=""0"" file=""" & WScript.Application & " " & WScript.Version & """>"
End Function

Open in new window

0
 
compdigit44Author Commented:
Thanks I am trying it and will keep you posted......
0
 
compdigit44Author Commented:
Thank you for all of your help....
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 12
  • 6
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now