Results 1 to 5 of 5

Thread: vbscript to map network drivers and set default printer

  1. #1
    David Allen Guest

    vbscript to map network drivers and set default printer

    I have a user who switches between offices
    I need to write a script that will set his drive mappings and switch his
    default printer
    depending on LOGONSERVER
    how can I do that?



  2. #2
    Richard Mueller [MVP] Guest

    Re: vbscript to map network drivers and set default printer

    David Allen wrote:

    >I have a user who switches between offices
    > I need to write a script that will set his drive mappings and switch his
    > default printer
    > depending on LOGONSERVER
    > how can I do that?
    >


    A VBScript program to map a drive and connect a printer according to the
    value of the LOGONSERVER environment variable could be similar to below.
    This assumes the user always authenticates to the same server, which may not
    be a good assumption:
    ========
    Option Explicit

    Dim objNetwork, objShell, objEnv, strServer

    Set objNetwork = CreateObject("Wscript.Network")

    Set objShell = CreateObject("Wscript.Shell")
    Set objEnv = objShell.Environment("process")
    strServer = objEnv("LOGONSERVER")

    Select Case LCase(strServer)
    Case "\\server1"
    On Error Resume Next
    objNetwork.MapNetworkDrive "K:", "\\Server1\sales"
    If (Err.Number <> 0) Then
    On Error GoTo 0
    objNetwork.RemoveNetworkDrive "K:", True, True
    objNetwork.MapNetworkDrive "K:", "\\Server1\sales"
    End If
    On ErrorGoTo 0
    objNetwork.AddWindowsPrinterConnection "\\Server1\Printer1"
    objNetwork.SetDefaultPrinter "\\Server1\Printer1"
    Case "\\server2"
    On Error Resume Next
    objNetwork.MapNetworkDrive "K:", "\\Server2\Acctg"
    If (Err.Number <> 0) Then
    On Error GoTo 0
    objNetwork.RemoveNetworkDrive "K:", True, True
    objNetwork.MapNetworkDrive "K:", "\\Server2\Acctg"
    End If
    On ErrorGoTo 0
    objNetwork.AddWindowsPrinterConnection "\\Server2\Printer2"
    objNetwork.SetDefaultPrinter "\\Server2\Printer2"
    End Select
    ===========
    If the locations have domains with more than one Domain Controller, you
    should check the site, the domain, or something else that will not vary.
    Assuming different domains, you can use code similar to:
    =========
    Option Explicit

    Dim objRootDSE, strDNSDomain

    Set objRootDSE = GetObject(LDAP://RootDSE)
    strDNSDomain = objRootDSE.Get("defaultNamingContext")
    Select Case LCase(strDNSDomain)
    Case "dc=domain1,dc=com"
    ' .... code to map drive and printer.
    Case "dc=domain2,dc=com"
    ' .... code to map drive and printer.
    End Select
    =======
    Assuming each office has a different site in AD:
    ============
    Option Explicit

    Dim objSysInfo, strSite

    Set objSysInfo = CreateObject("ADSystemInfo")
    strSite = objSysInfo.SiteName
    Select Case LCase(strSite)
    Case "mysite1"
    ' .... code to map drive and printer.
    Case "mysite2"
    ' .... code to map drive and printer.
    End Select
    ============
    Don't assume only one possible value for LOGONSERVER at a site, unless logon
    always fails if that machine is not available.

    --
    Richard Mueller
    Microsoft MVP Scripting and ADSI
    Hilltop Lab - http://www.rlmueller.net
    --



  3. #3
    David Allen Guest

    Re: vbscript to map network drivers and set default printer

    The first option works for me

    I'm having problems getting it to run

    -----------------
    Option Explicit

    Dim objNetwork, objShell, objEnv, strServer

    Set objNetwork = CreateObject("Wscript.Network")

    Set objShell = CreateObject("Wscript.Shell")
    Set objEnv = objShell.Environment("process")

    strServer = objEnv("LOGONSERVER")

    Select Case LCase(strServer)
    Case "\\OC1-AD01"
    objNetwork.AddWindowsPrinterConnection
    "\\OC1-S02\OrangeCounty-HP5100-3"
    objNetwork.SetDefaultPrinter "\\OC1-S02\OrangeCounty-HP5100-3"
    wscript.echo "OC"
    Case "\\LA1-AD01"
    objNetwork.AddWindowsPrinterConnection
    "\\LA1-S02\LosAngeles-Barrymore-5200"
    objNetwork.SetDefaultPrinter "\\LA1-S02\LosAngeles-Barrymore-5200"
    wscript.echo "LA"
    End Select

    wscript.echo "DONE"
    -------------------

    All I get is the DONE idiot box, nothing else
    If I move the printer connections outside of the logonserver check they do
    work



  4. #4
    Richard Mueller [MVP] Guest

    Re: vbscript to map network drivers and set default printer

    "David Allen" <Dod@pound.com> wrote in message
    news:%23fzgUhmaHHA.2436@TK2MSFTNGP06.phx.gbl...
    > The first option works for me
    >
    > I'm having problems getting it to run
    >
    > -----------------
    > Option Explicit
    >
    > Dim objNetwork, objShell, objEnv, strServer
    >
    > Set objNetwork = CreateObject("Wscript.Network")
    >
    > Set objShell = CreateObject("Wscript.Shell")
    > Set objEnv = objShell.Environment("process")
    >
    > strServer = objEnv("LOGONSERVER")
    >
    > Select Case LCase(strServer)
    > Case "\\OC1-AD01"
    > objNetwork.AddWindowsPrinterConnection
    > "\\OC1-S02\OrangeCounty-HP5100-3"
    > objNetwork.SetDefaultPrinter "\\OC1-S02\OrangeCounty-HP5100-3"
    > wscript.echo "OC"
    > Case "\\LA1-AD01"
    > objNetwork.AddWindowsPrinterConnection
    > "\\LA1-S02\LosAngeles-Barrymore-5200"
    > objNetwork.SetDefaultPrinter "\\LA1-S02\LosAngeles-Barrymore-5200"
    > wscript.echo "LA"
    > End Select
    >
    > wscript.echo "DONE"
    > -------------------
    >
    > All I get is the DONE idiot box, nothing else
    > If I move the printer connections outside of the logonserver check they do
    > work
    >


    The "Select Case" statement is comparing the lower case string, but your
    case statements are all upper case. You may want to use:

    Select Case UCase(strServer)

    --
    Richard Mueller
    Microsoft MVP Scripting and ADSI

  5. #5
    Join Date
    May 2011
    Posts
    3

    Re: vbscript to map network drivers and set default printer

    What if you want to map the drives according to their IP scheme? We have the second number unique to the location.

Similar Threads

  1. Script to Install Printer Drivers w/o adding printer
    By witecoko in forum Windows XP Support
    Replies: 7
    Last Post: 13-03-2013, 11:32 AM
  2. How to set a default printer by network in Windows 7
    By Itronix in forum Tips & Tweaks
    Replies: 1
    Last Post: 27-05-2011, 03:52 AM
  3. Replies: 3
    Last Post: 14-01-2011, 10:58 PM
  4. Replies: 4
    Last Post: 08-12-2010, 10:08 AM
  5. Vista - default printer doesn't stay as default
    By Freddie Kang in forum Vista Help
    Replies: 8
    Last Post: 19-04-2010, 03:12 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,398,722.10527 seconds with 17 queries