I have this script :
I have duplicated the check function and renamed it to cs because there is one drive that needs credentials to be able to connect to it.Code:
Option Explicit
Dim StrUserName, objNetwork, user, pass, strprofile
Set objNetwork = CreateObject("Wscript.Network")
strUserName = objNetwork.UserName
Select Case strUserName
Case "UserOne":
Call check("M:","\\server\Marketing")
Case "UserTwo":
Call check("M:","\\server\Marketing")
Case Else:
Call check("K:","\\dc\company")
Call cs("S:","\\server\share",false,"username","password")
Call check("Z:","\\dc\mitn")
End Select
'Example for strDriveLetter and strRemotePath replacing drive letter and remote path with relevant paths
'strDriveLetter = "W:"
'strRemotePath = "\\alan\drivers"
Public Function check(ByRef strDriveLetter, ByRef strRemotePath)
Set objNetwork = CreateObject("Wscript.Network")
Dim CheckDrive, AlreadyConnected, intDrive
Set CheckDrive = objNetwork.EnumNetworkDrives()
' This section deals with a For ... Next loop
' See how it compares the enumerated drive letters
' with strDriveLetter
On Error Resume Next
AlreadyConnected = False
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) = strDriveLetter Then
AlreadyConnected = True
End If
Next
' This section uses the If = then, else logic
' This tests to see if the Drive is already mapped.
' If yes then disconnects
Select Case AlreadyConnected
Case "True":
objNetwork.RemoveNetworkDrive strDriveLetter
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
Case "False":
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath
End Select
End Function
Public Function cs(ByRef strDriveLetter, ByRef strRemotePath, ByRef prof, ByRef User, ByRef Pass)
Set objNetwork = CreateObject("Wscript.Network")
Dim CheckDrive, AlreadyConnected, intDrive
Set CheckDrive = objNetwork.EnumNetworkDrives()
' This section deals with a For ... Next loop
' See how it compares the enumerated drive letters
' with strDriveLetter
On Error Resume Next
AlreadyConnected = False
For intDrive = 0 To CheckDrive.Count - 1 Step 2
If CheckDrive.Item(intDrive) = strDriveLetter Then
AlreadyConnected = True
End If
Next
' This section uses the If = then, else logic
' This tests to see if the Drive is already mapped.
' If yes then disconnects
Select Case AlreadyConnected
Case "True":
objNetwork.RemoveNetworkDrive strDriveLetter
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, strprofile, user, pass
Case "False":
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, strprofile, user, pass
End Select
End Function
What I want to know is what do I have to do to be able to use the first check function and get rid of the second one and still be able to map a network drive both ways ie with or without credentials and the profile set to false ??
