使用Array映射网络驱动器

时间:2012-05-17 14:56:38

标签: arrays vbscript for-loop wsh

问题在线(objNetwork.MapNetworkDrive CStr(colDrives.Item(i)),CStr(newMappingArray(j)),TRUE),我的脚本实际上尝试重新连接网络驱动器。它失败了,因为它没有将它检测为字符串?

我试图在路径和驱动器号周围使用相同的代码“”并且它有效,所以我不知道从哪里开始..

代码说明 它需要一个共享名称列表,检查是否有任何远程路径映射到驱动器。如果他们是,那么它通过他们。取消映射它们,使用相同的驱动器号并使用新的路径引用重新映射它们(当前已登录凭据..

下面的代码

On Error Resume Next

''''''''''''''''''''''''''''' ARRAY SETUP '''''''''''''''''''''''''''''
DIM oldMappingArray(5)
oldMappingArray(0) = "\\DDCSHRNASP02\u_amr_project"
oldMappingArray(1) = "\\DDCSHRNASP02\U_AMS_Deployment"
oldMappingArray(2) = "\\DDCSHRNASP02\u_corp_share"
oldMappingArray(3) = "\\DDCSHRNASP02\u_dcwb_control_room"
oldMappingArray(4) = "\\DDCSHRNASP02\u_dis"
oldMappingArray(5) = "\\DDCSHRNASP02\u_dis15"

oldMappingArrayLength = UBound(oldMappingArray)


DIM newMappingArray(5)
newMappingArray(0) = "\\EUCPRDNAS02\u_amr_project"
newMappingArray(1) = "\\EUCPRDNAS02\U_AMS_Deployment"
newMappingArray(2) = "\\EUCPRDNAS02\u_corp_share"
newMappingArray(3) = "\\EUCPRDNAS02\u_dcwb_control_room"
newMappingArray(4) = "\\EUCPRDNAS02\u_dis"
newMappingArray(5) = "\\EUCPRDNAS02\u_dis15"

newMappingArrayLength = UBound(newMappingArray)


''''''''''''''''''''''''''''' WMI SETUP '''''''''''''''''''''''''''''
Set objNetwork = CreateObject("Wscript.Network")
Set colDrives = objNetwork.EnumNetworkDrives
colDrivesLength = colDrives.count
wscript.echo colDrivesLength

''''''''''''''''''''''''''''' MAIN LOOP '''''''''''''''''''''''''''''
For i=0 to colDrivesLength-1 Step 2
    'Gets every other in the array, making i equal to a PATH
wscript.echo i
For j=0 to oldMappingArrayLength
    'count through all of the old path names
    wscript.echo j
    wscript.echo "Drive Letter = " + colDrives.Item(i) 'Drive letter
    wscript.echo "Current Path = " + colDrives.Item(i+1) 'Path of the Drive Above

    wscript.echo "Tested Path = " + oldMappingArray(j) 'Path Currently Testing

    if colDrives.Item(i+1) = oldMappingArray(j) then
        'If the drive path from enumeration is equal to the old drive mapping array...
        wscript.echo "Item being added -> " + colDrives.Item(i)
        wscript.echo "to be mapped ---> " + newMappingArray(j)

        objNetwork.RemoveNetworkDrive colDrives.Item(i), True, True
        WScript.sleep 3000 'Wait 1.0 seconds
        objNetwork.MapNetworkDrive CStr(colDrives.Item(i)), CStr(newMappingArray(j)), TRUE
        exit For 'ends the oldMappingArrayLength loop if a match was found
    End If
Next
Next

Set objNetwork = Nothing

1 个答案:

答案 0 :(得分:0)

没有测试出来,这对于映射驱动器来说似乎是一个耗时且复杂的过程,所以我不会用它来打破我的头,但是wscript.echo“要映射--->”+ newMappingArray( j)必须是罪魁祸首。 +在VbScript上没有连接,但是&是

所以无论你在哪里使用它都应该成为&

您可以使用以下方式构建数组来缩短脚本

oldMappingArray = ("u_amr_project", "U_AMS_Deployment", ...)

然后如果你需要使用整个字符串

"\\DDCSHRNASP02\" & oldMappingArray(j)