Link to home
Start Free TrialLog in
Avatar of whoam
whoam

asked on

Mapping Printers based on AD Sites

I am trying to map printers based on AD site.  I'm using the following approach.

 '+++++++BEGIN CREATING PRNTERS Per AD SITE++++++++++
Set objSysInfo = WScript.CreateObject("ADSystemInfo")
Set objNetwork = WScript.CreateObject("Wscript.Network")
strSiteName = objSysInfo.SiteName

    Select Case strSiteName  
        Case "Site1" &_    
            net.AddWindowsPrinterConnection "\\tb221002\PRMR",  "MB_HP"
            net.AddWindowsPrinterConnection "\\tb221002\PBMB",  "PBMB"
        Case "Site2" &_
             net.AddWindowsPrinterConnection "\\tb052002\HP4200",  "HP 4200"
             net.AddWindowsPrinterConnection "\\tb052002\HP4250",  "HP 4250"
        Case "Site3" &_
            net.AddWindowsPrinterConnection "\\tb022702\HP4250",  "HP 4200"
'++++++++++++++++++++++END PRINTER MAPPING PER AD SITE++++++++++++++++++++++++++++++++++++  

I'm getting an "Error 1024, Expected Statement"
at *here*
 
            net.AddWindowsPrinterConnection *here* "\\tb221002\PRMR",  "MB_HP"
           
Avatar of vinnyd79
vinnyd79

Avatar of whoam

ASKER

It works fine if I do an IF THEN based on AD group memebership, it's just when I pull the method into the select case.
Avatar of whoam

ASKER

I think each Case line may need a new line after it, i.le. the &_ may be the problem.  I'll try it tomorrow.
Avatar of whoam

ASKER

Also need and
End Select
statement I'm guessing
Avatar of Chris Dent

You're correct... the syntax for Case is:

Select Case <Variant>
    Case "<Value1>"
        ' Statements
    Case "<Value2>"
        ' Statements
End Select

The ampersand (&) is used for concatenation and the Underscore (_) is used to tell the script processer that the statement is continued on the next line (useful to make things look neater).

Chris
There are a lot of problems with that script.

-You are referencing variables that don't exist:
NET.addwindowsprinterconnection
<should be>
OBJNETWORK.addwindowsprinterconnection
Make "Option Explicit" the first line in the script so that you can't script for a non-declared variable

-The second parameter for AddWindowsPrinterConnection is a Driver name, not the display name for the printer.
You don't need to specify anything but the UNC for the printer share for this function:
OBJNETWORK.AddWindowsPrinterConnection "\\tb221002\PRMR",  "MB_HP"
<should be>
OBJNETWORK.AddWindowsPrinterConnection "\\tb221002\PRMR"

-As mentioned above you need "End Select" at the end of the "Select Case" statement
-As mentioned above you don't need the &_ after the Case statements

-You should lower or upper your comparisons to make sure you don't miss a site install due to capital or lowercase differences in letters'
All together now....

'Start Script
Option Explicit
Dim objSysInfo, ObjNetwork

Set objSysInfo = WScript.CreateObject("ADSystemInfo")

Set objNetwork = WScript.CreateObject("Wscript.Network")
    Select Case UCASE(objSysInfo.SiteName)
        Case "SITE1"  
            objNetwork.AddWindowsPrinterConnection "\\tb221002\PRMR"
            objNetwork.AddWindowsPrinterConnection "\\tb221002\PBMB"
        Case "SITE2"
             objNetwork.AddWindowsPrinterConnection "\\tb052002\HP4200"
             objNetwork.AddWindowsPrinterConnection "\\tb052002\HP4250"
        Case "SITE3"
            objNetwork.AddWindowsPrinterConnection "\\tb022702\HP4250"
        Case else
            msgbox "no printers were added"
    End Select

'End Script

You will problably find these two things usefull also:

Sub RemoveAllNetPrinters
   dim WSHNet
   dim clprinters, i
   Set WSHNet= WScript.CreateObject("Wscript.Network")
   Set clPrinters = WshNetwork.EnumPrinterConnections
   On Error Resume Next
   For i = 0 to clPrinters.Count - 1 Step 2
      WSHNet.RemovePrinterConnection clPrinters.Item(i+1), true
   Next
End Sub

Set a default printer AFTER you add a printer:

objNetwork.AddWindowsPrinterConnection "\\tb221002\PRMR"
objNetwork.AddWindowsPrinterConnection "\\tb221002\PBMB"
objNetwork.SetDefaultPrinter "\\tb221002\PRMR"

If you want to get real fancy you could actually assign printers based on:
-OU location (user or computer)
-User group membership
-Computer group membership
-Indexed container attributes

Good Luck....
Avatar of whoam

ASKER

Sorry, what I show is a small snip from the whole script.  I missed the DIM lines.  While I haven't had time to test the answers I was wondering if there was a way to assign a friendly name to a printer added via a UNC.  I had thought I could do it after the UNC as above, but I guess not.
You would set that info on the print server, in my environment we use descriptions like:

Main Copier Room (Color)
Main Copier Room (B & W)

It's the first field in the properties pages of the printer (on the server) just above the location field.  Microsoft didn't put a field name next to that property like the rest, how odd for them to be inconsistent.....
Avatar of whoam

ASKER

I get an error 800A03f6, Expeted 'End" at the beginning of the else case line

Set objSysInfo = WScript.CreateObject("ADSystemInfo")
Set objNetwork = WScript.CreateObject("Wscript.Network")
strSiteName = objSysInfo.SiteName

    Select Case strSiteName
        Case "Beach"
            net.AddWindowsPrinterConnection "\\tb221002\MyeHP",  "MB_HP"
            net.AddWindowsPrinterConnection "\\tb221002\PBMB",  "PBMB"
        Case "ville"
             net.AddWindowsPrinterConnection "\\tb052002\HP4200",  " HP 4200"
             net.AddWindowsPrinterConnection "\\tb052002\HP4250",  " HP 4250"
        Case "West"
            net.AddWindowsPrinterConnection "\\tb022702\HP4250",  " HP 4200"
        Else Case
            MsgBox "No site detected"
        End Select
Avatar of whoam

ASKER

Alright, stupid post, should be 'Case Else'
still working on it.
Avatar of whoam

ASKER

I get an error code Code 800A01A8 - Object required
when i try to use your print remove subroutine

looks like ...

Sub RemoveAllNetPrinters
   dim WSHNet
   dim clprinters, i
   Set WSHNet= WScript.CreateObject("Wscript.Network")
   Set clPrinters = WshNetwork.EnumPrinterConnections
*the above line should read...*
  Set clPrinters = WSHNet.creatobject("Wscript.network")
   On Error Resume Next
   For i = 0 to clPrinters.Count - 1 Step 2
     WSHNet.RemovePrinterConnection clPrinters.Item(i+1), true
   Next
End Sub
ASKER CERTIFIED SOLUTION
Avatar of ddepastino
ddepastino

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
Avatar of whoam

ASKER

So the enumPrinterConnections method will only address network printers?

As for the driver parameters, I thought I had gotten rid of them, but I guess not, will do that next.

The problem with this is there are so many ways to do things, that while I'm fixing one problem, I think of a new way to do something else.  It's fun, but I wish I was better at it.  Maybe after a few more months.
Avatar of whoam

ASKER

If work isnt' too busy tomorrow I should have time to finish this.

Yep, local printers are not affected by that sub.

I must encourage you to spend as much time with this stuff as possible, it will reduce the amount of work you have to do in the long run.  I started with WSCRIPT and went on to do VB6 and now I'm starting VB.Net.  I administer a school system where each student has their own logon (2000+ students); in the past 3 years I haven't created a single account.  I made an application that gets info from a district student database to create accounts, the teachers use this to create all the student accounts on the fly, literally a single click operation.  The app creates the user folder, assigns group memberships, logon scripts and even NTFS permissions.  The groups are tied to scripts and other apps/scripts that assign network drives, printers, and even access to deploying/running other applications.  After the accounts are created teachers can change student passwords and lock/unlock accounts when necessary, leaving our small IT department free to tinker with more complex issues.  Keep hacking away at it, automation is a very sweet thing...
Avatar of whoam

ASKER

I know I'm dragging my feet on the points, but I can't determine if this part work until I get the whole script working.  If you wouldn't mind looking at
https://www.experts-exchange.com/questions/21830526/Using-OU-membership-to-assign-drives-or-other-resources.html
Then I should be good.
Thanks
Avatar of whoam

ASKER

Okay, I took what you gave me and put it all together.  We are close but just not there yet.  If you could, follow to
https://www.experts-exchange.com/questions/21831914/AD-VBasic-Login-Script-Long.html