Problem installing printers with VB

Hello All,

I hope you guys can help me with a problem i have. For a customer we are trying to install printers via a script executed at login. We got it working on 32 bits workstations, but i wont work on 64 bits workstations.

We know that the problem is in removing the printers before connecting them. With the script we have now it works only in 32 bit, but we need a solution that will work on both. Or a script thats checkes the architecture of the PC en executes the script for 32 or 64 bits.

Big thanks in advance!

Michel 

Option Explicit 
 Dim WSHNetwork, FSO, strUserName, strUserDomain, ObjGroupDict, EnvVar, strVarUserName
Dim objNetwork, strLocal, strComputer, objWMIService, colInstalledPrinters, objPrinter
 Dim strUNCPrinter1, strUNCPrinter2, strUNCPrinter3, strUNCPrinter4
 Dim strUNCPrinter5
 dim printers
 dim counter




Set WSHNetwork = WScript.CreateObject("WScript.Network")
 Set EnvVar = WScript.CreateObject("WScript.Shell")
 Set FSO = CreateObject("Scripting.FileSystemObject")
 strVarUserName = EnvVar.ExpandEnvironmentStrings("%username%")




'Wait for user to log in




strUserName = ""
 While strUserName = ""
 WScript.Sleep 100 
 strUserName = WSHNetwork.UserName
 Wend
 strUserDomain = WSHNetwork.UserDomain




Set ObjGroupDict = CreateMemberOfObject(strUserDomain, strUserName)




' Create printers

strUNCPrinter1 = "\\s-exch\P50 - A4 Dubbel (Kleur)"
 strUNCPrinter2 = "\\s-exch\P50 - A4 Dubbel (Z-W)"
 strUNCPrinter3 = "\\s-exch\P50 - A4 Enkel (Kleur)"
 strUNCPrinter4 = "\\s-exch\P50 - A4 Enkel (Z-W)"
 strUNCPrinter5 = "\\s-exch\P50 - TCE - Briefpapier (Z-W)"




'Remove printers

Set objNetwork = CreateObject("WScript.Network")
  objNetwork.RemoveWindowsPrinterConnection "\\s-exch\P50 - A4 Dubbel (Kleur)"
  objNetwork.RemoveWindowsPrinterConnection "\\s-exch\P50 - A4 Dubbel (Z-W)"
  objNetwork.RemoveWindowsPrinterConnection "\\s-exch\P50 - A4 Enkel (Kleur)"
  objNetwork.RemoveWindowsPrinterConnection "\\s-exch\P50 - A4 Enkel (Z-W)"
  objNetwork.RemoveWindowsPrinterConnection "\\s-exch\P50 - TCE - Briefpapier (Z-W)"




' Create printermappings based on groupmembership

If MemberOf(ObjGroupDict, "P50") Then
   Set objNetwork = CreateObject("WScript.Network") 
   objNetwork.AddWindowsPrinterConnection strUNCPrinter1
 End If

If MemberOf(ObjGroupDict, "P50") Then
   Set objNetwork = CreateObject("WScript.Network") 
   objNetwork.AddWindowsPrinterConnection strUNCPrinter2
 End If

If MemberOf(ObjGroupDict, "P50") Then
   Set objNetwork = CreateObject("WScript.Network") 
   objNetwork.AddWindowsPrinterConnection strUNCPrinter3
 End If

If MemberOf(ObjGroupDict, "P50") Then
   Set objNetwork = CreateObject("WScript.Network") 
   objNetwork.AddWindowsPrinterConnection strUNCPrinter4
 End If

If MemberOf(ObjGroupDict, "P50") Then
   Set objNetwork = CreateObject("WScript.Network") 
   objNetwork.AddWindowsPrinterConnection strUNCPrinter5
 End If


 Function MemberOf(ObjDict, strKey)
   MemberOf = CBool(ObjGroupDict.Exists(strKey))
 End Function

Function CreateMemberOfObject(strDomain, strUserName)
   Dim objUser, objGroup
   Set CreateMemberOfObject = CreateObject("Scripting.Dictionary")
   CreateMemberOfObject.CompareMode = vbTextCompare
   Set objUser = GetObject("WinNT://" & strDomain & "/" & strUserName & ",user")
   For Each objGroup In objUser.Groups
   CreateMemberOfObject.Add objGroup.Name, "-"
   Next
   Set objUser = Nothing
 End Function


  • Edited by Miesjel Friday, January 03, 2014 12:02 PM
January 3rd, 2014 3:01pm

Why are you using an obsolete method of assigning printers in the first place?  Use Group Policy.  It works across all system architectures.
Free Windows Admin Tool Kit Click here and download it now
January 4th, 2014 12:41am

You are writing way to much redundant code which makes things harder to understand. Start with this.

' get groups
Set oNet = WScript.CreateObject("WScript.Network")
Set dct = CreateObject("Scripting.Dictionary")
Set objUser = GetObject("WinNT://" & oNet.UserDomain & "/" & oNet.UserName & ",user")
For Each objGroup In objUser.Groups
    dct.Add objGroup.Name, "-"
Next

' Define printers
strUNCPrinter1 = "\\s-exch\P50 - A4 Dubbel (Kleur)"
strUNCPrinter2 = "\\s-exch\P50 - A4 Dubbel (Z-W)"
strUNCPrinter3 = "\\s-exch\P50 - A4 Enkel (Kleur)"
strUNCPrinter4 = "\\s-exch\P50 - A4 Enkel (Z-W)"
strUNCPrinter5 = "\\s-exch\P50 - TCE - Briefpapier (Z-W)"

'Remove printers
oNet.RemovePrinterConnection strUNCPrinter1
oNet.RemovePrinterConnection strUNCPrinter2
oNet.RemovePrinterConnection strUNCPrinter3
oNet.RemovePrinterConnection strUNCPrinter4
oNet.RemovePrinterConnection strUNCPrinter5

' Create printer mappings based on groupmembership
If dct.Exists("P50") Then oNet.AddWindowsPrinterConnection strUNCPrinter1
If dct.Exists("P50") Then oNet.AddWindowsPrinterConnection strUNCPrinter2
If dct.Exists("P50") Then oNet.AddWindowsPrinterConnection strUNCPrinter3
If dct.Exists("P50") Then oNet.AddWindowsPrinterConnection strUNCPrinter4
If dct.Exists("P50") Then oNet.AddWindowsPrinterConnection strUNCPrinter5






Try to run this under the user account but at a prompt.  Look at the error messages.

January 4th, 2014 12:59am

One note.  The original script as posted could never have worked due to significant coding errors.

I have seen this before.  A script exisits and ther are a bunch of older machines.  They all appear to work.  New machines are purchased and the logon script fails.  The support techs complain that thee scrip has been working because all printers or disks are mapped.

What is usually happening is that, over time, the mappings have been manually made by techs as needed.  Once the mappings are made permanent then the script failure would never be noticed.  Your script would abort with an error almost immediately.  It could never have successfully run as posted.

The script I posted fixes the biggest errors but still does no error management. Any error will cause the script to abort.  I have not used a logon script to assign printers or disks for many years.  Use GPP.  It is much easier and more reliable.

Free Windows Admin Tool Kit Click here and download it now
January 4th, 2014 1:21am

One note.  The original script as posted could never have worked due to significant coding errors.

I have seen this before.  A script exisits and ther are a bunch of older machines.  They all appear to work.  New machines are purchased and the logon script fails.  The support techs complain that thee scrip has been working because all printers or disks are mapped.

What is usually happening is that, over time, the mappings have been manually made by techs as needed.  Once the mappings are made permanent then the script failure would never be noticed.  Your script would abort with an error almost immediately.  It could never have successfully run as posted.

The script I posted fixes the biggest errors but still does no error management. Any error will cause the script to abort.  I have not used a logon script to assign printers or disks for many years.  Use GPP.  It is much easier and more rel

January 7th, 2014 1:40pm

Do you have a explenation about doing this via group policy? I couldnt find anything that explains me how it works.

I typed 'manage printers group policy' into a search engine.

This link to documentation was the second result:

Deploying Printers by Using Group Policy

Bill

Free Windows Admin Tool Kit Click here and download it now
January 7th, 2014 6:39pm

You are writing way to much redundant code which makes things harder to understand. Start with this.

' get groups
Set oNet = WScript.CreateObject("WScript.Network")
Set dct = CreateObject("Scripting.Dictionary")
Set objUser = GetObject("WinNT://" & oNet.UserDomain & "/" & oNet.UserName & ",user")
For Each objGroup In objUser.Groups
    dct.Add objGroup.Name, "-"
Next

' Define printers
strUNCPrinter1 = "\\s-exch\P50 - A4 Dubbel (Kleur)"
strUNCPrinter2 = "\\s-exch\P50 - A4 Dubbel (Z-W)"
strUNCPrinter3 = "\\s-exch\P50 - A4 Enkel (Kleur)"
strUNCPrinter4 = "\\s-exch\P50 - A4 Enkel (Z-W)"
strUNCPrinter5 = "\\s-exch\P50 - TCE - Briefpapier (Z-W)"

'Remove printers
oNet.RemovePrinterConnection strUNCPrinter1
oNet.RemovePrinterConnection strUNCPrinter2
oNet.RemovePrinterConnection strUNCPrinter3
oNet.RemovePrinterConnection strUNCPrinter4
oNet.RemovePrinterConnection strUNCPrinter5

' Create printer mappings based on groupmembership
If dct.Exists("P50") Then oNet.AddWindowsPrinterConnection strUNCPrinter1
If dct.Exists("P50") Then oNet.AddWindowsPrinterConnection strUNCPrinter2
If dct.Exists("P50") Then oNet.AddWindowsPrinterConnection strUNCPrinter3
If dct.Exists("P50") Then oNet.AddWindowsPrinterConnection strUNCPrinter4
If dct.Exists("P50") Then oNet.AddWindowsPrinterConnection strUNCPrinter5






Try to run this under the user account but at a prompt.  Look at the error messages.

January 13th, 2014 7:01am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics