QTP: Mapping/Disconnecting remote drives in VBS

I've been seeing lots of questions on this in the forums on the MIC support site, so I thought I'd provide an example of how to do this via a resource (VBS) file. Naturally, this can be hacked up and used a needed.

Public Function MapRemoteDrive(ByVal strDriveLetter, ByVal strPath)
'***************************************************************************
'* Name: MapRemoteDrive
'* Author: Theo Moore
'* Assumptions: None
'* Inputs:
'* - strDriveLetter - Drive letter to use
'* - strPath - The path to which you want to map
'*
'* Outputs:
'* - None
'*
'* Notes: If the path to which you are mapping requires a user name and password,
'* it should be included in the strPath param using the following example:
'* /user:
'****************************************************************************

Dim objShell 'As Object
Dim objFSO 'As FileSystemObject
Dim lngCount 'As Long
Dim blnFail 'As Boolean

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Create the correct path based on date:

blnFail = False

'Make sure to remove any other Z: drive
Do
'Track number of attempts
objShell.Run "net.exe use " & strDriveLetter & " /delete", 0, True
lngCount = lngCount + 1
Wait(2)
If lngCount = 5 Then
blnFail = True
lngCount = 0 'Reset for the next loop
Exit Do
End If
Loop Until Not objFSO.DriveExists("Z:")

If blnFail = False Then 'We know the disconnect went correctly
Do Until objFSO.DriveExists("Z:")
objShell.Run "net.exe use " & strDriveLetter & " " & strPath
lngCount = lngCount + 1
Wait(2)
If lngCount = 5 Then 'Something went wrong
blnFail = True
lngCount = 0
Exit Do
End If
Loop
End If

Set objShell = Nothing
Set objFSO = Nothing

'Flip the value of blnFail to
'make it more user readable.
MapRemoteDrive = Not blnFail
End Function

Public Function DisconnectRemoteDrive(strDriveLetter)
'***************************************************************************
'* Name: DisconnectRemoteDrive
'* Author: Theo Moore
'* Assumptions: None
'* Inputs:
'* - strDriveLetter - Drive letter to use
'*
'* Outputs:
'* - None
'*
'* Notes: Simply disconnects the remote drive as indicated by the strDriveLetter
'****************************************************************************
Dim blnFail 'As Boolean
Dim objShell 'As Shell
Dim objFSO 'As Scripting.FileSystemObject
Dim lngCount 'As Long

blnFail = False

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Do
'Track number of attempts
objShell.Run "net.exe use " & strDriveLetter & " /delete", 0, True
lngCount = lngCount + 1
Wait(2)
If lngCount = 5 Then
blnFail = True
lngCount = 0 'Reset for the next loop
Exit Do
End If
Loop Until Not objFSO.DriveExists("Z:")

Set objShell = Nothing
Set objFSO = Nothing

DisconnectRemoteDrive = Not blnFail
End Function

posted @ Wednesday, October 04, 2006 1:52 PM

Print

Comments on this entry:

# re: QTP: Mapping/Disconnecting remote drives in VBS

Left by Alex Feinman at 10/5/2006 3:34 PM
Gravatar
You might want to check out WshNetwork object

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/438d1705-fb98-4a63-86e5-a8cc01f4ce16.asp

Also, these scripts you posted look broken. You might want to take another look at the code

# re: QTP: Mapping/Disconnecting remote drives in VBS

Left by Theo Moore at 10/6/2006 4:47 AM
Gravatar
Thank you for the input! I'll certainly look at the WshNetwork object as suggested.

Did you try running these in QTP? They work fine; connect and disconnect like a champ. Was there something specific you felt was broken?

T.

# re: QTP: Mapping/Disconnecting remote drives in VBS

Left by ReMovHer at 3/12/2008 7:02 AM
Gravatar
http://www.mail-archive.com/activedir@mail.activedir.org/msg41684.html


Subject: RE: [ActiveDir] OT: _vbscript_ to disconnect and reconnect persistent drive mappings


Lot of work and code there to remove drives...try this...

Option Explicit
Dim WshNetwork

on error resume next

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

WshNetwork.RemoveNetworkDrive "m:"
WshNetwork.RemoveNetworkDrive "n:"
WshNetwork.RemoveNetworkDrive "o:"
WshNetwork.RemoveNetworkDrive "p:"
WshNetwork.RemoveNetworkDrive "q:"
WshNetwork.RemoveNetworkDrive "r:"
WshNetwork.RemoveNetworkDrive "s:"
WshNetwork.RemoveNetworkDrive "t:"
WshNetwork.RemoveNetworkDrive "u:"
WshNetwork.RemoveNetworkDrive "v:"
WshNetwork.RemoveNetworkDrive "w:"
WshNetwork.RemoveNetworkDrive "x:"
WshNetwork.RemoveNetworkDrive "y:"
WshNetwork.RemoveNetworkDrive "z:"

...add all the letters you want. :)
Comments have been closed on this topic.
«December»
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910