映射网络驱动器并检查其在 VBScript 中的存在 [英] Mapping a network drive and checking for its existence in VBScript

查看:31
本文介绍了映射网络驱动器并检查其在 VBScript 中的存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 VBScript 将网络驱动器映射到网络路径.网络路径直接从输入中读取.如何映射网络驱动器以及如何检查输入的网络路径是否已经存在?

I need to map a network drive into a network path using VBScript. The network path is read directly from input. How should I map the network drive and how to check whether the entered network path already exists?

推荐答案

我创建了一个子程序来映射驱动器...

I created a subroutine to map drives...

    MapDrive "H:","\\server\share"

    Sub MapDrive(letter, uncpath)
         on error Resume Next
         dim drivetype, currentmapping

        dim objWMIService 
        dim colDisks, objDisk

        'Set wshnetwork = CreateObject("Wscript.Network")
        Set objWMIService = GetObject("winmgmts:" & _
             "{impersonationLevel=impersonate}!\\.\root\cimv2")
        Set colDisks = objWMIService.ExecQuery _
            ("Select * from Win32_LogicalDisk Where Name = """ & letter & """")
        For Each objDisk In colDisks        
             drivetype = objDisk.DriveType      
            currentmapping = objDisk.ProviderName
        Next    


        if (drivetype <> 4 and drivetype <> 0) then
            NotifyUser ucase(letter) & " cannot be mapped due to a physical device already reserving that drive letter" & vbcrlf & _
                        "This is most frequently caused by a thumbdrive or external disk.",5
            exit Function
        end if

        if (ucase(currentmapping) = ucase(uncpath)) then
            exit function
        end If

        if (drivemappings.Exists(uncpath)) then
            drivemappings.Add uncpath & "(" & letter & ")", letter
        else 
            drivemappings.Add uncpath, letter
        end if

        if (currentmapping <> "") then
                wshnetwork.RemoveNetworkDrive letter,,True
        end if

        wshnetwork.MapNetworkDrive letter, uncpath, true

        on Error goto 0
     End Sub

我把它留给你处理错误检查等.或者,如果你更喜欢 net use 路由,你可以做类似的事情..

I leave it up to you do handle error checking etc. Alternatively if you prefer the net use route you could do something like..

dim wshShell
Set wshShell = CreateObject("WScript.Shell")
wshshell.run "cmd /c net use H: ""\\server\share""",1,True

您可以更进一步,使用示例脚本专家创建.

You can take it a step further to automagically use the next available drive letter to map drives using an example The Scripting Guys created.

Set objDictionary = CreateObject("Scripting.Dictionary")

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk")

For Each objDisk in colDisks
    objDictionary.Add objDisk.DeviceID, objDisk.DeviceID
Next

For i = 67 to 90
    strDrive = Chr(i) & ":"
    If objDictionary.Exists(strDrive) Then
    Else
        Wscript.Echo strDrive & " is the next available drive letter."
        Wscript.Quit
    End If
Next
Wscript.Echo "There are no available drive letters on this computer."

我希望这会有所帮助.

这篇关于映射网络驱动器并检查其在 VBScript 中的存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆