首页 > 解决方案 > 获取磁盘信息并输出到一行

问题描述

我正在尝试读取存储驱动器上的信息,并希望将结果输出为(在 1 行):

1/2 - 三星 Evo - 500GB - 4 个分区 - C:, D:, E:, F:
2/2 - USB Transcend - 16GB - 2 个分区 - G :, H:
On Error Resume Next
' Create a FileSystemObject
Set FSO = CreateObject("Scripting.FileSystemObject")
' Provide file path
Dim result, strComputer, outFile, PropertyArr, ArrayItem
outFile = "C:\Users\MISS\Desktop\ok.txt"
' Sets computer name to the current computer name
strComputer = "."
' Setting up file to write
Set objFile = FSO.CreateTextFile(outFile, True)
' Connect to the WMI Service
Set CIMV2 = GetObject("winmgmts:" & "\\" & strComputer & "\root\CIMV2")
If Err Then
    WScript.StdOut.WriteLine "Unable to access WMI Service."
    WScript.Quit 32
End If
' Fetch all details from Win32_computersystem
Set Win32_DiskDrive = CIMV2.ExecQuery("Select * from Win32_DiskDrive")
PropertyArr = Array("Model","MediaType")
For Each item_PropertyArr In PropertyArr
    ArrayItem = item_PropertyArr
Next
For Each item In Win32_DiskDrive
    result = item.ArrayItem
    WScript.Echo "Result: " & result
Next

Set FSO = Nothing

结果是空的。

标签: vbscript

解决方案


要获得所需格式的字符串输出,我建议使用模板字符串并用于Replace()填写详细信息。

因为您还需要与每个分区关联的驱动器号,所以您需要做的不仅仅是查询Win32_DiskDrive,因为该查询不会返回驱动器号。看这里

下面的代码应该做你想做的事:

Option Explicit

Const ForAppending = 8
Dim objFso, objFile, objWMIService, colDiskDrives, objDiskDrive
Dim colPartitions, objDiskPartition, colLogicalDisks, objDriveLetters, objLogicalDisk
Dim outFile, strFormat, strResult, numCurrentDrive, strMediaType, strID, strQuery, strComputer

On Error Resume Next

' set up file to write
outFile = "C:\Users\MISS\Desktop\ok.txt"

Set objFso = CreateObject("Scripting.FileSystemObject")
If objFso.FileExists(outFile) Then objFso.DeleteFile outFile, True
Set objFile = objFso.OpenTextFile(outFile, ForAppending, True)

strComputer = "."
Set objWMIService = GetObject( "winmgmts:{ impersonationLevel=Impersonate }!//" & strComputer )
Set colDiskDrives = objWMIService.ExecQuery( "Select * FROM Win32_DiskDrive" )

'set up a string as template for the output
strFormat = "{0}/{1} - {2} - {3} - {4} partition(s)"
'create a variable for the current disk count
numCurrentDrive = 1
For Each objDiskDrive In colDiskDrives
    'start building the string to output
    strMediaType = objDiskDrive.MediaType
    If IsNull(strMediaType) Or Len(strMediaType) = 0 Then strMediaType = "Unknown"
    strResult = Replace(strFormat, "{0}", numCurrentDrive)
    strResult = Replace(strResult, "{1}", colDiskDrives.Count)
    strResult = Replace(strResult, "{2}", objDiskDrive.Model)
    strResult = Replace(strResult, "{3}", strMediaType)
    strResult = Replace(strResult, "{4}", objDiskDrive.Partitions)

    'increase the current drive counter
    numCurrentDrive = numCurrentDrive + 1
    'create an arraylist to capture the drive letters
    Set objDriveLetters = CreateObject("System.Collections.ArrayList")

    'escape the backslashes in objDiskDrive.DeviceID for the query
    strID = Replace( objDiskDrive.DeviceID, "\", "\\", 1, -1, vbTextCompare )
    strQuery = "Associators Of {Win32_DiskDrive.DeviceID=""" & strID & """} Where AssocClass = Win32_DiskDriveToDiskPartition"
    Set colPartitions = objWMIService.ExecQuery(strQuery)
    For Each objDiskPartition In colPartitions
        'get the drive letter for each partition
        strQuery = "Associators Of {Win32_DiskPartition.DeviceID=""" & objDiskPartition.DeviceID & """} Where AssocClass = Win32_LogicalDiskToPartition"
        Set colLogicalDisks = objWMIService.ExecQuery(strQuery)
        For Each objLogicalDisk In colLogicalDisks
            objDriveLetters.Add objLogicalDisk.DeviceID
            'objDriveLetters.Add objLogicalDisk.VolumeName
        Next

        Set colLogicalDisks = Nothing
    Next

    'add the driveletters to the output string
    strResult = strResult & " - " & Join(objDriveLetters.ToArray(), ", ")

    Set objDriveLetters = Nothing
    Set colPartitions = Nothing

    'output on screen
    WScript.Echo strResult
    'output to file
    objFile.WriteLine strResult
Next

'close the file
objFile.Close
Set objFile = Nothing
Set colDiskDrives = Nothing
Set objWMIService = Nothing


更新

根据您的评论,您不希望在代码中使用 .NET(ArrayList)。这当然可以做到,但需要更多的努力:

Option Explicit

Const ForAppending = 8
Dim objFso, objFile, objWMIService, colDiskDrives, objDiskDrive
Dim colPartitions, objDiskPartition, colLogicalDisks, objLogicalDisk
Dim outFile, strFormat, strResult, strMediaType, strID, strQuery, strComputer
Dim arrDriveLetters, numCurrentDrive, numDriveLetters

On Error Resume Next

' set up file to write
outFile = "C:\Users\MISS\Desktop\ok.txt"

Set objFso = CreateObject("Scripting.FileSystemObject")
If objFso.FileExists(outFile) Then objFso.DeleteFile outFile, True
Set objFile = objFso.OpenTextFile(outFile, ForAppending, True)

strComputer = "."
Set objWMIService = GetObject( "winmgmts:{ impersonationLevel=Impersonate }!//" & strComputer )
Set colDiskDrives = objWMIService.ExecQuery( "Select * FROM Win32_DiskDrive" )

'set up a string as template for the output
strFormat = "{0}/{1} - {2} - {3} - {4} partition(s)"
'create a variable for the current disk count
numCurrentDrive = 1
For Each objDiskDrive In colDiskDrives
    'start building the string to output
    strMediaType = objDiskDrive.MediaType
    If IsNull(strMediaType) Or Len(strMediaType) = 0 Then strMediaType = "Unknown"
    strResult = Replace(strFormat, "{0}", numCurrentDrive)
    strResult = Replace(strResult, "{1}", colDiskDrives.Count)
    strResult = Replace(strResult, "{2}", objDiskDrive.Model)
    strResult = Replace(strResult, "{3}", strMediaType)
    strResult = Replace(strResult, "{4}", objDiskDrive.Partitions)

    'increase the current drive counter
    numCurrentDrive = numCurrentDrive + 1
    'reset the dynamic array to capture the drive letters
    numDriveLetters = 0
    ReDim arrDriveLetters(numDriveLetters)

    'escape the backslashes in objDiskDrive.DeviceID for the query
    strID = Replace( objDiskDrive.DeviceID, "\", "\\", 1, -1, vbTextCompare )
    strQuery = "Associators Of {Win32_DiskDrive.DeviceID=""" & strID & """} Where AssocClass = Win32_DiskDriveToDiskPartition"
    Set colPartitions = objWMIService.ExecQuery(strQuery)
    For Each objDiskPartition In colPartitions
        'get the drive letter for each partition
        strQuery = "Associators Of {Win32_DiskPartition.DeviceID=""" & objDiskPartition.DeviceID & """} Where AssocClass = Win32_LogicalDiskToPartition"
        Set colLogicalDisks = objWMIService.ExecQuery(strQuery)
        For Each objLogicalDisk In colLogicalDisks
            ReDim Preserve arrDriveLetters(numDriveLetters)
            arrDriveLetters(numDriveLetters) = objLogicalDisk.DeviceID
            numDriveLetters = numDriveLetters + 1
        Next

        Set colLogicalDisks = Nothing
    Next

    'add the driveletters to the output string
    strResult = strResult & " - " & Join(arrDriveLetters, ", ")

    Erase arrDriveLetters
    Set colPartitions = Nothing

    'output on screen
    WScript.Echo strResult
    'output to file
    objFile.WriteLine strResult
Next

'close the file
objFile.Close
Set objFile = Nothing
Set colDiskDrives = Nothing
Set objWMIService = Nothing

输出将类似于

1/4 - Samsung SSD 750 EVO 250GB ATA Device - Fixed hard disk media - 1 partition(s) - C:
2/4 - ST3500418AS ATA Device - Fixed hard disk media - 1 partition(s) - E:
3/4 - WDC WD7501AALS-00J7B0 ATA Device - Fixed hard disk media - 1 partition(s) - D:
4/4 - Generic Ultra HS-SD/MMC USB Device - Unknown - 0 partition(s)

希望有帮助

PS 最好使用 CScript 而不是 WScript 运行,以避免一次出现一行弹出消息


推荐阅读