Jump to content

Script not setting the correct default printer


Recommended Posts

Posted

Guys and Gals,

 

I have a scripty that maps printers based on Computer OU or User Group, but the User group Mapping isnt working, it just makes the first in the If list the default, can someone have a look over as im not a scrip genius and just cut and shut scripts together.

 

As i say the OU bit works great but cannot get the Bt group bit to default a printer based on Groups called year 1, Year 2 etc

 

Thanks

 

'Variables

Set objSysInfo = CreateObject("ADSystemInfo")
strName = objSysInfo.ComputerName
arrComputerName = Split(strName, ",")
arrOU = Split(arrComputerName(1), "=")
strComputerOU = arrOU(1)
Set objNetwork = CreateObject("WScript.Network")
Set objPrinters = objNetwork.EnumPrinterConnections
strUser = objNetwork.UserName
strDomain = SPRINGGROVE

'Remove all the Network Printers from the Machine

For i = 0 to objPrinters.Count - 1 Step 2

           On Error Resume Next

    if Left(objPrinters.Item(i), 3) <> "lpt" And Left(objPrinters.Item(i), 3) <> "usb" then

             objNetwork.RemovePrinterConnection objPrinters.Item(i+1), true, true

           else Echo "No network printers found"

           end if

Next

'Map Global Printers

objNetwork.AddWindowsPrinterConnection "\\Spring-svr01\CopierColour"
objNetwork.AddWindowsPrinterConnection "\\Spring-svr01\CopierMono"
objNetwork.AddWindowsPrinterConnection "\\Spring-svr01\ICTSuite"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb01\Year1"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb02\Year2"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb03\Year3"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb04\Year4"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb05\Year5"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb06\Year6"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb07\EYUClass"
objNetwork.AddWindowsPrinterConnection "\\Spring-eyu02\EYUPlay"


'Map Printers Based on Group (Use Actual Printer name for local Printers) (Move below OU Section if you want to apply group defaults after)

Set objGroups = GetObject("WinNT://"& strDomain & "/" & strUser & ",user")
For Each objGroups In objGroups.Groups

If objGroups.Name = "Year 1" Then
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb01\Year1"
objNetwork.SetDefaultPrinter "\\Spring-iwb01\Year1"

ElseIf objGroups.Name = "Year 2" Then
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb02\Year2"
objNetwork.SetDefaultPrinter "\\Spring-iwb02\Year2"

ElseIf objGroups.Name = "Year 3" Then
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb03\Year3"
objNetwork.SetDefaultPrinter "\\Spring-iwb03\Year3"

ElseIf objGroups.Name = "Year 4" Then
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb04\Year4"
objNetwork.SetDefaultPrinter "\\Spring-iwb04\Year4"

ElseIf objGroups.Name = "Year 5" Then
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb05\Year5"
objNetwork.SetDefaultPrinter "\\Spring-iwb05\Year5"

ElseIf objGroups.Name = "Year 6" Then
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb06\Year6"
objNetwork.SetDefaultPrinter "\\Spring-iwb06\Year6"


End If

Next

'Map Printers based on OU (Use Actual Printer name for local Printers) 

Select Case strComputerOU
   Case "EYU Class"
       objNetwork.AddWindowsPrinterConnection "EYU Class"
       objNetwork.SetDefaultPrinter "EYU Class"
       objNetwork.SetDefaultPrinter "\\Spring-iwb07\EYUClass"
   Case "EYU Play"
       objNetwork.AddWindowsPrinterConnection "EYU Play"
       objNetwork.SetDefaultPrinter "EYU Play"
       objNetwork.SetDefaultPrinter "\\Spring-eyu02\EYUPlay"
   Case "ICT Suite"
       objNetwork.AddWindowsPrinterConnection "\\Spring-svr01\ICTSuite"
       objNetwork.SetDefaultPrinter "\\Spring-svr01\ICTSuite"
   Case "Library"
       objNetwork.AddWindowsPrinterConnection "\\Spring-svr01\ICTSuite"
       objNetwork.SetDefaultPrinter "\\Spring-svr01\ICTSuite"

End Select

Posted

You can try to use case statements for the year groups so instead of

 

If objGroups.Name = "Year 1" Then
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb01\Year1"
objNetwork.SetDefaultPrinter "\\Spring-iwb01\Year1"

ElseIf objGroups.Name = "Year 2" Then
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb02\Year2"
objNetwork.SetDefaultPrinter "\\Spring-iwb02\Year2"

 

etc it would read

Select Case objGroups.Name 
Case "Year 1"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb01\Year1"
objNetwork.SetDefaultPrinter "\\Spring-iwb01\Year1"

Case "Year 2"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb02\Year2"
objNetwork.SetDefaultPrinter "\\Spring-iwb02\Year2"

Posted

Tried that it doesnt work, it doesnt give and error but doesnt select the correct default (like its not picking up what group the user is in)

 

Heres what i ammended it to:

 

'Variables

Set objSysInfo = CreateObject("ADSystemInfo")
strName = objSysInfo.ComputerName
arrComputerName = Split(strName, ",")
arrOU = Split(arrComputerName(1), "=")
strComputerOU = arrOU(1)
Set objNetwork = CreateObject("WScript.Network")
Set objPrinters = objNetwork.EnumPrinterConnections
strUser = objNetwork.UserName
strDomain = SPRINGGROVE

'Remove all the Network Printers from the Machine

For i = 0 to objPrinters.Count - 1 Step 2

           On Error Resume Next

    if Left(objPrinters.Item(i), 3) <> "lpt" And Left(objPrinters.Item(i), 3) <> "usb" then

             objNetwork.RemovePrinterConnection objPrinters.Item(i+1), true, true

           else Echo "No network printers found"

           end if

Next

'Map Global Printers

objNetwork.AddWindowsPrinterConnection "\\Spring-svr01\CopierColour"
objNetwork.AddWindowsPrinterConnection "\\Spring-svr01\CopierMono"
objNetwork.AddWindowsPrinterConnection "\\Spring-svr01\ICTSuite"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb01\Year1"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb02\Year2"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb03\Year3"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb04\Year4"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb05\Year5"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb06\Year6"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb07\EYUClass"
objNetwork.AddWindowsPrinterConnection "\\Spring-eyu02\EYUPlay"


'Map Printers Based on Group (Use Actual Printer name for local Printers) (Move below OU Section if you want to 

apply group defaults after)

Set objGroups = GetObject("WinNT://"& strDomain & "/" & strUser & ",user")
For Each objGroups In objGroups.Groups

Select Case objGroups.Name 

Case "Year 1"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb01\Year1"
objNetwork.SetDefaultPrinter "\\Spring-iwb01\Year1"

Case "Year 2"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb02\Year2"
objNetwork.SetDefaultPrinter "\\Spring-iwb02\Year2"

Case "Year 3"
objNetwork.AddWindowsPrinterConnection "\\Spring-iwb03\Year3"
objNetwork.SetDefaultPrinter "\\Spring-iwb03\Year3"


End Select

Next

'Map Printers based on OU (Use Actual Printer name for local Printers) 

Select Case strComputerOU
   Case "EYU Class"
       objNetwork.AddWindowsPrinterConnection "EYU Class"
       objNetwork.SetDefaultPrinter "EYU Class"
       objNetwork.SetDefaultPrinter "\\Spring-iwb07\EYUClass"
   Case "EYU Play"
       objNetwork.AddWindowsPrinterConnection "EYU Play"
       objNetwork.SetDefaultPrinter "EYU Play"
       objNetwork.SetDefaultPrinter "\\Spring-eyu02\EYUPlay"
   Case "ICT Suite"
       objNetwork.AddWindowsPrinterConnection "\\Spring-svr01\ICTSuite"
       objNetwork.SetDefaultPrinter "\\Spring-svr01\ICTSuite"
   Case "Library"
       objNetwork.AddWindowsPrinterConnection "\\Spring-svr01\ICTSuite"
       objNetwork.SetDefaultPrinter "\\Spring-svr01\ICTSuite"

End Select

Posted

 

strDomain = SPRINGGROVE

 

 

I'd put "" around SPRINGROVE as (unless I'm completely off with my understanding of the code) your script will look for another variable called SPRINGGROVE and will try and use whatever value that variable has. Since there isn't a variable with that name your strDomain will end up being empty.

 

I may be making this up but I think that when using the WinNT method all the properties of an object are returned in uppercase so you want to change your code to use uppercase values in your case statement. So:

 

 

Select Case objGroups.Name

 

Case "Year 1"

 

would become

 

Select Case objGroups.Name

 

Case "YEAR 1"

 

If that still doesn't work then you can comment out the "On Error Resume Next" (using a ' at the start if you didn't know) line and the script will show any error messages that occur when it is run (along with codes and line numbers to help you track down the error)

Posted

The quotes around the domain name worked, thanks

 

Also had to create a bit more on the script to delete a reg entry as there was a ghost printer that this script wouldnt get rid of.

 

Thanks again

 

PS didnt need to do the CAPS on the Year 1 etc

Posted

Here's my default printer script - may be useful for someone. It works in a slightly different way. It might be slightly faster because the printer connections are per machine so don't need to be recreated on each logon. Also, it keeps the default printer information all in Group Policy, which makes it easier for editing especially when working with less script-savvy techs.

I have also used the method described by the original poster and it does work well.

 

First of all, you install the printer to the local machine using the pushprinterconnections method in Group Policy See here. This makes the printer available to all users on the machine but it doesn't set it as default. As we know, the problem with windows is the default printer option is controlled by the user part of the registry, not the computer. (Daft!).

 

You use a custom ADM file (attached) in Group policy - this writes a the name of the default printer to the local machine in the registry. You can use either the network or local name e.g. "\\svr1\printer" or "Canon 123") We now have a default printer value for the computer, rather than the user.

 

This script runs is called by the logon script (or could be called through GP). It reads the registry value, looks up the port name of the printer, then sets the default printer. It adds a registry key to set the default printer rather than using the WSH SetDefaultPrinter command. The reason is that the printer doesn't actually appear until a few minutes after logon so the SetDefaultPrinter method fails.

 

The script has a few extra If's than needed. This was because I was planning on making it more clever e.g. adding per user option. Or making it so it would set a local printer if there was one attached. It could also do with some error checking - e.g. what do do if the printer that is supposed to be default isn't actually installed.

 

 

'Option Explicit

Dim WShell, PrinterPol

Set objShell = WScript.CreateObject("WScript.Shell")



'****Read the value for the default printer set by group policy

'Switch Error Reporting Off

On Error Resume Next

'Attempt to read key (This key has been written by Group Policy via the custom ADM file)
strPrinterPol = objShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\DefaultPrinter\DefaultPrinter")

'Turn error reporting back on
On Error Goto 0

'Check the length of the key to see if the registry has retrieved anything.
If Len(strPrinterPol) > 0 Then
Call funSetDefaultPrinter(strPrinterPol)

Else
'	Wscript.echo "policy doesn't exist"

End If


Wscript.Quit

'##########   Functions   ############


Function funSetDefaultPrinter(strPrinterName)

'Get details about the printer, e.g. what it uses to spool (e.g. winspool) and port name (e.g NE01:)
'These commands just read from the registry but because the path to the key may contain back slashes the normal regread function won't work
'strPrinterPort is what is returned (it will be like "winspool, NE01:") 

Const HKEY_USERS = &H80000003
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

strKeyPath = ".DEFAULT\Software\Microsoft\Windows NT\CurrentVersion\Devices"

objReg.GetStringValue HKEY_USERS, strKeyPath, strPrinterName, strPrinterPort

'wscript.echo StrPrinterPort
'Put the printer name and port name together
strDefPrintKey = StrPrinterName & "," & strPrinterPort


'Now Write the values to the registry to set the default printer
objShell.RegWrite "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device", strDefPrintKey, "REG_SZ"

End Function



defaultprinterADM.zip

Posted

I may be making this up but I think that when using the WinNT method all the properties of an object are returned in uppercase so you want to change your code to use uppercase values in your case statement.

 

Don't think you're making it up but it's not safe to assume that you'll get all caps, all lower case etc.

 

What I always do is something like this:

sGroup=ucase(objgroups.name)
select case sGroup
 case "YEAR 1"

...

end select

 

That way, you're sure that you're working with upper case even if something in the underlying provider changes

  • 3 weeks later...
Posted

Eean,

 

I'm new to this forum.

Your custom adm file looks like just the solution I've been looking for.

I tried downlading it, with no luck.

I can get the zip file, but couldn't read the enclosed file.

It looks like I'm the only one to try looking at it so far. Did it post correctly? Or, could I be doing something wrong (Entirely Likely)

 

Thanks!

 

Ann

Posted

Hmm... that's wierd. It's not letting me either.

 

Copy and paste this script into Notepad and save it as defaultprinter.vbs

 

Dim WShell, PrinterPol

Set objShell = WScript.CreateObject("WScript.Shell")



'****Read the value for the default printer set by group policy

'Switch Error Reporting Off

On Error Resume Next

'Attempt to read key
strPrinterPol = objShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\DefaultPrinter\DefaultPrinter")

'Turn error reporting back on
On Error Goto 0

'Check the length of the key to see if the registry has retrieved anything.
If Len(strPrinterPol) > 0 Then
Call funSetDefaultPrinter(strPrinterPol)

Else
'	Wscript.echo "policy doesn't exist"

End If


Wscript.Quit

'##########   Functions   ############


Function funSetDefaultPrinter(strPrinterName)

'Get details about the printer, e.g. what it uses to spool (e.g. winspool) and port name (e.g NE01:)
'These commands just read from the registry but because the path to the key may contain back slashes the normal regread function won't work
'strPrinterPort is what is returned (it will be like "winspool, NE01:") 

Const HKEY_USERS = &H80000003
strComputer = "."
Set objReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")

strKeyPath = ".DEFAULT\Software\Microsoft\Windows NT\CurrentVersion\Devices"

objReg.GetStringValue HKEY_USERS, strKeyPath, strPrinterName, strPrinterPort

'wscript.echo StrPrinterPort
'Put the printer name and port name together
strDefPrintKey = StrPrinterName & "," & strPrinterPort


'Now Write the values to the registry to set the default printer
objShell.RegWrite "HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows\Device", strDefPrintKey, "REG_SZ"

End Function

 

Copy and paste this script and save it as defaultprinter.adm

CLASS MACHINE 

CATEGORY "Printers" 
POLICY "Set Default Printer" 

	KEYNAME "Software\Microsoft\Windows\CurrentVersion\Policies\DefaultPrinter"
	PART "DefaultPrinter"
		EDITTEXT
		VALUENAME "DefaultPrinter"
	END PART 
END POLICY 
END CATEGORY 

 

I'm assuming that you've already got printers installing themselves via group policy and pushprinterconnections.exe utility. And also that you know how to add custom adm files to group policy and add vbs files to your logon batch file (you could probably also initate this script using the logon scripts part of group policy though I haven't tested that). If you need help with these, let me know. It's only been tested with Server 2003 and Windows XP. Good luck!

  • Thanks 1
Posted

I am definately the less script-savvy tech that you refered to in your post above.

 

The lab I'm trying this out in gets the correct printer using group policy and pushprinterconnections.exe.

 

I'm running Windows 2003 servers and my labs are all Windows XP machines.

 

I was able to get the adm file to work, Thanks!

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\DefaultPrinter

gets a string value of DefaultPrinter set to the UNC of the pritner that I want.

In my case, this is:

\\highschool\Media Lab - RISO

 

The script seems to remove the existing default printer, but not set a new default printer. When I look in Printers and Faxes, none of the printers have the default icon with the checkmark. Although, when I print something, the correct printer does seem to be used.

 

So, I'm not sure if I have a problem or not.

 

Thanks for the help!

 

Ann

Posted
I am definately the less script-savvy tech that you refered to in your post above.

 

The lab I'm trying this out in gets the correct printer using group policy and pushprinterconnections.exe.

 

I'm running Windows 2003 servers and my labs are all Windows XP machines.

 

I was able to get the adm file to work, Thanks!

HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\policies\DefaultPrinter

gets a string value of DefaultPrinter set to the UNC of the pritner that I want.

In my case, this is:

\\highschool\Media Lab - RISO

 

The script seems to remove the existing default printer, but not set a new default printer. When I look in Printers and Faxes, none of the printers have the default icon with the checkmark. Although, when I print something, the correct printer does seem to be used.

 

So, I'm not sure if I have a problem or not.

 

Thanks for the help!

 

Ann

 

I would use wscripts sleep function / property and make it sleep for about 5 to 10 seconds before trying to set the default printer and the rest of it that way it removes all the printers etc and then sets the defaults etc

 

Also error wise may be worth while adding a case else and putting code under there to give you a msgbox or something so you know if there is an error or what happens if none of the above conditions are met ?

  • Thanks 1
Posted
To expand on what is said above it is useful to use message box on all the different conditions so you can see where the code is going.
  • Thanks 1
Posted

 

So, I'm not sure if I have a problem or not.

 

Well, if it works then you don't have a problem!

Try hitting F5 a few times when you go to the printer window. The reason is (and why my script has to set the default printer using the registry rather than the default printer command) is that when when you add printers on a per machine basis (like via pushprinterconnections) it takes a good 30-60seconds before they appear in the printers folder.

 

Also check Hkey Current User\Software\Microsoft\Windows NT\CurrentVersion\Windows\ then Device in the registry you should get something like:

 

\\highschool\Media Lab - RISO,winspool,Ne04:

 

Can you tell me what you get.

 

Also, it might not like Media Lab - RISO. Just for testing try making it a one word share name.

  • Thanks 1
Posted

I took a day off for a conference, and today it is working!!

 

Before my day off, Hkey Current User\Software\Microsoft\Windows NT\CurrentVersion\Windows\ was not showing any port information.

 

I did add the delay as suggested above, that seemed to help.

I shortened the name, and redeployed the printer with group policy.

 

Between everything, I now get port information and a nice default printer icon with checkmark.

 

Thanks All!

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...