我是第一次创建 vbs 脚本来映射 Windows 中的网络驱动器。由于某种原因,当用户登录到域时,脚本会运行,但不会映射任何网络驱动器。这是我正在使用的脚本。它非常简单明了。
Option Explicit
Dim wshNetwork
Set wshNetwork = CreateObject("WScript.Network")
wshNetwork.MapNetworkDrive "S:","\\server\shared"
wshNetwork.MapNetworkDrive "U:","\\server\" & wshNetwork.UserName
WScript.Quit
我究竟做错了什么?
答案1
尝试:
wshNetwork.MapNetworkDrive "S:","\\server\shared", True
wshNetwork.MapNetworkDrive "U:","\\server\" & wshNetwork.UserName, True
在映射驱动器之前,我还添加了一个例程来删除所有共享,以避免出现“设备已在使用中”的错误。
wshNetwork.RemoveNetworkDrive "S:", True, True
wshNetwork.RemoveNetworkDrive "U:", True, True
wscript.sleep 300
答案2
这是我一直在使用的一个功能:
Function MapDrive(ByVal strDrive, ByVal strShare)
' Function to map network share to a drive letter.
' If the drive letter specified is already in use, the function
' attempts to remove the network connection.
' objFSO is the File System Object, with global scope.
' objNetwork is the Network object, with global scope.
' Returns True if drive mapped, False otherwise.
Dim objDrive
On Error Resume Next
If (objFSO.DriveExists(strDrive) = True) Then
Set objDrive = objFSO.GetDrive(strDrive)
If (Err.Number <> 0) Then
On Error GoTo 0
MapDrive = False
Exit Function
End If
If (objDrive.DriveType = 3) Then
objNetwork.RemoveNetworkDrive strDrive, True, True
Else
MapDrive = False
Exit Function
End If
Set objDrive = Nothing
End If
objNetwork.MapNetworkDrive strDrive, strShare
If (Err.Number = 0) Then
MapDrive = True
Else
Err.Clear
MapDrive = False
End If
On Error GoTo 0
End Function
使用示例:
If (MapDrive("Z:", "\\yourserver\yourshare") = False) Then
' Do something because there was an error.
End If