我的 vbs 脚本遇到一些问题。它只会添加 F 驱动器,而不会添加其后的 G 驱动器。我做错了什么?
'## This is for network drives
Set objNetwork = CreateObject("WScript.Network")
objNetwork.RemoveNetworkDrive "F:", True, True
'## for adding
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive "F:" , "\\myserver\share1"
objNetwork.MapNetworkDrive "G:" , "\\myserver\share2"
MapDrive.vbs
VBScript 将驱动器号映射到网络文件共享(非持久)。
该脚本的设计考虑的是可靠性高于速度,因此每次登录时都会重新连接。 它考虑了“记住的”连接,包括那些不再存在或脱机的文件共享的连接。 对于不总是连接到域的机器来说,这是一个很好的方法,例如笔记本电脑。
Windows XP 不会将“记住的”连接映射到其他服务器,除非您首先取消映射并忘记现有连接,即使旧连接路径当前已断开连接,这也适用。
对于每个驱动器号,有几种可能的状态,可能必须由脚本处理: - 记住(持久连接)/不记住 - 已连接/连接到错误的网络共享/未连接。
此脚本将在连接到正确的文件共享之前删除任何现有的驱动器映射。
' Map a network drive
' Usage
' cscript MapDrive.vbs drive fileshare //NoLogo
' cscript MapDrive.vbs H: \\MyServer\MyShare //NoLogo
'
' This script will remove any existing drive map to the same drive letter
' including persistent or remembered connections (Q303209)
Option Explicit
Dim objNetwork, objDrives, objReg, i
Dim strLocalDrive, strRemoteShare, strShareConnected, strMessage
Dim bolFoundExisting, bolFoundRemembered
Const HKCU = &H80000001
' Check both parameters have been passed
If WScript.Arguments.Count < 2 Then
wscript.echo "Usage: cscript MapDrive.vbs drive fileshare //NoLogo"
WScript.Quit(1)
End If
strLocalDrive = UCase(Left(WScript.Arguments.Item(0), 2))
strRemoteShare = WScript.Arguments.Item(1)
bolFoundExisting = False
' Check parameters passed make sense
If Right(strLocalDrive, 1) <> ":" OR Left(strRemoteShare, 2) <> "\\" Then
wscript.echo "Usage: cscript MapDrive.vbs drive fileshare //NoLogo"
WScript.Quit(1)
End If
wscript.echo " - Mapping: " + strLocalDrive + " to " + strRemoteShare
Set objNetwork = WScript.CreateObject("WScript.Network")
' Loop through the network drive connections and disconnect any that match strLocalDrive
Set objDrives = objNetwork.EnumNetworkDrives
If objDrives.Count > 0 Then
For i = 0 To objDrives.Count-1 Step 2
If objDrives.Item(i) = strLocalDrive Then
strShareConnected = objDrives.Item(i+1)
objNetwork.RemoveNetworkDrive strLocalDrive, True, True
i=objDrives.Count-1
bolFoundExisting = True
End If
Next
End If
' If there's a remembered location (persistent mapping) delete the associated HKCU registry key
If bolFoundExisting <> True Then
Set objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\default:StdRegProv")
objReg.GetStringValue HKCU, "Network\" & Left(strLocalDrive, 1), "RemotePath", strShareConnected
If strShareConnected <> "" Then
objReg.DeleteKey HKCU, "Network\" & Left(strLocalDrive, 1)
Set objReg = Nothing
bolFoundRemembered = True
End If
End If
'Now actually do the drive map (not persistent)
Err.Clear
On Error Resume Next
objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, False
'Error traps
If Err <> 0 Then
Select Case Err.Number
Case -2147023694
'Persistent connection so try a second time
On Error Goto 0
objNetwork.RemoveNetworkDrive strLocalDrive, True, True
objNetwork.MapNetworkDrive strLocalDrive, strRemoteShare, False
WScript.Echo "Second attempt to map drive " & strLocalDrive & " to " & strRemoteShare
Case Else
On Error GoTo 0
WScript.Echo " - ERROR: Failed to map drive " & strLocalDrive & " to " & strRemoteShare
End Select
Err.Clear
End If
Set objNetwork = Nothing
我以前这样做过:
dim objNet, strLocal, strPath, fso
Set fso = WScript.CreateObject("Scripting.FileSystemObject")
Set objNet = WScript.CreateObject("WScript.Network")
'Name the drives and their paths
strLocal = Array("H:","M:")
strPath = Array("\\Full\Path\Number1", _
\\Full\Path\Number2")
'Loop to check if they are mapped, map it if they are not
For i = LBound(strLocal) To UBound(strLocal)
If fso.FolderExists(strLocal(i)) = True Then
wscript.echo(strLocal(i) & " Mapped")
Else
objNet.MapNetworkDrive strLocal(i), strPath(i), False
wscript.echo(strLocal(i) & " Re-mapped")
End If
Next
'Wrap up the script
WScript.Echo("")
WScript.Echo("Mapping Completed")
WScript.Sleep(2000)
'Keep the command prompt open long enough to see that it is completed
Set fso=Nothing
Set objNet=Nothing
本质上,它会检查驱动器是否已映射,如果没有,则会对其进行映射。我将其添加到我的启动文件夹中,因为当我重新启动时,我的公司网络驱动器不断失去连接。