× NETGEAR will be terminating ReadyCLOUD service by July 1st, 2023. For more details click here.
Orbi WiFi 7 RBE973
Reply

Script to Connect to Network Drive

THN
Aspirant
Aspirant

Script to Connect to Network Drive

Good Day all,

I've gleaned so much information from these posts, that I thought it's time for me to contribute something, as well. So, here we go:

I have created a little vbscript, which can be run from the start up folder in the start menu, or simply be executed with a shortcut on your desktop. As far as I know, this will only work on the Windows platform.



strLocalDrive = "N:" '<<< Change this to fit your needs.
strRemoteShare = "\\nas-server\Data" '<<< Change this to fit your needs. nas-server could also be an IP address.

Set objNetwork = WScript.CreateObject("WScript.Network")

on error resume next
objNetwork.RemoveNetworkDrive strLocalDrive
on error goto 0

EFlg="set"
While EFlg="set"
strPer = "FALSE"
strUsr = InputBox("Please enter your NAS User Name", "User Name")
strPas = InputBox("Please enter your NAS Password", "Password")
On Error Resume Next
If strUsr="" or strPas="" Then
objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, strPer
Else
objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, strPer, strUsr, strPas
End If
If Err.Number<>0 Then
EFlg="set"
' MsgBox Err.Description
Else
MsgBox "Connected " & strRemoteShare & " successfully to " & strLocalDrive '& ":"
EFlg=""
End If
Wend



Enjoy.
Message 1 of 19
bbaraniec
Luminary

Re: Script to Connect to Network Drive

Just a question, why would you need a script to map network drive in windows?
Message 2 of 19
dbott67
Guide

Re: Script to Connect to Network Drive

I think it's because Windows does not always connect mapped network drives on bootup (mine frequently say "Disconnected" after a reboot). The script will force the mapped drive to be in a connected state (I think).

-Dave
Message 3 of 19
THN
Aspirant
Aspirant

Re: Script to Connect to Network Drive

Dave is correct. This is supposed to be used as a logon script. If user A logs in, he maps folder 1 to drive N, while user B would map folder 2 to Dive N.
Message 4 of 19
Zaphod1
Aspirant

Re: Script to Connect to Network Drive

I just use a batch file to do this.
Message 5 of 19
jamesd11
Aspirant

Re: Script to Connect to Network Drive

I encountered the same problem that many users have reported about Windows not reconnecting a network drive at login. I have Windows XP Home edition, and mapped two network drives to two shares on my ReadyNAS NV+. The shares have a username that's different from the usernames on the XP box. Even though I checked the "Reconnect at logon" box, Windows would not reconnect the drives when I logged in later. According to this posting on an outside forum:

http://techrepublic.com.com/5208-6230-0.html?forumID=102&threadID=208564&messageID=2152515

XP Home edition does not cache user credentials for network resources. I confirmed this by checking the help for the "net use" command:


C:\>net help use
The syntax of this command is:

NET USE
[devicename | *] [\\computername\sharename[\volume] [password | *]]
[/USER:[domainname\]username]
[/USER:[dotted domain name\]username]
[/USER:[username@dotted domain name]
[/SMARTCARD]
[/SAVECRED]
[[/DELETE] | [/PERSISTENT:{YES | NO}]]

NET USE {devicename | *} [password | *] /HOME

NET USE [/PERSISTENT:{YES | NO}]

NET USE connects a computer to a shared resource or disconnects a
computer from a shared resource. When used without options, it lists
the computer's connections.
...
/SAVECRED Specifies that the username and password are to be saved.
This switch is ignored unless the command prompts for username
and password. This option is not available on Windows XP
Home Edition and will be ignored.


Apparently, this applies to other Windows editions as well. For example, the FAQ for Windows XP Media Center Edition 2005 states:

Can I connect a new PC running Windows XP Media Center Edition 2005 to a work network or domain?

While you can access network resources on a work network or a domain, you cannot join a Windows XP Media Center Edition 2005 PC to the domain. PCs running Windows XP Media Center Edition 2005 are designed specifically for home use. Windows XP Professional features, specifically Domain Join and Cached Credentials (Credentials Manager for logons) are not included. As a result, you will be prompted for your logon user name and password to access network resources after you reboot or log back on to the PC. ...


I didn't try the VBS script that THN provided above (but thanks for posting it). Instead, as a workaround, I followed Zaphod's example and created a command script:


C:\Documents and Settings\All Users\Start Menu\Programs\Startup>type NAS-shares.cmd
@echo off
set NASname=...
set NASuser=...
set NASpass=...
echo Setting persistence option for network drives ...
net use /persistent:no
echo Connecting network drives Z: ...
net use z: \\%NASname%\backup /user:%NASuser% "%NASpass%"
echo Connecting network drive U: ...
net use u: \\%NASname%\users /user:%NASuser% "%NASpass%"


I placed the script in All User's Startup folder so that it runs for everyone who uses the computer.
Message 6 of 19
sponder
Aspirant

Re: Script to Connect to Network Drive

can't help myself but stating the obvious here:
for the ones with a windows Domain:
You can set logon scripts in the active directory, so you won't have to manage the scripts on the clients. (handy for when you have a lot of clients..)
Message 7 of 19
lordoflard
Aspirant

Re: Script to Connect to Network Drive

Just to add my two penn'orth...

If you're using a batch file like jamesd and you have a number of shares to map, you can issue the command:
net use \\%NASname% /user:%NASuser% "%NASpass%" /persistent:no
then each share is connected using something of the form:
net use Z: \\%NASname%\ShareName
which will save, ooh, a second or so of typing or copy & pasting. I've found this method of connecting as the user before attempting to map drives to be slightly more reliable in an office of mixed Windows versions.

Of course, if you don't want to include the user's password in a plaintext script, omit the "%NASpass%" and a password prompt will appear in the command window.

The Lord of Lard
Message 8 of 19
daztrue
Aspirant

Re: Script to Connect to Network Drive

I've had this thread referred to me, which may be of help (and I do want to map shares etc.), but I just wondered if any of you have come across (or can solve :lol: ) the following symptoms:

Typically(!), the icon for my NAS (as a device), along with icons for add-ons all appear, along with the blue icons for my wireless laptop and (if on) my main PC. However, the blue icon for my NAS hasn't displayed for ages. It's usually fine on my wired PC (although there's been the odd moment), and I've tried so many options. I've even had long phone consultations with Netgear, whose engineers have tried to resolve the issue.

I can easily make the blue icon appear (and thus access the NAS) by entering the IP, but it won't automatically appear with its hostname. :evil:

Batch file:
@lordoflard (or whoever wants to add another two penn'orth!) Does your post mean the batch file would look like this:
net use \\%NASname% /user:%NASuser% "%NASpass%" /persistent:no
net use Z: \\%NASname%\ShareName

...with the second line used for each share/folder mapped? And that would be it?

And not to appear thick (just safe!), :roll: do I take it that batch files would be stored in the following as relevant?
C:\Users\<user>\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup
C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup


VBScript:
@THN @dbott67 I did a 'bit' of VB once and so am able to fumble-read the script, but want to confirm: would separate .vbs files still be necessary for each Windows account as well as each mapped share? If so, how does this differ from the batch file option?
Message 9 of 19
dbott67
Guide

Re: Script to Connect to Network Drive

Can you post a screen shot of which icons you're referring to and where you expect to see them (desktop, network neighbourhood, etc.). For example, this is what appears in my "Network" in Windows 7 Pro:



I've also got shortcuts on the Desktop for my Pro (Prometheus), NVX (Envious) and Duo (Devious):

Message 10 of 19
daztrue
Aspirant

Re: Script to Connect to Network Drive

Not sure how I can printscreen an icon that isn't there! 😮

But to answer: just like the icons at the top of your screenshot, I expect to see one of my NAS there (Network) in Vista Ultimate x64.
Like yourself I have 2 media icons: Skifta and Orb.
1 icon for my NAS as a device (bottom of screenshot).

As for the Desktop: currently nothing setup but I previously attempted regular shortcuts to shares, only to find they were deleted each time I logged onto that Windows account, or whenever doing maintenance on PC.

From what I've read (as well as your screenshot), I realise it'll have to be one or more of the following: mapped drive, batch or VBScript file.
Message 11 of 19
dbott67
Guide

Re: Script to Connect to Network Drive

Many 'hostname' resolution issues can be resolved by setting the NAS to be the WINS server. I've got my Pro set as the primary WINS server for all devices on my home network and never have issues connecting to any of my NASes. Have a look at this how-to:

http://home.bott.ca/webserver/?p=217

I don't have any scripts or batch files on my Windows 7 computer... it's just using the above mechanism for WINS name resolution and all shortcuts in "network" automatically appear. The desktop shortcuts were just created by right-clicking the device in "network" and selecting "Create Shortcut".
Message 12 of 19
daztrue
Aspirant

Re: Script to Connect to Network Drive

Wow, can't wait to try this, but from reading your link, combined with my checking my settings and thinking about my setup, my head began to buzz with questions! :idea: :idea:

1) Just as I have DHCP set up on my router (ProSafe SRXN3205), rather than NAS, I wondered if the same was possible for WINS. The Primary DNS Server is the router's LAN IP, so is the same possible for WINS? The SRNXN3205 manual states:

By default, the VPN firewall [SRXN3205] will function as a DHCP (Dynamic Host Configuration Protocol) server, allowing it to assign IP, DNS server, WINS Server, and default gateway addresses to all computers connected to the LAN.

Also...
The VPN firewall will deliver the following settings to any LAN device that requests DHCP:...
• WINS server (if you entered a WINS server address in the DHCP section of the LAN Setup screen).


2) In Network Connections > Properties my DNS tab (like the WINS tab) shows no addresses, yet Network Connection Details shows the router LAN IP for IPv4 DNS Server. Does this sound right, and if so, could putting the LAN IP in the DNS tab have the same effect as your page?

3) Network Connection Details already shows NBT as enabled, so could simply following your purge instructions alone rectify resolution problems (even though I'll still set up WINS for shortcuts!)?
Message 13 of 19
dbott67
Guide

Re: Script to Connect to Network Drive

daztrue wrote:
1) Just as I have DHCP set up on my router (ProSafe SRXN3205), rather than NAS, I wondered if the same was possible for WINS. The Primary DNS Server is the router's LAN IP, so is the same possible for WINS? The SRNXN3205 manual states:

By default, the VPN firewall [SRXN3205] will function as a DHCP (Dynamic Host Configuration Protocol) server, allowing it to assign IP, DNS server, WINS Server, and default gateway addresses to all computers connected to the LAN.


No problem having the firewall/router/whatever acting as DHCP server, DNS, etc. If the firewall allows for setting WINS server, just use the IP address of the NAS and when a client requests an IP from the DHCP server, all appropriate options (IP, subnet, gateway, DNS, WINS, etc.) will automatically get configured.

All you need to do is:

1. Enable WINS on the NAS (step #1 on my how-to. you can skip step #2 on my how-to, as your firewall can assign WINS automagically).
2. Configure firewall's DHCP server to assign WINS server for each client (using IP of NAS). No need to change anything else. DNS will remain IP of firewall.
3. Release & renew IP address on each client, followed buy purge:

ipconfig /release

ipconfig /renew

nbtstat -R
Message 14 of 19
daztrue
Aspirant

Re: Script to Connect to Network Drive

dbott67 wrote:
3. Release & renew IP address on each client, followed buy purge:

Thanks for syntax tips! :thumbsup: But why is the release and renew necessary when it wasn't at client level, and why isn't this dealt with at LAN level? I'm specifically thinking about how I've currently got some IPs reserved within DHCP pool, being used by clients.

dbott67 wrote:
1. Enable WINS on the NAS (step #1 on my how-to. you can skip step #2 on my how-to, as your firewall can assign WINS automagically).

Even easier. Great! 🙂 But I was actually wondering whether it was possible for the router itself to be the WINS server, rather than the NAS. If not, no problem; I'm just curious as ideally the router is obviously the centre of the LAN, and the Primary DNS Server uses the router/LAN IP, so I wondered if the WINS Server could do the same. :?:
Message 15 of 19
dbott67
Guide

Re: Script to Connect to Network Drive

daztrue wrote:
But I was actually wondering whether it was possible for the router itself to be the WINS server,


The router likely does not have the capability to perform WINS built-in (most do not). In fact, most don't even have DNS capabilities --- they merely forward the DNS request to the upstream (ISP) DNS server.

daztrue wrote:
If not, no problem; I'm just curious as ideally the router is obviously the centre of the LAN, and the Primary DNS Server uses the router/LAN IP, so I wondered if the WINS Server could do the same.


The router is acting as the central point, however, it forwards WINS requests to the NAS to perform the lookup.

daztrue wrote:
But why is the release and renew necessary when it wasn't at client level,


The DHCP settings are sent to the client during boot. The renew & release is required merely to update the computer's DHCP-assigned settings (specifically, the addition of the WINS server). You could also just reboot the PC, but the release/renew/purge is quicker.

daztrue wrote:
I've currently got some IPs reserved within DHCP pool, being used by clients.


If they are "static" reservations (i.e. the IP addressed is reserved in the DHCP by MAC address of client), then the same release/renew commands will update the DHCP settings to include the WINS server address. The purge (nbtstat -R)will flush any cached addresses and force the client to request new ones from the WINS server.
Message 16 of 19
dbott67
Guide

Re: Script to Connect to Network Drive

PS - I mistyped the switch in the nbtstat command. It should be:

nbtstat -R


(I had originally type /R)
Message 17 of 19
daztrue
Aspirant

Re: Script to Connect to Network Drive

Followed how-to 1; set up WINS on router with NAS IP; released, renewed, purged; rebooted, but no change.

net view only displayed PC in use (NAS & router only other devices on).

I even checked some other settings (new router) and enabled UPnP, successfully pinged the NAS, but it made no difference. And the reserved IP settings in the DHCP pool for the devices are the same. Could this be the reason? These settings are in LAN Groups, so (I think) if I delete them from there would it help? If so, should I still leave the NAS listed (see below)?

dbott67 wrote:
If they are "static" reservations (i.e. the IP addressed is reserved in the DHCP by MAC address of client), then the same release/renew commands will update the DHCP settings to include the WINS server address. The purge (nbtstat -R)will flush any cached addresses and force the client to request new ones from the WINS server.

The NAS was/is set up with a static IP, and configured on the router/firewall as static. Its IP is outside of DHCP server pool.
Message 18 of 19
daztrue
Aspirant

Re: Script to Connect to Network Drive

I was having this same problem with my previous router (although not originally), and I'm not convinced that it's a router issue. For example, in addition to not being able to view the NAS on my wireless laptop (without entering IP, although since WINS, I can now enter \\hostname), I'm also unable to access the laptop from my wired PC, even though the laptop can access the PC! :?

After a lot of attempts with a Netgear engineer on the phone with my previous router we discovered that when I connect my laptop to the router by Ethernet it recognises the NAS with no problems.

:evil: :evil: :evil: :evil:

Can you think of any other possible reasons for these symptoms? And could the fact that the NAS IP is outside of the DHCP pool be anything to do with WINS not appearing to resolve the symptoms?

I also mentioned previously that the laptop's Network Connection Details updated according to router settings, yet DNS and WINS settings (especially IP) don't display in their respective tabs in Network Connection Properties. Is this right?

Network Connection Details (NCD) showed the LAN IP as the Primary DNS Server, obviously as a default as there isn't actually anything entered in that field on the router. I entered the IP (hoping it would resolve issues), only to find NCD showing the same IP twice - so I reversed the setting. :cry:
Message 19 of 19
Top Contributors
Discussion stats
  • 18 replies
  • 6867 views
  • 0 kudos
  • 8 in conversation
Announcements