Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > VB Script

Vista - Map Network Drive

Reply
 
Old 07-31-2009   #1 (permalink)
Microsoft Newsgroups


 
 

Map Network Drive

I am new to scripting with vb and want to create a script that will
automatically may some network drives for our end users (they will be logged
into their machine under their profile). Does anyone have any samples I
could use. It would be greatly apprectiated.

James



My System SpecsSystem Spec
Old 07-31-2009   #2 (permalink)
Pegasus [MVP]


 
 

Re: Map Network Drive


"Microsoft Newsgroups" <james.mcgril@xxxxxx> wrote in message
news:OktkLWiEKHA.5780@xxxxxx
Quote:

>I am new to scripting with vb and want to create a script that will
>automatically may some network drives for our end users (they will be
>logged into their machine under their profile). Does anyone have any
>samples I could use. It would be greatly apprectiated.
>
> James
Have a look at the thread "logon vbscript mapping drives", posted in this
very newsgroup 9 days ago.


My System SpecsSystem Spec
Old 08-01-2009   #3 (permalink)
Richard Mueller [MVP]


 
 

Re: Map Network Drive


"Microsoft Newsgroups" <james.mcgril@xxxxxx> wrote in message
news:OktkLWiEKHA.5780@xxxxxx
Quote:

>I am new to scripting with vb and want to create a script that will
>automatically may some network drives for our end users (they will be
>logged into their machine under their profile). Does anyone have any
>samples I could use. It would be greatly apprectiated.
>
> James
In VBScript use the wshNetwork object to map a drive to a share. For
example, to map a share to drive K:
=========
Set objNetwork = CreateObject("Wscript.Network")
objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare"
=====
To map a shared printer (and make it the default):
==========
Set objNetwork = CreateObject("Wscript.Network")
objNetwork.AddWindowsPrinterConnection "\\MyServer\MyPrinter"
objNetwork.SetDefaultPrinter "\\MyServer\MyPrinter"
=========
The simplest way to map drives according to group membership (assuming you
only have a few groups to check and are only concerned with direct group
membership) is to use the IsMember method of the group object. For example:
=========
Set objNetwork = CreateObject("Wscript.Network")

' Bind to current user object.
Set objSysInfo = CreateObject("ADSystemInfo")
strUserDN = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUserDN)

' Bind to group, using full Distinguished Name.
Set objGroup = GetObject("LDAP://cn=Test Group,ou=West,dc=MyDomain,dc=com")

' Check group membership.
If (objGroup.IsMember(objUser.AdsPath) = True) Then
' User is a member of the group, so map a drive.
objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare"
End If
======
For more examples logon scripts that handle group nesting, see this link:

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

Also, a discussion of the pitfalls involved in methods many people use to
check group membership:

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

Plus, an FAQ on configuring logon scripts:

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

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


My System SpecsSystem Spec
Old 08-03-2009   #4 (permalink)
Microsoft Newsgroups


 
 

Re: Map Network Drive

That is exactly what I needed. Thanks Richard!

James

"Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
message news:eD3k0prEKHA.1252@xxxxxx
Quote:

>
> "Microsoft Newsgroups" <james.mcgril@xxxxxx> wrote in message
> news:OktkLWiEKHA.5780@xxxxxx
Quote:

>>I am new to scripting with vb and want to create a script that will
>>automatically may some network drives for our end users (they will be
>>logged into their machine under their profile). Does anyone have any
>>samples I could use. It would be greatly apprectiated.
>>
>> James
>
> In VBScript use the wshNetwork object to map a drive to a share. For
> example, to map a share to drive K:
> =========
> Set objNetwork = CreateObject("Wscript.Network")
> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare"
> =====
> To map a shared printer (and make it the default):
> ==========
> Set objNetwork = CreateObject("Wscript.Network")
> objNetwork.AddWindowsPrinterConnection "\\MyServer\MyPrinter"
> objNetwork.SetDefaultPrinter "\\MyServer\MyPrinter"
> =========
> The simplest way to map drives according to group membership (assuming you
> only have a few groups to check and are only concerned with direct group
> membership) is to use the IsMember method of the group object. For
> example:
> =========
> Set objNetwork = CreateObject("Wscript.Network")
>
> ' Bind to current user object.
> Set objSysInfo = CreateObject("ADSystemInfo")
> strUserDN = objSysInfo.UserName
> Set objUser = GetObject("LDAP://" & strUserDN)
>
> ' Bind to group, using full Distinguished Name.
> Set objGroup = GetObject("LDAP://cn=Test
> Group,ou=West,dc=MyDomain,dc=com")
>
> ' Check group membership.
> If (objGroup.IsMember(objUser.AdsPath) = True) Then
> ' User is a member of the group, so map a drive.
> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare"
> End If
> ======
> For more examples logon scripts that handle group nesting, see this link:
>
> http://www.rlmueller.net/freecode2.htm
>
> Also, a discussion of the pitfalls involved in methods many people use to
> check group membership:
>
> http://www.rlmueller.net/MemberOf.htm
>
> Plus, an FAQ on configuring logon scripts:
>
> http://www.rlmueller.net/LogonScriptFAQ.htm
>
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab - http://www.rlmueller.net
> --
>
>

My System SpecsSystem Spec
Old 08-03-2009   #5 (permalink)
Microsoft Newsgroups


 
 

Re: Map Network Drive

Richard,

Mapping the printers is working great and so is mapping a generic network
drive. But I have a couple more questions...

1. I tried the following code to map a drive directly to a users directory.
The user directory name matches the user name, so I thought I could modify
the code you sent. But I get an error that the path could not be found.
How can I fix that?

Set objNetwork = CreateObject("Wscript.Network")
Set objSysInfo = CreateObject("ADSystemInfo")
strUserDN = objSysInfo.UserName
Set objUser = GetObject("LDAP://" & strUserDN)
objNetwork.MapNetworkDrive "G:", "\\company name\users\" & strUserDN

2. Will the mapped drives reconnect after the pc is restarted and the user
logs in again?

Any more help you could provide would be great!

Regards,

James



"Microsoft Newsgroups" <donotreply@xxxxxx> wrote in message
news:%23snFdrDFKHA.3964@xxxxxx
Quote:

> That is exactly what I needed. Thanks Richard!
>
> James
>
> "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
> message news:eD3k0prEKHA.1252@xxxxxx
Quote:

>>
>> "Microsoft Newsgroups" <james.mcgril@xxxxxx> wrote in message
>> news:OktkLWiEKHA.5780@xxxxxx
Quote:

>>>I am new to scripting with vb and want to create a script that will
>>>automatically may some network drives for our end users (they will be
>>>logged into their machine under their profile). Does anyone have any
>>>samples I could use. It would be greatly apprectiated.
>>>
>>> James
>>
>> In VBScript use the wshNetwork object to map a drive to a share. For
>> example, to map a share to drive K:
>> =========
>> Set objNetwork = CreateObject("Wscript.Network")
>> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare"
>> =====
>> To map a shared printer (and make it the default):
>> ==========
>> Set objNetwork = CreateObject("Wscript.Network")
>> objNetwork.AddWindowsPrinterConnection "\\MyServer\MyPrinter"
>> objNetwork.SetDefaultPrinter "\\MyServer\MyPrinter"
>> =========
>> The simplest way to map drives according to group membership (assuming
>> you only have a few groups to check and are only concerned with direct
>> group membership) is to use the IsMember method of the group object. For
>> example:
>> =========
>> Set objNetwork = CreateObject("Wscript.Network")
>>
>> ' Bind to current user object.
>> Set objSysInfo = CreateObject("ADSystemInfo")
>> strUserDN = objSysInfo.UserName
>> Set objUser = GetObject("LDAP://" & strUserDN)
>>
>> ' Bind to group, using full Distinguished Name.
>> Set objGroup = GetObject("LDAP://cn=Test
>> Group,ou=West,dc=MyDomain,dc=com")
>>
>> ' Check group membership.
>> If (objGroup.IsMember(objUser.AdsPath) = True) Then
>> ' User is a member of the group, so map a drive.
>> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare"
>> End If
>> ======
>> For more examples logon scripts that handle group nesting, see this link:
>>
>> http://www.rlmueller.net/freecode2.htm
>>
>> Also, a discussion of the pitfalls involved in methods many people use to
>> check group membership:
>>
>> http://www.rlmueller.net/MemberOf.htm
>>
>> Plus, an FAQ on configuring logon scripts:
>>
>> http://www.rlmueller.net/LogonScriptFAQ.htm
>>
>> --
>> Richard Mueller
>> MVP Directory Services
>> Hilltop Lab - http://www.rlmueller.net
>> --
>>
>>
>
>

My System SpecsSystem Spec
Old 08-03-2009   #6 (permalink)
Richard Mueller [MVP]


 
 

Re: Map Network Drive

The value of strUserDN (objSysInfo.UserName) is the Distinguished Name of
the user, not the "pre-Windows 2000 logon name" you need. You can retrieve
the correct user name from the wshNetwork object. For example:
==========
Set objNetwork = CreateObject("Wscript.Network")
strName = objNetwork.UserName
objNetwork.MapNetworkDrive "G:", "\\company name\users\" & strName
==========
A given user's Distinguished Name might be similar to:

cn=Jim K. Smith,ou=Sales,ou=West,dc=MyDomain,dc=com

while the same user's "pre-Windows 2000 logon" name might be:

jsmith

The Distinguished Name uniquely identifies the object and indicates where in
Active Directory the object resides. The value "Jim K. Smith" is the "Common
Name" of the user, and does not uniquely identify the user. The Common Name
must be unique in the Container/OU (ou=Sales in my example), but there can
be several users with the same Common Name. The "pre-Windows 2000 logon
name" must be unique in the domain. This is also the value of the
environment variable "username".
--
Richard Mueller
MVP Directory Services
Hilltop Lab - http://www.rlmueller.net
--

"Microsoft Newsgroups" <donotreply@xxxxxx> wrote in message
news:%230%23O3LEFKHA.1380@xxxxxx
Quote:

> Richard,
>
> Mapping the printers is working great and so is mapping a generic network
> drive. But I have a couple more questions...
>
> 1. I tried the following code to map a drive directly to a users
> directory. The user directory name matches the user name, so I thought I
> could modify the code you sent. But I get an error that the path could
> not be found. How can I fix that?
>
> Set objNetwork = CreateObject("Wscript.Network")
> Set objSysInfo = CreateObject("ADSystemInfo")
> strUserDN = objSysInfo.UserName
> Set objUser = GetObject("LDAP://" & strUserDN)
> objNetwork.MapNetworkDrive "G:", "\\company name\users\" & strUserDN
>
> 2. Will the mapped drives reconnect after the pc is restarted and the
> user logs in again?
>
> Any more help you could provide would be great!
>
> Regards,
>
> James
>
>
>
> "Microsoft Newsgroups" <donotreply@xxxxxx> wrote in message
> news:%23snFdrDFKHA.3964@xxxxxx
Quote:

>> That is exactly what I needed. Thanks Richard!
>>
>> James
>>
>> "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
>> message news:eD3k0prEKHA.1252@xxxxxx
Quote:

>>>
>>> "Microsoft Newsgroups" <james.mcgril@xxxxxx> wrote in message
>>> news:OktkLWiEKHA.5780@xxxxxx
>>>>I am new to scripting with vb and want to create a script that will
>>>>automatically may some network drives for our end users (they will be
>>>>logged into their machine under their profile). Does anyone have any
>>>>samples I could use. It would be greatly apprectiated.
>>>>
>>>> James
>>>
>>> In VBScript use the wshNetwork object to map a drive to a share. For
>>> example, to map a share to drive K:
>>> =========
>>> Set objNetwork = CreateObject("Wscript.Network")
>>> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare"
>>> =====
>>> To map a shared printer (and make it the default):
>>> ==========
>>> Set objNetwork = CreateObject("Wscript.Network")
>>> objNetwork.AddWindowsPrinterConnection "\\MyServer\MyPrinter"
>>> objNetwork.SetDefaultPrinter "\\MyServer\MyPrinter"
>>> =========
>>> The simplest way to map drives according to group membership (assuming
>>> you only have a few groups to check and are only concerned with direct
>>> group membership) is to use the IsMember method of the group object. For
>>> example:
>>> =========
>>> Set objNetwork = CreateObject("Wscript.Network")
>>>
>>> ' Bind to current user object.
>>> Set objSysInfo = CreateObject("ADSystemInfo")
>>> strUserDN = objSysInfo.UserName
>>> Set objUser = GetObject("LDAP://" & strUserDN)
>>>
>>> ' Bind to group, using full Distinguished Name.
>>> Set objGroup = GetObject("LDAP://cn=Test
>>> Group,ou=West,dc=MyDomain,dc=com")
>>>
>>> ' Check group membership.
>>> If (objGroup.IsMember(objUser.AdsPath) = True) Then
>>> ' User is a member of the group, so map a drive.
>>> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare"
>>> End If
>>> ======
>>> For more examples logon scripts that handle group nesting, see this
>>> link:
>>>
>>> http://www.rlmueller.net/freecode2.htm
>>>
>>> Also, a discussion of the pitfalls involved in methods many people use
>>> to check group membership:
>>>
>>> http://www.rlmueller.net/MemberOf.htm
>>>
>>> Plus, an FAQ on configuring logon scripts:
>>>
>>> http://www.rlmueller.net/LogonScriptFAQ.htm
>>>
>>> --
>>> Richard Mueller
>>> MVP Directory Services
>>> Hilltop Lab - http://www.rlmueller.net
>>> --
>>>
>>>
>>
>>
>
>

My System SpecsSystem Spec
Old 08-03-2009   #7 (permalink)
James


 
 

Re: Map Network Drive

Again, you have been a great help! I truly appreicate it. You have just
helped make my job a lot easier. All I have to do to my script now is to
add some code to check to see if the printers and drives are already mapped
otherwise it will bomb out.

James

"Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
message news:%23buPynEFKHA.4316@xxxxxx
Quote:

> The value of strUserDN (objSysInfo.UserName) is the Distinguished Name of
> the user, not the "pre-Windows 2000 logon name" you need. You can retrieve
> the correct user name from the wshNetwork object. For example:
> ==========
> Set objNetwork = CreateObject("Wscript.Network")
> strName = objNetwork.UserName
> objNetwork.MapNetworkDrive "G:", "\\company name\users\" & strName
> ==========
> A given user's Distinguished Name might be similar to:
>
> cn=Jim K. Smith,ou=Sales,ou=West,dc=MyDomain,dc=com
>
> while the same user's "pre-Windows 2000 logon" name might be:
>
> jsmith
>
> The Distinguished Name uniquely identifies the object and indicates where
> in Active Directory the object resides. The value "Jim K. Smith" is the
> "Common Name" of the user, and does not uniquely identify the user. The
> Common Name must be unique in the Container/OU (ou=Sales in my example),
> but there can be several users with the same Common Name. The "pre-Windows
> 2000 logon name" must be unique in the domain. This is also the value of
> the environment variable "username".
> --
> Richard Mueller
> MVP Directory Services
> Hilltop Lab - http://www.rlmueller.net
> --
>
> "Microsoft Newsgroups" <donotreply@xxxxxx> wrote in message
> news:%230%23O3LEFKHA.1380@xxxxxx
Quote:

>> Richard,
>>
>> Mapping the printers is working great and so is mapping a generic network
>> drive. But I have a couple more questions...
>>
>> 1. I tried the following code to map a drive directly to a users
>> directory. The user directory name matches the user name, so I thought I
>> could modify the code you sent. But I get an error that the path could
>> not be found. How can I fix that?
>>
>> Set objNetwork = CreateObject("Wscript.Network")
>> Set objSysInfo = CreateObject("ADSystemInfo")
>> strUserDN = objSysInfo.UserName
>> Set objUser = GetObject("LDAP://" & strUserDN)
>> objNetwork.MapNetworkDrive "G:", "\\company name\users\" & strUserDN
>>
>> 2. Will the mapped drives reconnect after the pc is restarted and the
>> user logs in again?
>>
>> Any more help you could provide would be great!
>>
>> Regards,
>>
>> James
>>
>>
>>
>> "Microsoft Newsgroups" <donotreply@xxxxxx> wrote in message
>> news:%23snFdrDFKHA.3964@xxxxxx
Quote:

>>> That is exactly what I needed. Thanks Richard!
>>>
>>> James
>>>
>>> "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
>>> message news:eD3k0prEKHA.1252@xxxxxx
>>>>
>>>> "Microsoft Newsgroups" <james.mcgril@xxxxxx> wrote in message
>>>> news:OktkLWiEKHA.5780@xxxxxx
>>>>>I am new to scripting with vb and want to create a script that will
>>>>>automatically may some network drives for our end users (they will be
>>>>>logged into their machine under their profile). Does anyone have any
>>>>>samples I could use. It would be greatly apprectiated.
>>>>>
>>>>> James
>>>>
>>>> In VBScript use the wshNetwork object to map a drive to a share. For
>>>> example, to map a share to drive K:
>>>> =========
>>>> Set objNetwork = CreateObject("Wscript.Network")
>>>> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare"
>>>> =====
>>>> To map a shared printer (and make it the default):
>>>> ==========
>>>> Set objNetwork = CreateObject("Wscript.Network")
>>>> objNetwork.AddWindowsPrinterConnection "\\MyServer\MyPrinter"
>>>> objNetwork.SetDefaultPrinter "\\MyServer\MyPrinter"
>>>> =========
>>>> The simplest way to map drives according to group membership (assuming
>>>> you only have a few groups to check and are only concerned with direct
>>>> group membership) is to use the IsMember method of the group object.
>>>> For example:
>>>> =========
>>>> Set objNetwork = CreateObject("Wscript.Network")
>>>>
>>>> ' Bind to current user object.
>>>> Set objSysInfo = CreateObject("ADSystemInfo")
>>>> strUserDN = objSysInfo.UserName
>>>> Set objUser = GetObject("LDAP://" & strUserDN)
>>>>
>>>> ' Bind to group, using full Distinguished Name.
>>>> Set objGroup = GetObject("LDAP://cn=Test
>>>> Group,ou=West,dc=MyDomain,dc=com")
>>>>
>>>> ' Check group membership.
>>>> If (objGroup.IsMember(objUser.AdsPath) = True) Then
>>>> ' User is a member of the group, so map a drive.
>>>> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare"
>>>> End If
>>>> ======
>>>> For more examples logon scripts that handle group nesting, see this
>>>> link:
>>>>
>>>> http://www.rlmueller.net/freecode2.htm
>>>>
>>>> Also, a discussion of the pitfalls involved in methods many people use
>>>> to check group membership:
>>>>
>>>> http://www.rlmueller.net/MemberOf.htm
>>>>
>>>> Plus, an FAQ on configuring logon scripts:
>>>>
>>>> http://www.rlmueller.net/LogonScriptFAQ.htm
>>>>
>>>> --
>>>> Richard Mueller
>>>> MVP Directory Services
>>>> Hilltop Lab - http://www.rlmueller.net
>>>> --
>>>>
>>>>
>>>
>>>
>>
>>
>
>

My System SpecsSystem Spec
Old 08-03-2009   #8 (permalink)
James


 
 

Re: Map Network Drive

What is the switch or argument to make the mapped drive persistant?

James

"James" <donotreply@xxxxxx> wrote in message
news:O6WCaBFFKHA.4316@xxxxxx
Quote:

> Again, you have been a great help! I truly appreicate it. You have just
> helped make my job a lot easier. All I have to do to my script now is to
> add some code to check to see if the printers and drives are already
> mapped otherwise it will bomb out.
>
> James
>
> "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
> message news:%23buPynEFKHA.4316@xxxxxx
Quote:

>> The value of strUserDN (objSysInfo.UserName) is the Distinguished Name of
>> the user, not the "pre-Windows 2000 logon name" you need. You can
>> retrieve the correct user name from the wshNetwork object. For example:
>> ==========
>> Set objNetwork = CreateObject("Wscript.Network")
>> strName = objNetwork.UserName
>> objNetwork.MapNetworkDrive "G:", "\\company name\users\" & strName
>> ==========
>> A given user's Distinguished Name might be similar to:
>>
>> cn=Jim K. Smith,ou=Sales,ou=West,dc=MyDomain,dc=com
>>
>> while the same user's "pre-Windows 2000 logon" name might be:
>>
>> jsmith
>>
>> The Distinguished Name uniquely identifies the object and indicates where
>> in Active Directory the object resides. The value "Jim K. Smith" is the
>> "Common Name" of the user, and does not uniquely identify the user. The
>> Common Name must be unique in the Container/OU (ou=Sales in my example),
>> but there can be several users with the same Common Name. The
>> "pre-Windows 2000 logon name" must be unique in the domain. This is also
>> the value of the environment variable "username".
>> --
>> Richard Mueller
>> MVP Directory Services
>> Hilltop Lab - http://www.rlmueller.net
>> --
>>
>> "Microsoft Newsgroups" <donotreply@xxxxxx> wrote in message
>> news:%230%23O3LEFKHA.1380@xxxxxx
Quote:

>>> Richard,
>>>
>>> Mapping the printers is working great and so is mapping a generic
>>> network drive. But I have a couple more questions...
>>>
>>> 1. I tried the following code to map a drive directly to a users
>>> directory. The user directory name matches the user name, so I thought I
>>> could modify the code you sent. But I get an error that the path could
>>> not be found. How can I fix that?
>>>
>>> Set objNetwork = CreateObject("Wscript.Network")
>>> Set objSysInfo = CreateObject("ADSystemInfo")
>>> strUserDN = objSysInfo.UserName
>>> Set objUser = GetObject("LDAP://" & strUserDN)
>>> objNetwork.MapNetworkDrive "G:", "\\company name\users\" & strUserDN
>>>
>>> 2. Will the mapped drives reconnect after the pc is restarted and the
>>> user logs in again?
>>>
>>> Any more help you could provide would be great!
>>>
>>> Regards,
>>>
>>> James
>>>
>>>
>>>
>>> "Microsoft Newsgroups" <donotreply@xxxxxx> wrote in message
>>> news:%23snFdrDFKHA.3964@xxxxxx
>>>> That is exactly what I needed. Thanks Richard!
>>>>
>>>> James
>>>>
>>>> "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote
>>>> in message news:eD3k0prEKHA.1252@xxxxxx
>>>>>
>>>>> "Microsoft Newsgroups" <james.mcgril@xxxxxx> wrote in message
>>>>> news:OktkLWiEKHA.5780@xxxxxx
>>>>>>I am new to scripting with vb and want to create a script that will
>>>>>>automatically may some network drives for our end users (they will be
>>>>>>logged into their machine under their profile). Does anyone have any
>>>>>>samples I could use. It would be greatly apprectiated.
>>>>>>
>>>>>> James
>>>>>
>>>>> In VBScript use the wshNetwork object to map a drive to a share. For
>>>>> example, to map a share to drive K:
>>>>> =========
>>>>> Set objNetwork = CreateObject("Wscript.Network")
>>>>> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare"
>>>>> =====
>>>>> To map a shared printer (and make it the default):
>>>>> ==========
>>>>> Set objNetwork = CreateObject("Wscript.Network")
>>>>> objNetwork.AddWindowsPrinterConnection "\\MyServer\MyPrinter"
>>>>> objNetwork.SetDefaultPrinter "\\MyServer\MyPrinter"
>>>>> =========
>>>>> The simplest way to map drives according to group membership (assuming
>>>>> you only have a few groups to check and are only concerned with direct
>>>>> group membership) is to use the IsMember method of the group object.
>>>>> For example:
>>>>> =========
>>>>> Set objNetwork = CreateObject("Wscript.Network")
>>>>>
>>>>> ' Bind to current user object.
>>>>> Set objSysInfo = CreateObject("ADSystemInfo")
>>>>> strUserDN = objSysInfo.UserName
>>>>> Set objUser = GetObject("LDAP://" & strUserDN)
>>>>>
>>>>> ' Bind to group, using full Distinguished Name.
>>>>> Set objGroup = GetObject("LDAP://cn=Test
>>>>> Group,ou=West,dc=MyDomain,dc=com")
>>>>>
>>>>> ' Check group membership.
>>>>> If (objGroup.IsMember(objUser.AdsPath) = True) Then
>>>>> ' User is a member of the group, so map a drive.
>>>>> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare"
>>>>> End If
>>>>> ======
>>>>> For more examples logon scripts that handle group nesting, see this
>>>>> link:
>>>>>
>>>>> http://www.rlmueller.net/freecode2.htm
>>>>>
>>>>> Also, a discussion of the pitfalls involved in methods many people use
>>>>> to check group membership:
>>>>>
>>>>> http://www.rlmueller.net/MemberOf.htm
>>>>>
>>>>> Plus, an FAQ on configuring logon scripts:
>>>>>
>>>>> http://www.rlmueller.net/LogonScriptFAQ.htm
>>>>>
>>>>> --
>>>>> Richard Mueller
>>>>> MVP Directory Services
>>>>> Hilltop Lab - http://www.rlmueller.net
>>>>> --
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>

My System SpecsSystem Spec
Old 08-03-2009   #9 (permalink)
Richard Mueller [MVP]


 
 

Re: Map Network Drive

objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare", True

The third parameter given to the MapNetworkDrive method, which is optional,
indicates if the mapping is persistent. The default is False. Because the
user can map drives to the standard letters used in the logon script and
make them persistent, I generally account for this. The command will raise
an error if there is a persistent drive mapping. I use code similar to below
to account for this:
=========
' Trap possible error because K: has a persistent mapping.
On Error Resume Next
objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare"
If (Err.Number <> 0) Then
' Mapping raised error, remove existing mapping.
' Restore normal error handing so we are alerted to errors.
On Error GoTo 0
objNetwork.RemoveNetworkDrive "K:", True, True
' Try again. If there is an error this time, we are alerted.
objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare"
End If
' Restore normal error handling.
On Error GoTo 0
==========
The RemoveNetworkDrive method has one required parameter, the drive letter,
and two optional. The first optional parameter indicates if the mapping is
to be removed even if it is use. The second optional parameter indicates if
removal is persistent, so the mapping is no longer persistent. I hope this
helps. You can check WSH documentation for details of the MapNetworkDrive
and RemoveNetworkDrive methods. See this link to download the documentation:

http://www.microsoft.com/downloads/d...displaylang=en

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

"James" <donotreply@xxxxxx> wrote in message
news:%23LxdOdFFKHA.2376@xxxxxx
Quote:

> What is the switch or argument to make the mapped drive persistant?
>
> James
>
> "James" <donotreply@xxxxxx> wrote in message
> news:O6WCaBFFKHA.4316@xxxxxx
Quote:

>> Again, you have been a great help! I truly appreicate it. You have just
>> helped make my job a lot easier. All I have to do to my script now is to
>> add some code to check to see if the printers and drives are already
>> mapped otherwise it will bomb out.
>>
>> James
>>
>> "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote in
>> message news:%23buPynEFKHA.4316@xxxxxx
Quote:

>>> The value of strUserDN (objSysInfo.UserName) is the Distinguished Name
>>> of the user, not the "pre-Windows 2000 logon name" you need. You can
>>> retrieve the correct user name from the wshNetwork object. For example:
>>> ==========
>>> Set objNetwork = CreateObject("Wscript.Network")
>>> strName = objNetwork.UserName
>>> objNetwork.MapNetworkDrive "G:", "\\company name\users\" & strName
>>> ==========
>>> A given user's Distinguished Name might be similar to:
>>>
>>> cn=Jim K. Smith,ou=Sales,ou=West,dc=MyDomain,dc=com
>>>
>>> while the same user's "pre-Windows 2000 logon" name might be:
>>>
>>> jsmith
>>>
>>> The Distinguished Name uniquely identifies the object and indicates
>>> where in Active Directory the object resides. The value "Jim K. Smith"
>>> is the "Common Name" of the user, and does not uniquely identify the
>>> user. The Common Name must be unique in the Container/OU (ou=Sales in my
>>> example), but there can be several users with the same Common Name. The
>>> "pre-Windows 2000 logon name" must be unique in the domain. This is also
>>> the value of the environment variable "username".
>>> --
>>> Richard Mueller
>>> MVP Directory Services
>>> Hilltop Lab - http://www.rlmueller.net
>>> --
>>>
>>> "Microsoft Newsgroups" <donotreply@xxxxxx> wrote in message
>>> news:%230%23O3LEFKHA.1380@xxxxxx
>>>> Richard,
>>>>
>>>> Mapping the printers is working great and so is mapping a generic
>>>> network drive. But I have a couple more questions...
>>>>
>>>> 1. I tried the following code to map a drive directly to a users
>>>> directory. The user directory name matches the user name, so I thought
>>>> I could modify the code you sent. But I get an error that the path
>>>> could not be found. How can I fix that?
>>>>
>>>> Set objNetwork = CreateObject("Wscript.Network")
>>>> Set objSysInfo = CreateObject("ADSystemInfo")
>>>> strUserDN = objSysInfo.UserName
>>>> Set objUser = GetObject("LDAP://" & strUserDN)
>>>> objNetwork.MapNetworkDrive "G:", "\\company name\users\" & strUserDN
>>>>
>>>> 2. Will the mapped drives reconnect after the pc is restarted and the
>>>> user logs in again?
>>>>
>>>> Any more help you could provide would be great!
>>>>
>>>> Regards,
>>>>
>>>> James
>>>>
>>>>
>>>>
>>>> "Microsoft Newsgroups" <donotreply@xxxxxx> wrote in message
>>>> news:%23snFdrDFKHA.3964@xxxxxx
>>>>> That is exactly what I needed. Thanks Richard!
>>>>>
>>>>> James
>>>>>
>>>>> "Richard Mueller [MVP]" <rlmueller-nospam@xxxxxx> wrote
>>>>> in message news:eD3k0prEKHA.1252@xxxxxx
>>>>>>
>>>>>> "Microsoft Newsgroups" <james.mcgril@xxxxxx> wrote in message
>>>>>> news:OktkLWiEKHA.5780@xxxxxx
>>>>>>>I am new to scripting with vb and want to create a script that will
>>>>>>>automatically may some network drives for our end users (they will be
>>>>>>>logged into their machine under their profile). Does anyone have any
>>>>>>>samples I could use. It would be greatly apprectiated.
>>>>>>>
>>>>>>> James
>>>>>>
>>>>>> In VBScript use the wshNetwork object to map a drive to a share. For
>>>>>> example, to map a share to drive K:
>>>>>> =========
>>>>>> Set objNetwork = CreateObject("Wscript.Network")
>>>>>> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare"
>>>>>> =====
>>>>>> To map a shared printer (and make it the default):
>>>>>> ==========
>>>>>> Set objNetwork = CreateObject("Wscript.Network")
>>>>>> objNetwork.AddWindowsPrinterConnection "\\MyServer\MyPrinter"
>>>>>> objNetwork.SetDefaultPrinter "\\MyServer\MyPrinter"
>>>>>> =========
>>>>>> The simplest way to map drives according to group membership
>>>>>> (assuming you only have a few groups to check and are only concerned
>>>>>> with direct group membership) is to use the IsMember method of the
>>>>>> group object. For example:
>>>>>> =========
>>>>>> Set objNetwork = CreateObject("Wscript.Network")
>>>>>>
>>>>>> ' Bind to current user object.
>>>>>> Set objSysInfo = CreateObject("ADSystemInfo")
>>>>>> strUserDN = objSysInfo.UserName
>>>>>> Set objUser = GetObject("LDAP://" & strUserDN)
>>>>>>
>>>>>> ' Bind to group, using full Distinguished Name.
>>>>>> Set objGroup = GetObject("LDAP://cn=Test
>>>>>> Group,ou=West,dc=MyDomain,dc=com")
>>>>>>
>>>>>> ' Check group membership.
>>>>>> If (objGroup.IsMember(objUser.AdsPath) = True) Then
>>>>>> ' User is a member of the group, so map a drive.
>>>>>> objNetwork.MapNetworkDrive "K:", "\\MyServer\MyShare"
>>>>>> End If
>>>>>> ======
>>>>>> For more examples logon scripts that handle group nesting, see this
>>>>>> link:
>>>>>>
>>>>>> http://www.rlmueller.net/freecode2.htm
>>>>>>
>>>>>> Also, a discussion of the pitfalls involved in methods many people
>>>>>> use to check group membership:
>>>>>>
>>>>>> http://www.rlmueller.net/MemberOf.htm
>>>>>>
>>>>>> Plus, an FAQ on configuring logon scripts:
>>>>>>
>>>>>> http://www.rlmueller.net/LogonScriptFAQ.htm
>>>>>>
>>>>>> --
>>>>>> Richard Mueller
>>>>>> MVP Directory Services
>>>>>> Hilltop Lab - http://www.rlmueller.net
>>>>>> --
>>>>>>
>>>>>>
>>>>>
>>>>>
>>>>
>>>>
>>>
>>>
>>
>>
>
>

My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Invisible network drive (Linkstation) on home network Vista networking & sharing
trouble moving files from local drive to network drive Vista networking & sharing
Map a Network Drive on Vista Premium on Network Attached Storage ( Vista networking & sharing
Unable to map network drive because no network was found Vista networking & sharing
Network Mapped Drive : Lost Network Connection Vista networking & sharing


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46