Windows 2003 VB Login Script for Mapped Drives & Printers

  • Thread starter Thread starter circulent
  • Start date Start date
C

circulent

Does anyone have a simple VB script template for mapping multiple network
drives and network printers via group policies I can use?

Thanks
 
"circulent" <circulent@discussions.microsoft.com> wrote in message
news:A4169737-056E-47C0-BA45-4C8AB4EC47F1@microsoft.com...
> Does anyone have a simple VB script template for mapping multiple network
> drives and network printers via group policies I can use?


A very simple VBScript logon script would be:
=============
' VBScript logon script program.
Option Explicit
Dim objNetwork

Set objNetwork = CreateObject("Wscript.Network")

' Map shared folders.
objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare2"
objNetwork.MapNetworkDrive "M:", "\\MyServer\MyShare1"

' Map shared printer.
objNetwork.AddWindowsPrinterConnection "\\MyServer\Printer1"

' Make this printer the default.
objNetwork.SetDefaultPrinter "\\MyServer\Printer1"
==============

If the drive letter could be in use due to persistent connections, you can
trap the possible error, attempt to remove the drive mapping, then try to
map again. The code would be similar to:
============
' Trap error if K: already in use.
On Error Resume Next
objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare1"
If (Err.Number <> 0) Then
' Restore normal error handling.
On Error GoTo 0
' Attempt to remove drive mapping.
objNetwork.RemoveNetworkDrive "K:", True, True
' Try again to map the drive.
objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare1"
End If
' Restore normal error handling.
On Error GoTo 0
===========

The above assumes you do not map according to group membership. Everyone
subject to the GPO gets the same mappings.

--
Richard Mueller
Microsoft MVP Scripting and ADSI
Hilltop Lab - http://www.rlmueller.net
--
 
OK just to make sure, if we had to map 2 drives, it would look like this?

On Error Resume Next
objNetwork.MapNetworkDrive "R:", "\\SQL1\marketing"
If (Err.Number <> 0) Then
' Restore normal error handling.
On Error GoTo 0
' Attempt to remove drive mapping.
objNetwork.RemoveNetworkDrive "R:", True, True
' Try again to map the drive.
objNetwork.MapNetworkDrive "R:", "\\SQL1\marketing"
End If
' Restore normal error handling.
On Error GoTo 0

On Error Resume Next
objNetwork.MapNetworkDrive "S:", "\\SQL1\finance"
If (Err.Number <> 0) Then
' Restore normal error handling.
On Error GoTo 0
' Attempt to remove drive mapping.
objNetwork.RemoveNetworkDrive "R:", True, True
' Try again to map the drive.
objNetwork.MapNetworkDrive "S:", "\\SQL1\finance"
End If
' Restore normal error handling.
On Error GoTo 0

"Richard Mueller [MVP]" wrote:

>
> "circulent" <circulent@discussions.microsoft.com> wrote in message
> news:A4169737-056E-47C0-BA45-4C8AB4EC47F1@microsoft.com...
> > Does anyone have a simple VB script template for mapping multiple network
> > drives and network printers via group policies I can use?

>
> A very simple VBScript logon script would be:
> =============
> ' VBScript logon script program.
> Option Explicit
> Dim objNetwork
>
> Set objNetwork = CreateObject("Wscript.Network")
>
> ' Map shared folders.
> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare2"
> objNetwork.MapNetworkDrive "M:", "\\MyServer\MyShare1"
>
> ' Map shared printer.
> objNetwork.AddWindowsPrinterConnection "\\MyServer\Printer1"
>
> ' Make this printer the default.
> objNetwork.SetDefaultPrinter "\\MyServer\Printer1"
> ==============
>
> If the drive letter could be in use due to persistent connections, you can
> trap the possible error, attempt to remove the drive mapping, then try to
> map again. The code would be similar to:
> ============
> ' Trap error if K: already in use.
> On Error Resume Next
> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare1"
> If (Err.Number <> 0) Then
> ' Restore normal error handling.
> On Error GoTo 0
> ' Attempt to remove drive mapping.
> objNetwork.RemoveNetworkDrive "K:", True, True
> ' Try again to map the drive.
> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare1"
> End If
> ' Restore normal error handling.
> On Error GoTo 0
> ===========
>
> The above assumes you do not map according to group membership. Everyone
> subject to the GPO gets the same mappings.
>
> --
> Richard Mueller
> Microsoft MVP Scripting and ADSI
> Hilltop Lab - http://www.rlmueller.net
> --
>
>
>
 
Yes, that looks good, as long as you have the "Set objNetwork" statement
ahead of this.

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

"circulent" <circulent@discussions.microsoft.com> wrote in message
news:2246D69A-614C-4ABE-8789-1D0448D8EAAB@microsoft.com...
> OK just to make sure, if we had to map 2 drives, it would look like this?
>
> On Error Resume Next
> objNetwork.MapNetworkDrive "R:", "\\SQL1\marketing"
> If (Err.Number <> 0) Then
> ' Restore normal error handling.
> On Error GoTo 0
> ' Attempt to remove drive mapping.
> objNetwork.RemoveNetworkDrive "R:", True, True
> ' Try again to map the drive.
> objNetwork.MapNetworkDrive "R:", "\\SQL1\marketing"
> End If
> ' Restore normal error handling.
> On Error GoTo 0
>
> On Error Resume Next
> objNetwork.MapNetworkDrive "S:", "\\SQL1\finance"
> If (Err.Number <> 0) Then
> ' Restore normal error handling.
> On Error GoTo 0
> ' Attempt to remove drive mapping.
> objNetwork.RemoveNetworkDrive "R:", True, True
> ' Try again to map the drive.
> objNetwork.MapNetworkDrive "S:", "\\SQL1\finance"
> End If
> ' Restore normal error handling.
> On Error GoTo 0
>
> "Richard Mueller [MVP]" wrote:
>
>>
>> "circulent" <circulent@discussions.microsoft.com> wrote in message
>> news:A4169737-056E-47C0-BA45-4C8AB4EC47F1@microsoft.com...
>> > Does anyone have a simple VB script template for mapping multiple
>> > network
>> > drives and network printers via group policies I can use?

>>
>> A very simple VBScript logon script would be:
>> =============
>> ' VBScript logon script program.
>> Option Explicit
>> Dim objNetwork
>>
>> Set objNetwork = CreateObject("Wscript.Network")
>>
>> ' Map shared folders.
>> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare2"
>> objNetwork.MapNetworkDrive "M:", "\\MyServer\MyShare1"
>>
>> ' Map shared printer.
>> objNetwork.AddWindowsPrinterConnection "\\MyServer\Printer1"
>>
>> ' Make this printer the default.
>> objNetwork.SetDefaultPrinter "\\MyServer\Printer1"
>> ==============
>>
>> If the drive letter could be in use due to persistent connections, you
>> can
>> trap the possible error, attempt to remove the drive mapping, then try to
>> map again. The code would be similar to:
>> ============
>> ' Trap error if K: already in use.
>> On Error Resume Next
>> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare1"
>> If (Err.Number <> 0) Then
>> ' Restore normal error handling.
>> On Error GoTo 0
>> ' Attempt to remove drive mapping.
>> objNetwork.RemoveNetworkDrive "K:", True, True
>> ' Try again to map the drive.
>> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare1"
>> End If
>> ' Restore normal error handling.
>> On Error GoTo 0
>> ===========
>>
>> The above assumes you do not map according to group membership. Everyone
>> subject to the GPO gets the same mappings.
>>
>> --
>> Richard Mueller
>> Microsoft MVP Scripting and ADSI
>> Hilltop Lab - http://www.rlmueller.net
>> --
>>
>>
>>
 
Rich - The drive mapping worked like a charm. Thanks. The deleting of a
previous existing drive was an bonus!

Separately, we created a general logon script for printers:

*******************
' VBScript logon script program.
Option Explicit
Dim objNetwork

Set objNetwork = CreateObject("Wscript.Network")

' Map shared printer.
objNetwork.AddWindowsPrinterConnection "\\server\DELL1710N"
objNetwork.AddWindowsPrinterConnection "\\server\DELL3100CNPS"
objNetwork.AddWindowsPrinterConnection "\\server\HPLJ2300N"

' Make this printer the default.
objNetwork.SetDefaultPrinter "\\server\DELL1710N"
*******************

What's odd here is that we're getting this error upon login:

\\domain.local\sysvol\domain.local\Policies\{5465784.....7686}\User\Scripts\Logon\logon_printers.vbs

Line: 8
Char: 1
Error: The filename, directory name, or volume label syntax is incorrect.
Code: 8007007B
Source: (null)

P.S. I checked the share name of each printer and they are correct. Am I
missing something? Thanks

"Richard Mueller [MVP]" wrote:

> Yes, that looks good, as long as you have the "Set objNetwork" statement
> ahead of this.
>
> --
> Richard Mueller
> Microsoft MVP Scripting and ADSI
> Hilltop Lab - http://www.rlmueller.net
> --
>
> "circulent" <circulent@discussions.microsoft.com> wrote in message
> news:2246D69A-614C-4ABE-8789-1D0448D8EAAB@microsoft.com...
> > OK just to make sure, if we had to map 2 drives, it would look like this?
> >
> > On Error Resume Next
> > objNetwork.MapNetworkDrive "R:", "\\SQL1\marketing"
> > If (Err.Number <> 0) Then
> > ' Restore normal error handling.
> > On Error GoTo 0
> > ' Attempt to remove drive mapping.
> > objNetwork.RemoveNetworkDrive "R:", True, True
> > ' Try again to map the drive.
> > objNetwork.MapNetworkDrive "R:", "\\SQL1\marketing"
> > End If
> > ' Restore normal error handling.
> > On Error GoTo 0
> >
> > On Error Resume Next
> > objNetwork.MapNetworkDrive "S:", "\\SQL1\finance"
> > If (Err.Number <> 0) Then
> > ' Restore normal error handling.
> > On Error GoTo 0
> > ' Attempt to remove drive mapping.
> > objNetwork.RemoveNetworkDrive "R:", True, True
> > ' Try again to map the drive.
> > objNetwork.MapNetworkDrive "S:", "\\SQL1\finance"
> > End If
> > ' Restore normal error handling.
> > On Error GoTo 0
> >
> > "Richard Mueller [MVP]" wrote:
> >
> >>
> >> "circulent" <circulent@discussions.microsoft.com> wrote in message
> >> news:A4169737-056E-47C0-BA45-4C8AB4EC47F1@microsoft.com...
> >> > Does anyone have a simple VB script template for mapping multiple
> >> > network
> >> > drives and network printers via group policies I can use?
> >>
> >> A very simple VBScript logon script would be:
> >> =============
> >> ' VBScript logon script program.
> >> Option Explicit
> >> Dim objNetwork
> >>
> >> Set objNetwork = CreateObject("Wscript.Network")
> >>
> >> ' Map shared folders.
> >> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare2"
> >> objNetwork.MapNetworkDrive "M:", "\\MyServer\MyShare1"
> >>
> >> ' Map shared printer.
> >> objNetwork.AddWindowsPrinterConnection "\\MyServer\Printer1"
> >>
> >> ' Make this printer the default.
> >> objNetwork.SetDefaultPrinter "\\MyServer\Printer1"
> >> ==============
> >>
> >> If the drive letter could be in use due to persistent connections, you
> >> can
> >> trap the possible error, attempt to remove the drive mapping, then try to
> >> map again. The code would be similar to:
> >> ============
> >> ' Trap error if K: already in use.
> >> On Error Resume Next
> >> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare1"
> >> If (Err.Number <> 0) Then
> >> ' Restore normal error handling.
> >> On Error GoTo 0
> >> ' Attempt to remove drive mapping.
> >> objNetwork.RemoveNetworkDrive "K:", True, True
> >> ' Try again to map the drive.
> >> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare1"
> >> End If
> >> ' Restore normal error handling.
> >> On Error GoTo 0
> >> ===========
> >>
> >> The above assumes you do not map according to group membership. Everyone
> >> subject to the GPO gets the same mappings.
> >>
> >> --
> >> Richard Mueller
> >> Microsoft MVP Scripting and ADSI
> >> Hilltop Lab - http://www.rlmueller.net
> >> --
> >>
> >>
> >>

>
>
>
 
Hi Rich,

Just thought I'd check with you again to see if you any input.

Thanks

"circulent" wrote:

> Rich - The drive mapping worked like a charm. Thanks. The deleting of a
> previous existing drive was an bonus!
>
> Separately, we created a general logon script for printers:
>
> *******************
> ' VBScript logon script program.
> Option Explicit
> Dim objNetwork
>
> Set objNetwork = CreateObject("Wscript.Network")
>
> ' Map shared printer.
> objNetwork.AddWindowsPrinterConnection "\\server\DELL1710N"
> objNetwork.AddWindowsPrinterConnection "\\server\DELL3100CNPS"
> objNetwork.AddWindowsPrinterConnection "\\server\HPLJ2300N"
>
> ' Make this printer the default.
> objNetwork.SetDefaultPrinter "\\server\DELL1710N"
> *******************
>
> What's odd here is that we're getting this error upon login:
>
> \\domain.local\sysvol\domain.local\Policies\{5465784.....7686}\User\Scripts\Logon\logon_printers.vbs
>
> Line: 8
> Char: 1
> Error: The filename, directory name, or volume label syntax is incorrect.
> Code: 8007007B
> Source: (null)
>
> P.S. I checked the share name of each printer and they are correct. Am I
> missing something? Thanks
>
> "Richard Mueller [MVP]" wrote:
>
> > Yes, that looks good, as long as you have the "Set objNetwork" statement
> > ahead of this.
> >
> > --
> > Richard Mueller
> > Microsoft MVP Scripting and ADSI
> > Hilltop Lab - http://www.rlmueller.net
> > --
> >
> > "circulent" <circulent@discussions.microsoft.com> wrote in message
> > news:2246D69A-614C-4ABE-8789-1D0448D8EAAB@microsoft.com...
> > > OK just to make sure, if we had to map 2 drives, it would look like this?
> > >
> > > On Error Resume Next
> > > objNetwork.MapNetworkDrive "R:", "\\SQL1\marketing"
> > > If (Err.Number <> 0) Then
> > > ' Restore normal error handling.
> > > On Error GoTo 0
> > > ' Attempt to remove drive mapping.
> > > objNetwork.RemoveNetworkDrive "R:", True, True
> > > ' Try again to map the drive.
> > > objNetwork.MapNetworkDrive "R:", "\\SQL1\marketing"
> > > End If
> > > ' Restore normal error handling.
> > > On Error GoTo 0
> > >
> > > On Error Resume Next
> > > objNetwork.MapNetworkDrive "S:", "\\SQL1\finance"
> > > If (Err.Number <> 0) Then
> > > ' Restore normal error handling.
> > > On Error GoTo 0
> > > ' Attempt to remove drive mapping.
> > > objNetwork.RemoveNetworkDrive "R:", True, True
> > > ' Try again to map the drive.
> > > objNetwork.MapNetworkDrive "S:", "\\SQL1\finance"
> > > End If
> > > ' Restore normal error handling.
> > > On Error GoTo 0
> > >
> > > "Richard Mueller [MVP]" wrote:
> > >
> > >>
> > >> "circulent" <circulent@discussions.microsoft.com> wrote in message
> > >> news:A4169737-056E-47C0-BA45-4C8AB4EC47F1@microsoft.com...
> > >> > Does anyone have a simple VB script template for mapping multiple
> > >> > network
> > >> > drives and network printers via group policies I can use?
> > >>
> > >> A very simple VBScript logon script would be:
> > >> =============
> > >> ' VBScript logon script program.
> > >> Option Explicit
> > >> Dim objNetwork
> > >>
> > >> Set objNetwork = CreateObject("Wscript.Network")
> > >>
> > >> ' Map shared folders.
> > >> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare2"
> > >> objNetwork.MapNetworkDrive "M:", "\\MyServer\MyShare1"
> > >>
> > >> ' Map shared printer.
> > >> objNetwork.AddWindowsPrinterConnection "\\MyServer\Printer1"
> > >>
> > >> ' Make this printer the default.
> > >> objNetwork.SetDefaultPrinter "\\MyServer\Printer1"
> > >> ==============
> > >>
> > >> If the drive letter could be in use due to persistent connections, you
> > >> can
> > >> trap the possible error, attempt to remove the drive mapping, then try to
> > >> map again. The code would be similar to:
> > >> ============
> > >> ' Trap error if K: already in use.
> > >> On Error Resume Next
> > >> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare1"
> > >> If (Err.Number <> 0) Then
> > >> ' Restore normal error handling.
> > >> On Error GoTo 0
> > >> ' Attempt to remove drive mapping.
> > >> objNetwork.RemoveNetworkDrive "K:", True, True
> > >> ' Try again to map the drive.
> > >> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare1"
> > >> End If
> > >> ' Restore normal error handling.
> > >> On Error GoTo 0
> > >> ===========
> > >>
> > >> The above assumes you do not map according to group membership. Everyone
> > >> subject to the GPO gets the same mappings.
> > >>
> > >> --
> > >> Richard Mueller
> > >> Microsoft MVP Scripting and ADSI
> > >> Hilltop Lab - http://www.rlmueller.net
> > >> --
> > >>
> > >>
> > >>

> >
> >
> >
 
Back
Top