Link to home
Start Free TrialLog in
Avatar of HKFuey
HKFueyFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Map Drives vbs causes errors

I have this script to map drives in AD 2003: -
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive "K:" , "\\files\shared"
objNetwork.MapNetworkDrive "S:" , "\\files\users"

The users get errors if drive is already mapped does anyone know the syntax to delete then map drives?
SOLUTION
Avatar of MatthiasVance
MatthiasVance
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of HKFuey

ASKER

Hi MatthiasVance
I did try that but I get errors if the drive is not mapped already!
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

You can use the EnumNetworkDrives Method of the Wscript.Network object

something like

            set objdrive = objNetwork.EnumNetworkDrives
            For intDrive = 0 to objDrive.Count -1 Step 2
                  intNetLetter = IntNetLetter +1
                  letter=objDrive.Item(intDrive)
                  unc=objDrive.Item(intDrive +1)
                  If letter="K:" and unc <> "\\files\shared" Then
                        oNet.RemoveNetworkDrive "K:", true, true
                  End If
                  If letter="S:" and unc<>"\\files\users\" Then
                        oNet.RemoveNetworkDrive "S:", true, true
                  End If
            Next
            oNet.MapNetworkDrive "K:", "\\files\shared", true
                                oNet.MapNetworkDrive "S:", "\\files\users", true