VBScript MapNetworkDrive错误处理

VBScript MapNetworkDrive错误处理,vbscript,error-handling,Vbscript,Error Handling,我正试图用VBScript编写一个小脚本,只供我在家使用,它是在Macrium Reflect的定时备份之前运行的 我遇到了一个看似很小的问题,那就是网络驱动器物理断开连接时的错误处理,即未连接电缆 此时,脚本会检查驱动器是否已连接,如果驱动器未连接,则会显示一条消息,告诉用户连接电缆并按“是” 现在,一切都很好,用户会按照要求连接电缆,然后按下“是”按钮,但我想知道在连接驱动器电缆之前按下“是”的时间 在代码中有一个“On Error Resume Next”(错误恢复下一步),它掩盖了这种可

我正试图用VBScript编写一个小脚本,只供我在家使用,它是在Macrium Reflect的定时备份之前运行的

我遇到了一个看似很小的问题,那就是网络驱动器物理断开连接时的错误处理,即未连接电缆

此时,脚本会检查驱动器是否已连接,如果驱动器未连接,则会显示一条消息,告诉用户连接电缆并按“是”

现在,一切都很好,用户会按照要求连接电缆,然后按下“是”按钮,但我想知道在连接驱动器电缆之前按下“是”的时间

在代码中有一个“On Error Resume Next”(错误恢复下一步),它掩盖了这种可能性,因此我注释掉了这一行&事实上,我在第40行得到了一个错误“网络路径未找到”:

objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, _
我想使用此捕获的错误向用户显示驱动器尚未连接的警报,请连接并重试&继续重试,直到驱动器实际连接

我的问题是,我似乎找不到在哪里添加任何错误处理代码来显示此消息

这是我的密码:

Option Explicit
Dim strDriveLetter, strRemotePath, strUser, strPassword, strProfile, strName, objNetwork, objShell, CheckDrive, AlreadyConnected, intDrive

' The section sets the variables. 
strDriveLetter = "X:" 
strRemotePath = "\\192.168.1.1\shared"
strUser = "user"
strPassword = "password"
strProfile = "true"
strName = "Backup Drive"

' This sections creates two objects:
' objShell and objNetwork and counts the drives
Set objShell = CreateObject("WScript.Shell") 
Set objNetwork = CreateObject("WScript.Network") 
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
Next 

If AlreadyConnected = False Then 

Dim result
result = MsgBox("A Backup Is Now Due But The Drive Is Not Connected." & vbNewLine & vbNewLine & "Please Connect The Drive & Press YES To Continue." & vbNewLine & vbNewLine & "If You Wish To Postpone Backup Then Press NO Now.", 4 + 32, "BACKUP DRIVE NOT CONNECTED")
If result = 7 Then
WScript.Quit
Else
Call MapDRV
End If

Sub MapDRV()
Set objNetwork = WScript.CreateObject("WScript.Network") 
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath, _
strProfile, strUser, strPassword
Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(strDriveLetter).Self.Name = strName
End Sub

WScript.Quit
错误处理代码大致如下:

If Err.Number <> 0 Then
    'error handling:
    'ALERT USER HERE
Err.Clear
End If
如果错误号为0,则
'错误处理:
'此处提醒用户
呃,明白了
如果结束
任何帮助都将不胜感激

参考资料不提供有用的指导。您需要为每个容易发生的操作分别捕获错误或成功。
通用规则():通过错误转到0时的
禁用错误处理,并仅对可疑操作启用它

例如,失败的原因可能不止一个(服务器离线、用户被阻止、密码错误/更改等):

整个脚本可以如下所示:

Option Explicit
On Error GoTo 0

Dim strResult: strResult = Wscript.ScriptName

Dim strDriveLetter, strRemotePath, strUser, strPassword, strProfile , strName _
  , objNetwork, objShell, CheckDrive, AlreadyConnected, intDrive

' The section sets the variables. 
strDriveLetter = "X:" 
strRemotePath = "\\192.168.1.1\shared"
strUser = "user"
strPassword = "password"
strProfile = "true"
strName = "Backup Drive"

' This sections creates two objects:
' objShell and objNetwork and counts the drives
Set objShell = CreateObject("WScript.Shell") 
Set objNetwork = CreateObject("WScript.Network") 

' This section deals with a For ... Next loop
' See how it compares the enumerated drive letters with strDriveLetter
Dim result, toShare

AlreadyConnected = False
Do While AlreadyConnected = False
  strResult = strResult & vbNewLine & "--- new check"
  AlreadyConnected = False
  Set CheckDrive = objNetwork.EnumNetworkDrives() 
  For intDrive = 0 To CheckDrive.Count - 1 Step 2 
    If CheckDrive.Item(intDrive) = strDriveLetter Then
      AlreadyConnected = True
      toShare = CheckDrive.Item(intDrive + 1)
    End If
    strResult = strResult & vbNewLine & CheckDrive.Item(intDrive)
    strResult = strResult & vbTab & CheckDrive.Item(intDrive + 1)
  Next 
  If AlreadyConnected Then Exit Do
  result = MsgBox("A Backup Is Now Due But The Drive Is Not Connected." _
    & vbNewLine & vbNewLine & "If you wish to ..." _
    & vbNewLine & vbTab & "... postpone backup then press ABORT." _
    & vbNewLine & vbTab & "... backup to " & strRemotePath & " then press RETRY." _
    & vbNewLine & "Otherwise, please connect the drive & press IGNORE to continue." _
      , vbAbortRetryIgnore + vbQuestion, "BACKUP DRIVE NOT CONNECTED")
  Select Case result
  Case vbAbort
    Call scriptQuit
  Case vbRetry
    Call MapDRV
  Case Else
    ' The Case Else clause is not required
  End Select
Loop

  strResult = strResult & vbNewLine & "copy here to " & toShare 

Sub MapDRV
  ' no need to redefine: WshNetwork Object is already defined
  ' Set objNetwork = WScript.CreateObject("WScript.Network")
  Dim errResult
  On Error Resume Next
  objNetwork.MapNetworkDrive strDriveLetter, strRemotePath _
                      , strProfile, strUser, strPassword
  If Err.Number = 0 Then
    On Error GoTo 0
    Set objShell = CreateObject("Shell.Application")
    objShell.NameSpace(strDriveLetter).Self.Name = strName
  Else
    errResult = Err.Number & " 0x" & Hex(Err.Number) & " " & Err.Source
    errResult = errResult & vbNewLine & Err.Description
    On Error GoTo 0
    MsgBox errResult, vbOKOnly + vbCritical, "Error occurred"
    strResult = strResult & vbNewLine & vbNewLine & errResult
  End If
End Sub

Call scriptQuit

Sub scriptQuit
  Wscript.Echo strResult
  Wscript.Quit
End Sub
请注意,
strResult
变量仅用于调试目的,以查看下一个输出:

==> cscript D:\VB_scripts\SO\37776762.vbs
37776762.vbs
--- new check
Y:      \\S-PC\VB_scripts_help

-2147024843 0x80070035 WSHNetwork.MapNetworkDrive
The network path was not found.

--- new check
Y:      \\S-PC\VB_scripts_help

--- new check
Y:      \\S-PC\VB_scripts_help
X:      \\S-PC\test
copy here to \\S-PC\test

==>
上述输出对应于下一个操作:

  • 运行脚本
  • 第一个
    ——新检查
    找到
    Y:
    映射磁盘;然后调用的重试操作失败(未找到网络路径)
  • 第二次<代码>--重新检查
    找到
    Y:
    映射磁盘;然后手动映射磁盘
    X:
    ,然后调用忽略操作
  • 第三个
    ——新检查发现
    Y:
    X:
    映射磁盘
  • 退出循环时执行,脚本继续执行下一个操作
为完整起见,以下输出显示了调用的中止操作:


哇!你在那里做了很多工作,我现在不在电脑旁。我明天会把它浏览一遍,然后寄回来。不过看起来非常非常有希望。
==> cscript D:\VB_scripts\SO\37776762.vbs
37776762.vbs
--- new check
Y:      \\S-PC\VB_scripts_help

-2147024843 0x80070035 WSHNetwork.MapNetworkDrive
The network path was not found.

--- new check
Y:      \\S-PC\VB_scripts_help

--- new check
Y:      \\S-PC\VB_scripts_help
X:      \\S-PC\test
copy here to \\S-PC\test

==>
==> net use x: /delete
x: was deleted successfully.

==> cscript D:\VB_scripts\SO\37776762.vbs
37776762.vbs
--- new check
Y:      \\S-PC\VB_scripts_help

==>