Go Back   TechArena Community > Technical Support > Computer Help > Windows Server > Windows Server Help
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



vbscript to map network drivers and set default printer

Windows Server Help


Reply
 
Thread Tools Search this Thread
  #1  
Old 17-03-2007
David Allen
 
Posts: n/a
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?



Reply With Quote
  #2  
Old 17-03-2007
Richard Mueller [MVP]
 
Posts: n/a
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
--


Reply With Quote
  #3  
Old 20-03-2007
David Allen
 
Posts: n/a
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


Reply With Quote
  #4  
Old 03-04-2007
Richard Mueller [MVP]
 
Posts: n/a
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
Reply With Quote
  #5  
Old 03-05-2011
Member
 
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.
Reply With Quote
Reply

  TechArena Community > Technical Support > Computer Help > Windows Server > Windows Server Help
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "vbscript to map network drivers and set default printer"
Thread Thread Starter Forum Replies Last Post
Script to Install Printer Drivers w/o adding printer witecoko Windows XP Support 7 13-03-2013 11:32 AM
How to set a default printer by network in Windows 7 Itronix Tips & Tweaks 1 27-05-2011 03:52 AM
how to enable local printer AND disable network printer- printer w Robbin M Windows XP Support 3 14-01-2011 09:58 PM
ADD Printer in Windows 7 Cannot connect to the network printer and shows Access denied aFRODITA Networking & Security 4 08-12-2010 09:08 AM
Vista - default printer doesn't stay as default Freddie Kang Vista Help 8 19-04-2010 03:12 AM


All times are GMT +5.5. The time now is 09:46 AM.