Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Script to tell me if a drive doesn't map.

Status
Not open for further replies.

mark01

Technical User
Joined
Jan 17, 2001
Messages
600
Location
US
I have a script that will map about 4 drives. How can I make the script tell me if any of the drives never map? (Sometimes the drive wont map because the user changed the password)

-=This is my code for mapping drives=-
Sub MapDrive(strDrive,strShare)
WSHNetwork.MapNetworkDrive strDrive, strShare
If Err.Number Then

WSHNetwork.RemoveNetworkDrive strDrive
WSHNetwork.MapNetworkDrive strDrive, strShare

End If

End Sub
 
Hello mark01,

Off-hand, you can use the construction as a function returning the error number. Besides, if the script throws a runtime error, the drive is not mapped at all, hence, no need to remove it. The construction would be something like this.
Code:
Function MapDrive(strDrive,strShare)
   On Error Resume Next
   Dim WshNetwork
   Set WshNetwork = CreateObject("WScript.Network")
   WshNetwork.MapNetworkDrive strDrive, strShare
   If Err.Number<>0 Then
      MapDrive = err.number
    Else
      MapDrive = 0
    End If
    err.clear
    Set WshNetwork = Nothing
    On Error Goto 0
End Function
A couple of lines there are not really necessary just being a better practice in case you expand the function.

regards - tsuji
 
Hmm, I can't seem to get that to work...
 
mark01,

What is not working? I'm on the way out, maybe I might get your feedback before.

- tsuji
 
Well I put this code inplace of mine, and it still doesn't tell me if the drive didn't map. I setup a drive mapping to a non-existant place, and it didn't tell me, that it didn't map.

Thanks
 
mark01,

In the main, your read the return value of the (now) &quot;function&quot; MapDrive(). If it is zero, that means the drive is mapped else you get the runtime error number.
Code:
'... your code till the mapping
Dim iReturn
iReturn = MapDrive(strDrive,strShare)  '<<<use your actual parameter
If iReturn <> 0 Then
   wscript.echo &quot;Runtime error : &quot; & cstr(iReturn) & vbcrlf & &quot;Drive mapping failed.&quot;
Else
   wscript.echo &quot;Drive mapping done.&quot;
End If
'... continue your code
regards - tsuji
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top