[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 303
  • Last Modified:

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"
           
0
whoam
Asked:
whoam
1 Solution
 
vinnyd79Commented:
0
 
whoamAuthor Commented:
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.
0
 
whoamAuthor Commented:
I think each Case line may need a new line after it, i.le. the &_ may be the problem.  I'll try it tomorrow.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
whoamAuthor Commented:
Also need and
End Select
statement I'm guessing
0
 
Chris DentPowerShell DeveloperCommented:

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
0
 
ddepastinoCommented:
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....
0
 
whoamAuthor Commented:
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.
0
 
ddepastinoCommented:
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.....
0
 
whoamAuthor Commented:
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
0
 
whoamAuthor Commented:
Alright, stupid post, should be 'Case Else'
still working on it.
0
 
whoamAuthor Commented:
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
0
 
ddepastinoCommented:
You were close, but no cigar
Change the set cls printers line to:
Set clPrinters = WshNet.EnumPrinterConnections

WSHNetwork was not declared (Should have been WSHNet; remember option explicit......), I had copied that out of my 300 line login script where I actually had a WSHNetwork object declared, DOH!

So the whole thing is:

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

Are you ready for conditional drive/printer mappings yet?

BTW, on your "else case" post I see you still placing something in the driver parameter of your printers, are you trying to set a printer display name there?
0
 
whoamAuthor Commented:
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.
0
 
whoamAuthor Commented:
If work isnt' too busy tomorrow I should have time to finish this.

0
 
ddepastinoCommented:
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...
0
 
whoamAuthor Commented:
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
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21830526.html
Then I should be good.
Thanks
0
 
whoamAuthor Commented:
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
http://www.experts-exchange.com/Programming/Programming_Languages/Visual_Basic/Q_21831914.html
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now