From: dlc on
is it possible to have a script to map a drive letter to a directory without
using subst?
From: Richard Mueller [MVP] on

"dlc" <dlc(a)discussions.microsoft.com> wrote in message
news:F3D74709-39DE-45F1-8A46-851C1C47D47F(a)microsoft.com...
> is it possible to have a script to map a drive letter to a directory
> without
> using subst?

Yes. It's been a long time since I've seen subst used. It only works to map
local drives, but is actually faster as it does not use the network stack.
However, local drive mappings are rare, most are remote resources. Use the
MapNetworkDrive method of the wshNetwork object to map a share to a drive in
VBScript. For example:
=========
Option Explicit
Dim objNetwork, strShare, strDrive

strDrive = "K:"
strShare = "\\MyServer\MyShare"

Set objNetwork = CreateObject("Wscript.Network")
objNetwork.MapNetworkDrive strDrive, strShare
=======
The mapping will fail if the drive is in use, perhaps because the drive was
previously mapped as persistent. One way to handle this is to trap the
possible error, then attempt to remove the mapping (including making the
mapping no longer persistent), then try again to map the drive. For example:
=========
Option Explicit
Dim objNetwork, strShare, strDrive

strDrive = "K:"
strShare = "\\MyServer\MyShare"

Set objNetwork = CreateObject("Wscript.Network")

' Trap possible error if drive in use.
objNetwork.MapNetworkDrive strDrive, strShare
If (Err.Number <> 0) Then
' Restore normal error handling.
On Error GoTo 0
' Remove possible existing mapping. The first True
' removes the mapping even if it is in use. The second
' True makes the mapping no longer persistent.
objNetwork.RemoveNetworkDrive sDrive, True, True
' Try again to map the drive. If an error
' is raised, allow normal error handling so we
' see the error message.
objNetwork.MapNetworkDrive strDrive, strShare
End If
On Error GoTo 0

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


From: Richard Mueller [MVP] on

"Richard Mueller [MVP]" <rlmueller-nospam(a)ameritech.nospam.net> wrote in
message news:ecLGFTT2IHA.5832(a)TK2MSFTNGP02.phx.gbl...
>
> "dlc" <dlc(a)discussions.microsoft.com> wrote in message
> news:F3D74709-39DE-45F1-8A46-851C1C47D47F(a)microsoft.com...
>> is it possible to have a script to map a drive letter to a directory
>> without
>> using subst?
>
> Yes. It's been a long time since I've seen subst used. It only works to
> map local drives, but is actually faster as it does not use the network
> stack. However, local drive mappings are rare, most are remote resources.
> Use the MapNetworkDrive method of the wshNetwork object to map a share to
> a drive in VBScript. For example:
> =========
> Option Explicit
> Dim objNetwork, strShare, strDrive
>
> strDrive = "K:"
> strShare = "\\MyServer\MyShare"
>
> Set objNetwork = CreateObject("Wscript.Network")
> objNetwork.MapNetworkDrive strDrive, strShare
> =======
> The mapping will fail if the drive is in use, perhaps because the drive
> was previously mapped as persistent. One way to handle this is to trap the
> possible error, then attempt to remove the mapping (including making the
> mapping no longer persistent), then try again to map the drive. For
> example:
> =========
> Option Explicit
> Dim objNetwork, strShare, strDrive
>
> strDrive = "K:"
> strShare = "\\MyServer\MyShare"
>
> Set objNetwork = CreateObject("Wscript.Network")
>
> ' Trap possible error if drive in use.
> objNetwork.MapNetworkDrive strDrive, strShare
> If (Err.Number <> 0) Then
> ' Restore normal error handling.
> On Error GoTo 0
> ' Remove possible existing mapping. The first True
> ' removes the mapping even if it is in use. The second
> ' True makes the mapping no longer persistent.
> objNetwork.RemoveNetworkDrive sDrive, True, True
> ' Try again to map the drive. If an error
> ' is raised, allow normal error handling so we
> ' see the error message.
> objNetwork.MapNetworkDrive strDrive, strShare
> End If
> On Error GoTo 0
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab - http://www.rlmueller.net
> --
>
>

Also, here is an example VBScript logon script from my web site:

http://www.rlmueller.net/Logon3.htm

This includes a function to map drives using the logic I described above.
The example also has a function to check group membership so you can map
drives according to group membership, even if the groups are nested. It also
maps a printer according to the group the local computer is a member of.

--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--


From: Tom Lavedas on
On Jun 28, 11:36 am, "Richard Mueller [MVP]" <rlmueller-
nos...(a)ameritech.nospam.net> wrote:
> "dlc" <d...(a)discussions.microsoft.com> wrote in message
>
> news:F3D74709-39DE-45F1-8A46-851C1C47D47F(a)microsoft.com...
>
> > is it possible to have a script to map a drive letter to a directory
> > without
> > using subst?
>
> Yes. It's been a long time since I've seen subst used. It only works to map
> local drives, but is actually faster as it does not use the network stack..
> However, local drive mappings are rare, most are remote resources. Use the
> MapNetworkDrive method of the wshNetwork object to map a share to a drive in
> VBScript. For example:
> =========
> Option Explicit
> Dim objNetwork, strShare, strDrive
>
> strDrive = "K:"
> strShare = "\\MyServer\MyShare"
>
> Set objNetwork = CreateObject("Wscript.Network")
> objNetwork.MapNetworkDrive strDrive, strShare
> =======
> The mapping will fail if the drive is in use, perhaps because the drive was
> previously mapped as persistent. One way to handle this is to trap the
> possible error, then attempt to remove the mapping (including making the
> mapping no longer persistent), then try again to map the drive. For example:
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab -http://www.rlmueller.net
> --

Well, I'll be ...

I woulda never ...

To tell the truth I had to see it for myself before I believed it -
but that's good to know. I was just blind to the thought of the local
machine as a mappable network resource - relative to itself. Duh!

The only drawback to this approach is that the folder must be shared
before it can be connected.

I reworked your approach to create the share and then do the mapping,
but it's hardly complete application. It would need to accept an
input argument path and select or request a drive letter and also
provide a means of disconnecting. All quite do-able, now that the
idea of using a network connection to a local resource in place of
SUBST is revealed.

Dim oNet, sShare, sDrive

sDrive = "K:"
sSharePath = "C:\Someplace\Testing"
sShareName = "Testing"

set oNet = CreateObject("Wscript.Network")
sNetShare = "\\" & oNet.ComputerName & "\" & sShareName
wsh.echo sNetShare

Const FILE_SHARE = 0
Const MAXIMUM_CONNECTIONS = 1

Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\.\root\cimv2")

Set oNewShare = objWMIService.Get("Win32_Share")

errRet = oNewShare.Create(sSharePath, sShareName, FILE_SHARE, 1,
sShareName)

if errRet = 0 then
oNet.MapNetworkDrive sDrive, sNetShare
else
wsh.echo "Failed"
end if

Tom Lavedas
===========
From: Pegasus (MVP) on
"dlc" <dlc(a)discussions.microsoft.com> wrote in message
news:F3D74709-39DE-45F1-8A46-851C1C47D47F(a)microsoft.com...
> is it possible to have a script to map a drive letter to a directory
> without
> using subst?

As usual there is also a very concise batch file answer to your question:

@echo off
net use Q: "\\%ComputerName%\E$"

or perhaps a little more robust:

@echo off
net share | find /i "DriveE" > nul || net share DriveE=E:\
net use Q: "\\%ComputerName%\DriveE"