Wednesday, July 9, 2008

Mapped Network Drive Disconnection Problem

If you are using mapped network drives to share files across multiple computers across windows workgroup, you will suffer from mapped network drives random disconnection. We made a lot of testing regarding this issue and at last we found a solution that will keep mapped network drives connection.

The following is a description of issues you may face with mapped network drives and the solution for each issue.

Use IP Address Instead of Computer Name

In workgroup environment using computer name results in latency accessing shared resources. Since there is no Domain Name System Server (DNS) that resolves computer names to its corresponding IPs.

To achieve less latency between workgroup connected computers, use IP address instead of computer name to access remote computers without the need for DNS.

To use IP address in workgroup, configure each computer to have a static IP address instead of relying on DHCP. Ensure that all computers in the workgroup are using static IPs in the same range e.g. 192.168.10.1 to 192.168.10.255.

In case of existence of DHCP, ask you network administrator to reserve a set of IP addresses to use in the workgroup installation.

You can use the IP address of each PC to access it or configure windows hosts (this file can be found under C:\WINDOWS\system32\drivers\etc) file to assign each PC name to its static IP address.

Mapped Network Drive Connections

The mapped network drives configured on client PCs may suffer disconnection during windows startup or during the day. This occurs for two reasons:

  1. Computers running in Workgroup may fail to reestablish the Mapped Network Drive Connection during computer logon process.
  2. The Computer that hosts the shared folders will disconnect idle clients if no action occurred in the current session. This will result in disconnection of mapped network drives in client PCs

To resolve the above issues several actions should be taken

  1. Reconnect the network drives in workgroup PCs every time users logon to windows. This can be achieved automatically using windows script that runs during windows startup
  2. Configure the shared folder host computer to disable automatic session disconnect. This can be achieved automatically using windows script that runs during windows startup

The above actions can be performed using windows net command. To make life easier, a windows script can be used to call the required net commands. The following code snippet describes how such script can be done.

MappDrives.JS Code Snippet

This code need to run on each PC that needs to have mapped network drive. This can be done in the computer startup, as auto run entry in the host computer registry under HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run.

var shell = WScript.CreateObject("WScript.Shell");
var serverIP = "\\\\192.168.10.1";
var username = "";
var password = "";
var zDrive = serverIP+"\\Shared";
DisableAutoDisconnect();
MapDrive("Z:",zDrive);
function MapDrive(drive,share)
{
var netcmd = "net use " + drive + " " + share;
if(username != "")
{
netcmd = netcmd + " /USER:" + username + " " + password
}
netcmd = netcmd + " /P:YES";

RunCmd(netcmd);
}
function DisableAutoDisconnect()
{
var netcmd = "net config server /AUTODISCONNECT:-1";
RunCmd(netcmd);
}
function RunCmd(cmd)
{
shell.Run(cmd,0,false);
}

serverIP:
has the IP of the host PC with shared folder we want to map network drive from

username & password:
has the username and password required to access shared folders in the host PC

zDrive:
the target shared folder path [Computer Name]\\[Shared Folder Name]

MapDrive: construct the required net command to map the shared folder to Z drive.

DisableAutoDisconnect.JS Code Snippet

This script needs to run on the host PC. This can be done in the computer startup, as auto run entry in the host computer registry under HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Run.

var shell = WScript.CreateObject("WScript.Shell");
DisableAutoDisconnect();
function DisableAutoDisconnect()
{
var netcmd = "net config server /AUTODISCONNECT:-1";
RunCmd(netcmd);
}
function RunCmd(cmd)
{
shell.Run(cmd,0,false);
}

DisableAutoDisconnect: disable the host PC automatic session disconnect by setting the timeout to -1

Avoid Disconnect During the Day

To ensure that there are no disconnect will occur, additional step can be done. Schedule a periodic task on windows to run the MapDrives.JS script. You can use windows tasks scheduler to do this or implement your own scheduler in your software application.