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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
MrBassCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows 2000

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.