Home All Groups Group Topic Archive Search About

vbscript to map network drivers and set default printer



Author
16 Mar 2007 9:31 PM
David Allen
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?

Author
17 Mar 2007 1:52 AM
Richard Mueller [MVP]
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
--
Author
19 Mar 2007 8:51 PM
David Allen
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
Author
3 Apr 2007 4:48 PM
Richard Mueller [MVP]
Show quote
"David Allen" <D**@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
Hilltop Lab - http://www.rlmueller.net
--

AddThis Social Bookmark Button