[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

VBSCript for Default Printer

Posted on 2014-08-26
25
Medium Priority
?
204 Views
Last Modified: 2014-08-30
I have found online how to set a default printer using VB but for my print cluster migration I need to set users new default printer to any printer shareing with the name \\ServerA
0
Comment
Question by:compdigit44
  • 15
  • 10
25 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 40287271
Do you want to use EnumPrinterConnections to check existing connections, then call SetDefaultPrinter on the last one starting with \\ServerA?  Or something a bit different?

Rob.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40287595
Thanks I am not a vbscripting person.

I understand what you are saying but how can I ensure the printer mapping go to any printer server starting with \\server A and not a local printer
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40287853
Any suggestions on have been reseraching this an stomped..
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 20

Author Comment

by:compdigit44
ID: 40289302
More information:


I am migrating from one printer cluster to another and are printer are divided up between two resource groups. I already have a script that will remove all printer mapped to my old cluster the problem is when it run it leave user without a default printer.  I just want to set their default to any printer on the new cluster.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 40289305
Sorry, I just got back into work, and have written the script to do what I originally suggested.

strServer = "\\SERVERA\"
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(strServer))) = UCase(strServer) Then strDefaultPrinter = strPrinter
Next
If strDefaultPrinter <> "" Then
	objNetwork.SetDefaultPrinter strDefaultPrinter
	WScript.Echo "Default printer has been set to " & strDefaultPrinter
End If

Open in new window


So that goes through all of the printer connection for the current user, and sets the default printer to the *last* printer enumerated that starts with the value in strServer.

Regards,

Rob.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40290045
Thanks how can we make the script work so that it would map set a default printer host on server \\servera or \\serverb. This is becuase we host our print queues on two seperate cluster resources
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 40290127
This should check for both.

strServer1 = "\\SERVERA\"
strServer2 = "\\SERVERB\"
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(strServer1))) = UCase(strServer1) Or UCase(Left(strPrinter, Len(strServer2))) = UCase(strServer2) Then strDefaultPrinter = strPrinter
Next
If strDefaultPrinter <> "" Then
	objNetwork.SetDefaultPrinter strDefaultPrinter
	WScript.Echo "Default printer has been set to " & strDefaultPrinter
End If

Open in new window


Rob.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40290239
It works.. How can I remove the dialog box that tells the user their new default printer
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 40290247
You can delete the entire WScript.Echo line.

Rob.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40290347
Thank you!!!!!!!! Your a genius!!!!!!!

In the scrip you posted would it be possibe to log the currect default printer to an already existing log file before it is changed?
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40290559
Any thoughts on this??
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40291144
I am so close to figuring this out my self... Write now when I try to write to my existing log file I get accessed denied .I know this is not a permission issue but a file lock issue...

OFile.Write is already defined to write and specific syntax to the file to file I type in oFile.Write <text>

It does write to the log file but write the the syntax of each line... I only need to check and set the default printer once.


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 = "\\servera"
strNewServer2 = "\\serverb"
Set oShell = CreateObject("WScript.Shell")
strValue = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
strDefaultPRN = oShell.RegRead(strValue)
Set oDefaultPrinter = oFSO.OpenTextFile(strUserProfile & "\file.txt", 8, True, 0)
oDeafultPrinter.close
oDefaultPrinter.Write = (strDefaultPRN) & "   ****OLD DEFAULT PRINTER****"

Open in new window

0
 
LVL 65

Accepted Solution

by:
RobSampson earned 2000 total points
ID: 40291632
Hi, that's pretty close. You were nearly there.  A few things

1. You don't need to create the file, close it, then open it for appending.  When you use CreateTextFile, you are automatically overwriting any existing file (when you add True as a second parameter), and it will be an object bound to the newly open file, so you can write to it straight away.
NOTE: If you wanted to append every time, without overwriting, don't use CreateTextFile, but use
Set oFile = oFSO.OpenTextFile(strUserProfile & "\file.txt", 8, True, 0)
instead.

2. The main issue you had is that you set oFile to lock file.txt and then tried to use oDefaultPrinter to open the same file, without closing the oFile object.  You could have put oFile.Close before the Set oDefaultPrinter line, but you don't need to that.  Since you created the file reference with oFile, you can just use that to do all of the file operations.

I hope that makes it a bit clearer for you.  We just create the file and set oFile as a reference, write to the same one, and close it.

Regards,

Rob.

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

strNewServer1 = "\\servera"
strNewServer2 = "\\serverb"
Set oShell = CreateObject("WScript.Shell")
strValue = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
strDefaultPRN = oShell.RegRead(strValue)
oFile.Write = (strDefaultPRN) & "   ****OLD DEFAULT PRINTER****"
oFile.Close

Open in new window

0
 
LVL 20

Author Comment

by:compdigit44
ID: 40292350
I am getting the error Object doesn't support this property or method "oFile.Write" Do I have to rename oFile to something else since I am already using oFile.write in another part of my script?
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 40292402
Oh, just noticed a typo. Take the equals sign out from the oFile.Write line.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40292475
Thanks..

The new error I am getting is :"object variable not set"  for oFile.Wirte and the line referenced it the last line of my script which hasn't changed. Since we are only working on the top part.
I know oFile is already defined . Any suggestions.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 40292483
Can you show me the whole code you're using? That will give me a better idea of what might be missing.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40292492
strSrvName1 = "\\Server1\H"
strSrvName2 = "\\Server2\C"

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 = "\\ServerI\"
strNewServer2 = "\\ServerQ\"
Set oShell = CreateObject("WScript.Shell")
strValue = "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device"
strDefaultPRN = oShell.RegRead(strValue)
oFile.Write (strDefaultPRN) & "   ****OLD DEFAULT PRINTER****"
oFile.Close



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

     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
 
LVL 65

Expert Comment

by:RobSampson
ID: 40292505
Remove
oFile.Close

That line is causing the file handle to be closed, but then you call LogEntry which tries to write to it again.  The code can be cleaned up a bit, so I can do that for you in a day or two, but it should work.

Rob.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40292527
Good News is that it is running but outputing the result and "<![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 & """>"

On each line.

Don't worry about cleaning up the script . I know it is a mess but I just need to to run
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40292664
Is there something wrong with the function did wasn't doing this before???
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 40292892
It writes that text every time you call LogEntry which you have for each condition when you check each existing printer against Server1 or Server2. That is what you are telling it to do. If you don't want that in the log file, comment out each call to LogEntry "<some text>" by putting an apostrophe at the start of each line.
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40293355
Thank you for all of your help......
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 40293894
No problem. Sorry I missed your comments, I was asleep. Did you still want me clean up the code for you?
0
 
LVL 20

Author Comment

by:compdigit44
ID: 40294688
Don't worry about cleaning up the code. At this point I am happy it works..

I did open another question though...
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28508295.html#a40294297

Thanks Again
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Integration Management Part 2
Loops Section Overview
Suggested Courses

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question