gpt4 book ai didi

vbscript - VBScript MapNetworkDrive错误处理

转载 作者:行者123 更新时间:2023-12-03 07:49:47 28 4
gpt4 key购买 nike

我试图仅用VBScript编写一个小的脚本供家庭使用,该脚本在Macrium Reflect中进行预定备份之前运行。

我被困在一个看似很小的问题上,这是在物理断开网络驱动器(即未连接电缆)时的错误处理。

此时,脚本将检查是否已连接驱动器,如果尚未连接驱动器,则会显示一条消息,提示用户连接电缆,然后按YES。

现在,一切正常,用户可以按要求连接电缆,然后按YES按钮,但是我想了解在连接驱动器电缆之前按YES的时间。

在代码中,有一个'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

任何帮助,将不胜感激

最佳答案

Err Object (VBScript)引用没有提供有用的指南。您需要为每个run-time error容易采取的措施分别捕获错误或成功。
通用规则(best practice):禁止通过On Error GoTo 0禁用错误处理,并仅对可疑操作启用它。

例如,有很多原因导致 MapNetworkDrive method失败(服务器脱机,用户被阻止,密码错误/更改等):

Sub MapDRV
Dim errResult
Set objNetwork = WScript.CreateObject("WScript.Network")
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"
End If
End Sub

整个脚本如下所示:
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

==>

以上输出对应于以下操作:
  • 运行脚本
  • 第一个--- new check找到Y:映射磁盘;然后调用的“重试”操作失败(找不到网络路径);
  • 2nd --- new check再次找到Y:映射磁盘;然后手动映射磁盘X:,然后调用“忽略”操作;
  • 找到第三个--- new checkY:X:映射磁盘;
  • Do While循环已退出,脚本继续进行下一个操作。

  • 为了完整起见,以下输出显示了调用的Abort操作:
    ==> 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

    ==>

    关于vbscript - VBScript MapNetworkDrive错误处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37776762/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com