Tom
Sat Jun 28 13:57:59 CDT 2008
On Jun 28, 11:36=A0am, "Richard Mueller [MVP]" <rlmueller-
nos...@ameritech.nospam.net> wrote:
> "dlc" <d...@discussions.microsoft.com> wrote in message
>
> news:F3D74709-39DE-45F1-8A46-851C1C47D47F@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 m=
ap
> 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 th=
e
> MapNetworkDrive method of the wshNetwork object to map a share to a drive=
in
> VBScript. For example:
> =3D=3D=3D=3D=3D=3D=3D=3D=3D
> Option Explicit
> Dim objNetwork, strShare, strDrive
>
> strDrive =3D "K:"
> strShare =3D "\\MyServer\MyShare"
>
> Set objNetwork =3D CreateObject("Wscript.Network")
> objNetwork.MapNetworkDrive strDrive, strShare
> =3D=3D=3D=3D=3D=3D=3D
> The mapping will fail if the drive is in use, perhaps because the drive w=
as
> 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 examp=
le:
>
> --
> 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 =3D "K:"
sSharePath =3D "C:\Someplace\Testing"
sShareName =3D "Testing"
set oNet =3D CreateObject("Wscript.Network")
sNetShare =3D "\\" & oNet.ComputerName & "\" & sShareName
wsh.echo sNetShare
Const FILE_SHARE =3D 0
Const MAXIMUM_CONNECTIONS =3D 1
Set objWMIService =3D GetObject("winmgmts:" _
& "{impersonationLevel=3Dimpersonate}!\\.\root\cimv2")
Set oNewShare =3D objWMIService.Get("Win32_Share")
errRet =3D oNewShare.Create(sSharePath, sShareName, FILE_SHARE, 1,
sShareName)
if errRet =3D 0 then
oNet.MapNetworkDrive sDrive, sNetShare
else
wsh.echo "Failed"
end if
Tom Lavedas
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D