获取磁盘信息并输出到一行

时间:2019-05-26 01:24:47

标签: vbscript

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

1/2 - Samsung Evo - 500GB - 4 partitions - C :, D :, E :, F:
2/2 - USB Transcend - 16GB - 2 partitions - 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

结果为空。

1 个答案:

答案 0 :(得分:0)

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

由于还需要与每个分区关联的驱动器盘,您需要做的不仅仅是查询Win32_DiskDrive,因为该查询不会返回驱动器盘。参见here

下面的代码应做您想要的:

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)

希望有帮助

P.S。最好使用CScript而不是WScript来运行,以避免一次弹出消息仅一行一行