+ Post New Thread
Results 1 to 9 of 9
Scripts Thread, Set Default printer according to computers OU in Coding and Web Development; I am so lost with this, never done vbs scripts before but i'm looking to create a script that looks ...
  1. #1
    Pyroman's Avatar
    Join Date
    Sep 2007
    Posts
    1,160
    Thank Post
    413
    Thanked 130 Times in 96 Posts
    Rep Power
    70

    Set Default printer according to computers OU

    I am so lost with this, never done vbs scripts before but i'm looking to create a script that looks at the computers OU (in this case "upper" or "lower") and then sets the defaulkt printer.

    I have a batch file that actually adds the printer connections all i was hoping to do was add in a .vbs to the end of the batch script that ran after the printers had been added to then set the default printer based on location HELP!!!

    This is what i have so far and isn't working but hopefully you guys should be able to tell me if i'm a n idiot and completely missing something

    Code:
    Set objADInfo = CreateObject("ADSystemInfo")
    strUserOU = objADInfo.UserName
    strComputerOU = objADInfo.ComputerName
    
    If InStr(1, strComputerOU,"OU=upper OU") > 0 Then
    objNetwork.SetDefaultPrinter("\\*servername*\topcorri")
    End If
    edit: the batch file that sets printers also sets mapped network drives etc. and is a USER logon script and i want ot set default printer by COMPUER

    edit2: i'm not sure if it even has to be a vbs file can you determine the computers OU and set default printer using just the batch file?
    Last edited by Pyroman; 27th April 2009 at 10:23 AM.

  2. #2

    SpuffMonkey's Avatar
    Join Date
    Jul 2005
    Posts
    2,192
    Thank Post
    52
    Thanked 270 Times in 178 Posts
    Rep Power
    131
    Below is (a cut down version of)our solution - its run from a vbs file attached to login scripts area in the GPO - seems to work OK



    ' Get the Computer Name

    sCompPath = oAdSysInfo.ComputerName


    ' Set the printer dependent on the OU name for the PC

    if InStr (1, sCompPath, "OU=ICT", 1) then
    WShNetwork.AddWindowsPrinterConnection "\\server9\IT_1300n"
    WShNetwork.SetDefaultPrinter "\\server9\IT_1300n"
    elseif InStr(1, SCompPath, "OU=B29", 1) then
    WShNetwork.AddWindowsPrinterConnection "\\server9\B29_3300"
    WShNetwork.SetDefaultPrinter "\\server9\B29_3300"

    *lots more elseif's here - I'm sure theres a more elegant way - but this works...

    else
    WScript.Echo "A default printer has not been assigned. Please contact IT so that the problem can be resolved"
    end if

  3. Thanks to SpuffMonkey from:

    Pyroman (27th April 2009)

  4. #3
    Pyroman's Avatar
    Join Date
    Sep 2007
    Posts
    1,160
    Thank Post
    413
    Thanked 130 Times in 96 Posts
    Rep Power
    70
    Cheers, nice 1!

    managed to get it running.

  5. #4
    tazz's Avatar
    Join Date
    May 2007
    Location
    Notts
    Posts
    224
    Thank Post
    48
    Thanked 16 Times in 9 Posts
    Rep Power
    17
    Hi

    Trying to use Spuffmonkey's script for our network (vbs newb). This is what I've got so foar:

    ' Get the Computer Name

    sCompPath = oAdSysInfo.ComputerName


    ' Set the printer dependent on the OU name for the PC

    if InStr (1, sCompPath, "OU=R19", 1) then
    WShNetwork.AddWindowsPrinterConnection "\\servername\R19-Printer"
    WShNetwork.SetDefaultPrinter "\\servername\R19-Printer"
    elseif InStr(1, SCompPath, "OU=R20", 1) then
    WShNetwork.AddWindowsPrinterConnection "\\servername\R20-printer"
    WShNetwork.SetDefaultPrinter "\\servername\R20-printer"

    else
    WScript.Echo "A default printer has not been assigned. Please contact IT so that the problem can be
    resolved"
    end if

    I get the attached error:

    Now, I think its me being thick as the computer is in the OU called R19, but the computer does not have a name that begins with R19 - is that the problem?
    Attached Images Attached Images

  6. #5

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,213
    Thank Post
    2,766
    Thanked 935 Times in 875 Posts
    Rep Power
    343
    Quote Originally Posted by tazz View Post
    Hi

    Trying to use Spuffmonkey's script for our network (vbs newb). This is what I've got so foar:

    ' Get the Computer Name
    Set objADInfo = CreateObject("ADSystemInfo")

    sCompPath = oAdSysInfo.ComputerName


    ' Set the printer dependent on the OU name for the PC

    if InStr (1, sCompPath, "OU=R19", 1) then
    WShNetwork.AddWindowsPrinterConnection "\\servername\R19-Printer"
    WShNetwork.SetDefaultPrinter "\\servername\R19-Printer"
    elseif InStr(1, SCompPath, "OU=R20", 1) then
    WShNetwork.AddWindowsPrinterConnection "\\servername\R20-printer"
    WShNetwork.SetDefaultPrinter "\\servername\R20-printer"

    else
    WScript.Echo "A default printer has not been assigned. Please contact IT so that the problem can be
    resolved"
    end if

    I get the attached error:

    Now, I think its me being thick as the computer is in the OU called R19, but the computer does not have a name that begins with R19 - is that the problem?

    You need to set the variable and create the object ie


    Set objADInfo = CreateObject("ADSystemInfo")

    Then you can use the properties of the variable so in this case

    sCompPath = oAdSysInfo.ComputerName

    oAdSysInfo notice that this is what you Set and you are accessing a property from the oAdSysInfo which is ComputerName ?

  7. #6

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,144
    Thank Post
    113
    Thanked 518 Times in 447 Posts
    Blog Entries
    2
    Rep Power
    121
    Quote Originally Posted by SpuffMonkey View Post
    *lots more elseif's here - I'm sure theres a more elegant way - but this works...
    elegance needs the case statement and a little bit of tweaking :-)

    Code:
    sCompPath = oAdSysInfo.ComputerName
    sBits=split(sCompPath,",") 'split up the name at the commas and store in an array
    sOU=lcase(sBits(1)) 'take the second bit of the name (item 0 is the computername)
    
    select case sOU
      case sOU="ou=r19"
        sPrinter="\\server\printerr19"
      case sOU="ou=r20"
        sPrinter="\\server\printerr20"
      case else
        sPrinter="\\server\mainlibrary"
    end select
    
    WShNetwork.AddWindowsPrinterConnection sPrinter
    WShNetwork.SetDefaultPrinter sPrinter

    Note that this also allows you to have a default if there's nothing specific ("mainlibrary" at the end)

  8. #7

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,213
    Thank Post
    2,766
    Thanked 935 Times in 875 Posts
    Rep Power
    343
    Quote Originally Posted by srochford View Post
    elegance needs the case statement and a little bit of tweaking :-)

    Code:
    sCompPath = oAdSysInfo.ComputerName
    sBits=split(sCompPath,",") 'split up the name at the commas and store in an array
    sOU=lcase(sBits(1)) 'take the second bit of the name (item 0 is the computername)
    
    select case sOU
      case sOU="ou=r19"
        sPrinter="\\server\printerr19"
      case sOU="ou=r20"
        sPrinter="\\server\printerr20"
      case else
        sPrinter="\\server\mainlibrary"
    end select
    
    WShNetwork.AddWindowsPrinterConnection sPrinter
    WShNetwork.SetDefaultPrinter sPrinter
    Note that this also allows you to have a default if there's nothing specific ("mainlibrary" at the end)
    I did not see that comment otherwise I would of replied the same lol

  9. #8

    Join Date
    Aug 2005
    Location
    London
    Posts
    3,144
    Thank Post
    113
    Thanked 518 Times in 447 Posts
    Blog Entries
    2
    Rep Power
    121
    Quote Originally Posted by mac_shinobi View Post
    I did not see that comment otherwise I would have replied the same lol
    They don't call me Mr Elegant for nothing :-)

  10. #9

    mac_shinobi's Avatar
    Join Date
    Aug 2005
    Posts
    9,213
    Thank Post
    2,766
    Thanked 935 Times in 875 Posts
    Rep Power
    343
    Quote Originally Posted by srochford View Post
    They don't call me Mr Elegant for nothing :-)
    Alright smarty pants - I am still stuck on that lotus notes question lol

SHARE:
+ Post New Thread

Similar Threads

  1. How do i set a default font? + menu question
    By ianaddisonuk in forum EduGeek Joomla 1.5 Package
    Replies: 0
    Last Post: 22nd August 2008, 07:56 PM
  2. Replies: 13
    Last Post: 20th February 2008, 04:29 PM
  3. Set Default Gateway for all users
    By link470 in forum Wireless Networks
    Replies: 2
    Last Post: 15th January 2008, 10:30 AM
  4. Set Default Printer to Machines with Local Printers
    By Heebeejeebee in forum Windows
    Replies: 15
    Last Post: 5th February 2007, 03:00 PM
  5. Cannot set default printer Server 2003
    By tosca925 in forum Windows
    Replies: 2
    Last Post: 26th August 2005, 03:49 PM

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •