VBSCript for Default Printer

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
LVL 20
compdigit44Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

RobSampsonCommented:
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
compdigit44Author Commented:
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
compdigit44Author Commented:
Any suggestions on have been reseraching this an stomped..
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

compdigit44Author Commented:
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
RobSampsonCommented:
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
compdigit44Author Commented:
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
RobSampsonCommented:
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
compdigit44Author Commented:
It works.. How can I remove the dialog box that tells the user their new default printer
0
RobSampsonCommented:
You can delete the entire WScript.Echo line.

Rob.
0
compdigit44Author Commented:
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
compdigit44Author Commented:
Any thoughts on this??
0
compdigit44Author Commented:
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
RobSampsonCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
compdigit44Author Commented:
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
RobSampsonCommented:
Oh, just noticed a typo. Take the equals sign out from the oFile.Write line.
0
compdigit44Author Commented:
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
RobSampsonCommented:
Can you show me the whole code you're using? That will give me a better idea of what might be missing.
0
compdigit44Author Commented:
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
RobSampsonCommented:
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
compdigit44Author Commented:
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
compdigit44Author Commented:
Is there something wrong with the function did wasn't doing this before???
0
RobSampsonCommented:
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
compdigit44Author Commented:
Thank you for all of your help......
0
RobSampsonCommented:
No problem. Sorry I missed your comments, I was asleep. Did you still want me clean up the code for you?
0
compdigit44Author Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.