Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

mapped network drives disconnects after logon 1

Status
Not open for further replies.

9991

IS-IT--Management
Aug 7, 2006
2
US
I am using "mapnetworkdrive" to map drives". unfortunately, they get disconnected in explorer after little while. applications mapped to these drive gets network path not found error. The only refresh when you put the cursor on them in explorer. Anybody know how to keep these drives connected or refreshed at all times in explorer. Here is the code:

"Option Explicit
'-----------------------------------------------------------------------------------------
' NAME: LogonScript
'-----------------------------------------------------------------------------------------
' Step1-Map network drives for a user
'-----------------------------------------------------------------------------------------
Sub MapNetworkDrives
ON ERROR RESUME Next

Dim objShell 'This is the shell object
Dim objNetwork ' This is our network object
Dim objDomain 'This is our Domain Object
Dim strDomainName 'This is our Domain Name
Dim strUserName 'This is our user string
Dim objUser 'This is our UserObject
Dim objGroup 'This is our group object
Dim strLowerCaseGroupName 'This String holds the Lower Case group Name
Dim clDrives 'Collection of Drives
Dim strShareConnected '
Dim strLocalDrive 'Name of Local Drive
Dim bolFoundExisting '
Dim bolFoundRemembered 'Bolean variable thru which drive is remembered
Dim i 'Array Counter
Dim objReg 'Registry for mapped drive
Dim APP1, APP3
Const HKCU = &H80000001 'Constant that represent the HKCU Hive

APP1 = "\\server1\"
APP3 = "\\server2\"

'Create an instance of the Shell
Set objShell = CreateObject("WScript.Shell")

'Create an instance of the Network
Set objNetwork = CreateObject("WScript.Network")

'Automatically find the domain name
Set objDomain = GetObject("LDAP://rootDse")
strDomainName = objDomain.Get("dnsHostName")

'Grab the user name
strUserName = objNetwork.UserName

'Bind to the user object to get user name and check for group memberships later
Set objUser = GetObject("WinNT://" & strDomainName & "/" & strUserName)

Dim strDrive 'Temp variable for storing drive letters

'Disconnect Production mapped drives
Set objNetwork = WScript.CreateObject("WScript.Network")
objNetwork.RemoveNetworkDrive "G:"
objNetwork.RemoveNetworkDrive "H:"
objNetwork.RemoveNetworkDrive "I:"
objNetwork.RemoveNetworkDrive "J:"
objNetwork.RemoveNetworkDrive "L:"
objNetwork.RemoveNetworkDrive "S:"
objNetwork.RemoveNetworkDrive "X:"

' 'If there's a remembered location (persistent mapping) delete the associated HKCU registry key
' If bolFoundExisting <> True Then
' Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
' objReg.GetStringValue HKCU, "Network\" & Left(strLocalDrive, 1), "RemotePath", strShareConnected
' If strShareConnected <> "" Then
' objReg.DeleteKey HKCU, "Network\" & Left(strLocalDrive, 1)
' Set objReg = Nothing
' bolFoundRemembered = True
' End If
' End If

'Give the PC time to do the disconnect, wait 300 milliseconds
wscript.sleep 300

'Now check for group memberships and map appropriate drives
For Each objGroup In objUser.Groups
'Get the group name and change it to lowercase
'Note: This can be either Lowercase (LCase) or Uppercase (UCase)
strLowerCaseGroupName = LCase (objGroup.Name)
Select Case strLowerCaseGroupName
'Check for group memberships and take needed action

Case "it-central-admin"

objNetwork.MapNetworkDrive "I:", APP1 & strUserName & "$", True
objNetwork.MapNetworkDrive "J:", APP3 & "CADMINAPPS$", True
objNetwork.MapNetworkDrive "S:", APP1 & "CADMIN$", True
objNetwork.MapNetworkDrive "X:", APP3 & "APPS", True

Case "it-network-support"

objNetwork.MapNetworkDrive "I:", APP1 & strUserName & "$", True
objNetwork.MapNetworkDrive "S:", APP1 & "groups", True
objNetwork.MapNetworkDrive "X:", APP1 & "apps", True

End Select
Next

End Sub
'END CALL To MAP DRIVES

Call MapNetworkDrives"

BenQ.
 
Is this happening with all your machines on the network or just a test box? Without looking at your code close it sounds like you are simply losing the network connection for a frief period. I've seen this happen before and even more so over VPN tunnels.

I found that to be the case by mapping the drives without scripting and seeing the same behavior


____________ signature below ______________
General FAQ faq333-2924
5 steps to asking a question faq333-3811
 
I have tried to modify this script to allow me to automatically use 2003 AD to map the default printer based on membership of 4 distribution groups

' New Logon script
On Error Resume Next

'4 initial object creation
Dim objNetwork, objGroup , UserObj
Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
DomainString = "Company.co.uk"
UserString = WSHNetwork.UserName

set objNetwork = WScript.createObject("WScript.Network")
Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)

'14 Welcome Message
Dim strDomain, strUser
strDomain = ObjNetwork.UserDomain
strUser = ObjNetwork.UserName

MsgBox "Welcome to the " & strDomain & " Network, " & strUser & "!"

'21 Drive Mapping

objNetwork.MapNetworkDrive "G:", "\\Servername\Data1"
objNetwork.MapNetworkDrive "H:", "\\Servername\Accounts"
objNetwork.MapNetworkDrive "I:", "\\Servername\Documents"
objNetwork.MapNetworkDrive "J:", "\\Servername\Personnel"
objNetwork.MapNetworkDrive "K:", "\\Servername\Trescray Utils"
objNetwork.MapNetworkDrive "L:", "\\Servername\Electrical"
objNetwork.MapNetworkDrive "M:", "\\Servername\Archive"
objNetwork.MapNetworkDrive "N:", "\\Servername\Fuji"
objNetwork.MapNetworkDrive "O:", "\\Servername\Homedirs\" & strUser
objNetwork.MapNetworkDrive "P:", "\\Servername\Apps"
objNetwork.MapNetworkDrive "T:", "\\Servername\TestResults"
objNetwork.MapNetworkDrive "V:", "\\Servername\Fiber"
objNetwork.MapNetworkDrive "X:", "\\Servername\Fluor"
objNetwork.MapNetworkDrive "Z:", "\\Servername\Admin Documentation"

'38 Printer Mapping

objNetwork.AddWindowsPrinterConnection "\\Servername\First Floor C450", "Konica Minolta C450 PCL"
objNetwork.AddWindowsPrinterConnection "\\Servername2\Second Floor C450", "Konica Minolta C450 PCL"

objNetwork.AddWindowsPrinterConnection "\\Servername2\First Floor C450", "Konica Minolta C450 PCL"
objNetwork.AddWindowsPrinterConnection "\\Servername\2nd Floor C450", "Konica Minolta C450 PCL"

objNetwork.AddWindowsPrinterConnection "\\Servername\First Floor - Laserjet 2200dn", "HP Laserjet 2200 Series PCL"
objNetwork.AddWindowsPrinterConnection "\\Servername2\First Floor - Laserjet 2200dn", "HP Laserjet 2200 Series PCL"

objNetwork.AddWindowsPrinterConnection "\\Servername\Second Floor - Laserjet 4050", "HP Laserjet 4050 Series PCL"
objNetwork.AddWindowsPrinterConnection "\\Servername2\Second Floor - LaserJet 4050", "HP Laserjet 4050 Series PCL"

objNetwork.AddWindowsPrinterConnection "\\Servername\StoresLJ3200", "HP Laserjet 4M"
objNetwork.AddWindowsPrinterConnection "\\Servername2\Stores Department - LaserJet 3200", "HP Laserjet 4M"

'55 Now check for group memberships and map appropriate drives
For Each GroupObj In UserObj.Groups

Select Case GroupObj.Name
'59 Check for group memberships and take needed action
Case "1stFloor User"
objNetwork.SetDefaultPrinter "\\Servername\First Floor C450"
Case "2ndFloor User"
objNetwork.SetDefaultPrinter "\\Servername\Second Floor C450"
Case "3rdFloor User"
objNetwork.SetDefaultPrinter "\\Servername\Second Floor C450"
Case "CAD User"
objNetwork.AddWindowsPrinterConnection "\\Servername\CAD Department - DesignJet 800", "HP DesignJet 800 42 by HP"
objNetwork.AddWindowsPrinterConnection "\\Servername2\CAD Department - DesignJet 800", "HP DesignJet 800 42 by HP"

End Select

Next

'74 Synchronizes the time with Main AD Server our NTP Server
WSHShell.Run "NET TIME \\AD Servername /set /y"
Has anybody got any idea's why this does not work ???

Thanks
 
Guys, you make me sad. [sadeyes] Those scritps look awefully familiar but I don't see any reference to the original author. faq329-5798

9991, you are experienceing a problem with the AutoDisconnect. On each server and workstation run the command: net config server /autodisconnect:-1

Alternatively you can edit the registry via script. Manual steps are:
1. Click Start, click Run, type regedit (Windows 2000 or Windows Server 2003) or type regedt32 (Windows NT 4.0), and then click OK.
2. Locate and then click the following key in the registry:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters
3. In the right pane, click the autodisconnect value, and then on the Edit menu, click Modify. If the autodisconnect value does not exist, follow these steps: a. On the Edit menu, point to New, and then click REG_DWORD.
b. Type autodisconnect, and then press ENTER.

4. Click Hexadecimal.
5. In the Value data box, type ffffffff, and then click OK.


Ntman2000, the NET TIME command should not be used unless you have NT machines still in your domain. If you have Windows 2000 clients or greater then AD will manage that for you. I originally had this in my FAQ because when I first published it there was still a lot of NT around. It has long since been removed from my FAQ. Please do not hijack threads. Best to start your own.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
Mark

Firstly I have cobbled this script from several places not just one (but thanks to all those I have copied from)

Secondly I was not trying to Hijack this thread I though that the fact we are both trying to script with a group membership check was simalar and therefore was a related section so I apologise to 9991 for any offence

Thirdly although you almost definately know more about scripting than I do you missed the point relevant to this thread that this script does not work in relation to checking if the user is a member of a group and then setting the correct default printer as per lines 55 to 74 which is why I included it on this thread !



 
NTman2000, I have addressed your issue in the seperate thread you opened. As this was unrelated to the OPs question of auto disconnecting mapped drives I did not respond to that intentionally as it would fork the thread, so no, I did not miss the point.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
 
markdmac, I have to give you a star for this tip because this is a challenge I have seen more than once on multiple forums and your reponce has to be the most consice answer I have seen yet. Thanks!!!!

Why not just add code to the script to add the setting to regitry??

Code:
Set oShell = CreateObject("WScript.Shell") 

sRegKey = "HLM\SYSTEM\CurrentControlSet\Services\lanmanserver\parameters" 

' check for marker 
sRegMarkerValue = ""  ' init value 
sRegMarkerValue = oShell.RegRead( sRegKey & "\autodisconnect") 
On Error Goto 0 

If sRegMarkerValue <> "ffffffff" Then 
   oShell.RegWrite sRegKey & "\coreInstalled", "ffffffff" 
End If

Thanks

John Fuhrman
Titan Global Services
 
oops...


the regwrite line should have been
Code:
oShell.RegWrite sRegKey & "\autodisconnect", "ffffffff"

Sorry about that..

hope this helps.


Thanks

John Fuhrman
Titan Global Services
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top