我正在尝试连接到Windows共享(\ print),但如果我的身份验证失败,例如,用户名或密码错误,我该如何捕获该错误? 问题是,当执行脚本(exe。文件)时,如果我插入错误的凭据,错误消息来自Autoit程序,而不是我想要弹出的消息(上面的代码),如果验证有问题。
以下是我的代码示例:
在这里,我尝试使用用户名和密码连接到服务器\ print
$WshNetwork = ObjCreate("WScript.Network")
$WshNetwork.MapNetworkDrive("", "\\print", True, $username, $password)
$ username和$ password的值从Inputbox填充。 所以,在那段代码之后我试图抓住这样的错误:
if @error Then
MsgBox(16, "", "Authentication error!")
EndIf
顺便说一下,我从控制台得到的错误就是这个:
C:\Users\user\Desktop\test.au3 (33) : ==> The requested action with this object has failed.:
$WshNetwork.MapNetworkDrive( "","\\print",True, $username, $password)
$WshNetwork.MapNetworkDrive( "","\\print",True, $username, $password)^ ERROR
嗯,错误正在发生,但有没有办法从代码中捕获错误并发送警报(Msgbox)?
答案 0 :(得分:1)
查看AutoIt中的DriveMapAdd(...)
。或尝试使用以下内容:
$USERNAME = InputBox(...)
$PASSWORD = InputBox(...)
$DRIVEINPUTBOX = InputBox(...)
$SHARE = "\\print\foldername"
$DRIVE = $DRIVEINPUTBOX & ": "
$COMMAND = "NET USE " & $DRIVE & " " & $SHARE & " /USER:" & $USERNAME & " " & $PASSWORD & " > TEMP"
RunWait(@ComSpec & " /c " & $COMMAND)
; Read the file "TEMP"
; Choose what to do
; Delete file "TEMP"