[dba-Tech] reconnect/logon to network drive

Steve Erbach erbachs at gmail.com
Tue May 20 10:06:44 CDT 2008


Peter,

Do these help at all?

<http://www.visualbasicscript.com/m_32231/mpage_1/key_/tm.htm#32231>

Also, Experts Exchange had this:

~~~~~~~~~~~~~~~~~~~~~~~~
Dissconnect Reconnect Map Drive VBS script.

Question: I am trying to write a simple script that will  disconnect
two mapped drives and then reconnect them. currently I am using this
script.

'-------------------
ON ERROR RESUME NEXT

Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj, Path


Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
'Automatically find the domain name
Set objDomain = getObject("LDAP://rootDse")
DomainString = objDomain.Get("dnsHostName")

'Grab the user name
UserString = WSHNetwork.UserName
'Bind to the user object to get user name and check for group memberships later
Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)

'Grab the computer name for use in add-on code later
strComputer = WSHNetwork.ComputerName


'Synchronizes the time with Server our NTP Server
WSHShell.Run "NET TIME \\server /set /y"

'Disconnect any drive mappings as needed.
WSHNetwork.RemoveNetworkDrive "P:", True, True
WSHNetwork.RemoveNetworkDrive "Z:", True, True

'Disconnect ALL mapped drives
Set clDrives = WshNetwork.EnumNetworkDrives
For i = 0 to clDrives.Count -1 Step 2
    WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True
Next

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

'Map drives needed by all
'Note the first command uses the user name as a variable to map to a user share.
WSHNetwork.MapNetworkDrive "P:", "\\server1\public",True
WSHNetwork.MapNetworkDrive "Z:", "\\server\easy Dental",True

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

'Add On Code goes below this line
'=====================================

'Adding the Notepad Application to the SendTo Menu
strSendToFolder = WSHShell.SpecialFolders("SendTo")
strPathToNotepad = WinDir & "windows\system32\Notepad.exe"
Set objShortcut = WSHShell.CreateShortcut(strSendToFolder & _
 "\Notepad.lnk")
objShortcut.TargetPath = strPathToNotepad
objShortcut.Save



'=====================================
'Add On Code goes above this line

'Clean Up Memory We Used
set UserObj = Nothing
set GroupObj = Nothing
set WSHNetwork = Nothing
set DomainString = Nothing
set WSHSHell = Nothing
Set WSHPrinters = Nothing


'Quit the Script
wscript.quit
'----------------

It will only map the first drive P it will not map Z. If I switch them
then it will map Z instead of P. Nayone know what I am doing wrong?
Your help would be appreciated.
Tags:
drive, vbs, script, map, reconnect

Zone:
VB Script


Author:
m_m_cooper, Premium Service Member




RobSampson:
Hi, try some of the options used here:
http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_22752236.html

I suggested to use objExec, and wait for that process to finish, but
unfortunately, it appeared that WScript.Sleep between each one may be
the only way.....

Regards,

Rob.




 you were looking for?Yes No
RobSampson:
Alternatively, comment out the On Error Resume Next statement, then
see if you receive any errors when it gets to the mapping of the
second drive....

Rob.



chandru_sol:
Hi,

Can you try the below code? I have added a message box once the drives
are delete to make sure it is getting deleted.

Can you also make sure that the location given for P and Z drive can
be accessed using the same URL from your machine?

ON ERROR RESUME NEXT

Dim WSHShell, WSHNetwork, objDomain, DomainString, UserString, UserObj, Path

Set WSHShell = CreateObject("WScript.Shell")
Set WSHNetwork = CreateObject("WScript.Network")
'Automatically find the domain name
Set objDomain = getObject("LDAP://rootDse")
DomainString = objDomain.Get("dnsHostName")
'Find the Windows Directory
WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")

'Grab the user name
UserString = WSHNetwork.UserName
'Bind to the user object to get user name and check for group memberships later
Set UserObj = GetObject("WinNT://" & DomainString & "/" & UserString)

'Grab the computer name for use in add-on code later
strComputer = WSHNetwork.ComputerName


'Synchronizes the time with Server our NTP Server
WSHShell.Run "NET TIME \\server /set /y"

'Disconnect any drive mappings as needed.
WSHNetwork.RemoveNetworkDrive "P:", True, True
WSHNetwork.RemoveNetworkDrive "Z:", True, True

'Disconnect ALL mapped drives
'Set clDrives = WshNetwork.EnumNetworkDrives
'For i = 0 to clDrives.Count -1 Step 2
'    WSHNetwork.RemoveNetworkDrive clDrives.Item(i), True, True
'Next

'Give the PC time to do the disconnect, wait 300 milliseconds
wscript.sleep 3000
wScript.Echo "Removed"

'Map drives needed by all
'Note the first command uses the user name as a variable to map to a user share.
WSHNetwork.MapNetworkDrive "P:", "\\server1\public",True
WSHNetwork.MapNetworkDrive "Z:", "\\server\easy Dental",True

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

'Add On Code goes below this line
'=====================================

'Adding the Notepad Application to the SendTo Menu
strSendToFolder = WSHShell.SpecialFolders("SendTo")
strPathToNotepad = WinDir & "windows\system32\Notepad.exe"
Set objShortcut = WSHShell.CreateShortcut(strSendToFolder & _
 "\Notepad.lnk")
objShortcut.TargetPath = strPathToNotepad
objShortcut.Save
'=====================================
'Add On Code goes above this line

'Clean Up Memory We Used
set UserObj = Nothing
set GroupObj = Nothing
set WSHNetwork = Nothing
set DomainString = Nothing
set WSHSHell = Nothing
Set WSHPrinters = Nothing


'Quit the Script
wscript.quit

regards
~~~~~~~~~~~~~~~~~~~~~~~~

Steve Erbach
Neenah, WI


On Sun, May 18, 2008 at 11:58 AM, Peter Brawley
<peter.brawley at earthlink.net> wrote:
> A couple of our XP PCs go for days or weeks between reboots, and
> sometimes they "forget" network drives they connected to at bootup,
> which is a pain eg for backup scripts. Three questions about this:
>
> 1. What can we do to prevent these XP boxes dropping network drive
> connections?
>
> 2. Google finds lots of vbs scripts for mapping network drives, but not for
> --- reconnecting to an already mapped network drive
> --- logging onto a mapped network drive that the user hasn't yet logged
> onto manually.
> Anybody know where to find them? TIA.
>
> PB



More information about the dba-Tech mailing list