VB Script problems when replacing a logon script

I am using a VB script to map a network drive and to also add network printers to every computer on the network.  I had been using a logon script (batch file) to map the drive, but chose to use the vb script when I added the printers.  The problem that I am having is that the vb script fails if there is a previous mapped drive with the same drive letter, or if there is already a printer with the same name.  I want to know if there is something that I can add to the script that will tell it to overwrite the mapped drives and printers that have the same drive letters or names.  Here is an example of the script that I am using.



Set objWSHNetwork = CreateObject("WScript.Network")  'create network object
strConnectString = "\\Dellserver\8400DX"
strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)

Dim WshNetwork
Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "P:", "\\192.168.0.10\Public"
WshNetwork.MapNetworkDrive "Q:", "\\192.168.0.10\DCC"
WshNetwork.MapNetworkDrive "o:", "\\192.168.0.53\FTP"


wdabbsAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
MrBassConnect With a Mentor Commented:
Actually for printers it's even simpler:

Set objWSHNetwork = CreateObject("WScript.Network")  'create network object
strConnectString = "\\Dellserver\8400DX"

On Error Resume Next
objWshNetwork.RemovePrinterConnection strConnectString

strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)

- OR -

strConnectString = "\\Dellserver\8400DX"

Set curPrinters = objWshNetwork.EnumPrinterConnections
For i = 0 to curPrinters.Count -1
    If curPrinters.Item(i) = strConnectString Then objWshNetwork.RemovePrinterConnection strConnectString
Next
strResult = objWSHNetwork.AddWindowsPrinterConnection(strConnectString)
0
 
merowingerCommented:
WshNetwork.MapNetworkDrive "o:", "\\192.168.0.53\FTP" /p:yes


mero
0
 
MrBassCommented:
Create a loop at the beginning of your script to remove any drive mappings you want to remap:

Dim WshNetwork
Dim strRequiredDrives

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

strRequiredDrives = "P:,Q:,O:"

Set curDrives = WshNetwork.EnumNetworkDrives
For i = 0 to curDrives.Count -1
   If Len(curDrives.Item(i)) > 0 And InStr(strRequiredDrives,Left(curDrives.Item(i),2)) Then
      WshNetwork.RemoveNetworkDrive curDrives.Item(i),True
   End If
Next

WshNetwork.MapNetworkDrive "P:", "\\192.168.0.10\Public"
WshNetwork.MapNetworkDrive "Q:", "\\192.168.0.10\DCC"
WshNetwork.MapNetworkDrive "O:", "\\192.168.0.53\FTP"

You can do the same sort of thing for printers using WshNetwork.EnumPrinterConnections
0
All Courses

From novice to tech pro — start learning today.