Link to home
Start Free TrialLog in
Avatar of compdigit44
compdigit44

asked on

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
Avatar of RobSampson
RobSampson
Flag of Australia image

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.
Avatar of compdigit44
compdigit44

ASKER

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
Any suggestions on have been reseraching this an stomped..
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.
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.
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
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.
It works.. How can I remove the dialog box that tells the user their new default printer
You can delete the entire WScript.Echo line.

Rob.
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?
Any thoughts on this??
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

ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
Oh, just noticed a typo. Take the equals sign out from the oFile.Write line.
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.
Can you show me the whole code you're using? That will give me a better idea of what might be missing.
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

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.
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
Is there something wrong with the function did wasn't doing this before???
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.
Thank you for all of your help......
No problem. Sorry I missed your comments, I was asleep. Did you still want me clean up the code for you?
Don't worry about cleaning up the code. At this point I am happy it works..

I did open another question though...
https://www.experts-exchange.com/questions/28508295/VBSCript-to-Set-Default-printer-to-New-Server-but-same-queue.html?anchorAnswerId=40294297#a40294297

Thanks Again