WMI:MSVM_Imagemanagmentservice挂载方法不分配驱动器号

时间:2014-02-03 07:57:44

标签: wmi

我试图在Windows 2012 hyperV核心数据中心版本上安装vhd。用于挂载vhd我使用MSVM_Imagemanagementservice类和mount方法。磁盘已安装,但问题是没有分配驱动器号。我已经在其他2012数据中心版和2012年核心上尝试过它也可以正常工作。即挂载后,驱动器号被分配。

对此的任何想法都会有所帮助。

1 个答案:

答案 0 :(得分:0)

您可以查询驱动器号列表,然后根据您的要求分配驱动器号。下面给出了获取驱动器号列表的功能。

Function GetDriveLetterList(path)

    'Where path is path of VHD

    Dim mountedImage, diskDevice, diskPartition, diskPartitions, logicalPartition, timeout, query
    Dim logicalPartitionList

    timeout = 0
    driveLetterList = ""

    path = Replace(path,"\","\\")

    Set wmiServiceCIM = GetObject("winmgmts:\\.\root\cimv2")  

    Do
        WScript.Sleep(3000)
        timeout = timeout + 3

        Set mountedImage = (wmiService.ExecQuery("SELECT * FROM Msvm_MountedStorageImage WHERE Name='" & path & "'")).ItemIndex(0)

        query = "SELECT * From Win32_DiskDrive WHERE Model='Msft Virtual Disk SCSI Disk Device' and SCSITargetId='" & mountedImage.TargetId & "' and SCSILogicalUnit='" & mountedImage.Lun & "' and SCSIPort='" & mountedImage.PortNumber & "'"
        Set diskDevice = wmiServiceCIM.ExecQuery(query).ItemIndex(0)

        query = "ASSOCIATORS OF {" & diskDevice.Path_.Path & "} where AssocClass=Win32_DiskDriveToDiskPartition"

        Set diskPartitions = wmiServiceCIM.ExecQuery(query)

        For Each diskPartition In diskPartitions

            query = "ASSOCIATORS OF {" & diskPartition.Path_.Path & "} WHERE AssocClass=Win32_LogicalDiskToPartition"
            Set logicalPartitionList = wmiServiceCIM.ExecQuery(query)
            If logicalPartitionList.count > 0 Then
                Set logicalPartition = logicalPartitionList.ItemIndex(0)
                'WScript.Echo logicalPartition.DeviceID
                driveLetterList = driveLetterList + logicalPartition.DeviceID
            End If

        Next
End Function

如果要检查系统驱动器。然后你可以这样做......

folderPath = driveLetter + ":\Windows\System32"

Set fileSys = CreateObject("Scripting.FileSystemObject")
If filesys.FolderExists(folderPath) Then
    //doStuff
End If