VBS枚举硬盘信息

1、GetHDInfo.vbs
这个脚本,是Google后找到的,虽然是VBS,但比网上绝大多数的代码,靠谱的多得多

'---------------------------------------------------
'Get the properties of all partitions on all drives, 
'including USB drives.
'Core code from the Scripting Guy
'7.1.2012 FNL
'---------------------------------------------------
Set oWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set cDiskDrives = oWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive")

For Each oDrive In cDiskDrives
	WScript.echo "Disk #" & oDrive.Index & "(" & oDrive.InterfaceType & "): " _
		& oDrive.Caption & ", Size=" & Format(oDrive.Size, False) & " MBytes"
	WScript.echo "Part.  Drive          F/S   Size(MBytes)  Free(MBytes)  Active  Primary"
	WScript.echo String(71, "-")

	'WScript.Echo "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & Replace(oDrive.DeviceID, "\", "\\") & """} WHERE AssocClass = " & "Win32_DiskDriveToDiskPartition"

	Set cPartitions = oWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" _
		& Replace(oDrive.DeviceID, "\", "\\") & """} WHERE AssocClass = " & "Win32_DiskDriveToDiskPartition")
 
	For Each oPartition In cPartitions
		aPartition = Split(oPartition.DeviceID)
		Set cLogicalDisks = oWMIService.ExecQuery _
	 		("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & oPartition.DeviceID _
			& """} WHERE AssocClass = Win32_LogicalDiskToPartition")
 		if oPartition.Bootable         then sActive  = "Yes" Else sActive  = "No "
 		if oPartition.PrimaryPartition then sPrimary = "Yes" Else sPrimary = "No "
		For Each oLogicalDisk In cLogicalDisks
			For Each oVolume In cLogicalDisks
				sLabel = Left(oVolume.VolumeName & Space(12), 12)
			Next
			With oLogicalDisk
				sSpacer = Space(8 - Len(.FileSystem))
	 	   		WScript.Echo " " & aPartition(3) & "    " & .DeviceID & " " & sLabel _
				& .FileSystem & sSpacer & Format(.Size, True) & "       " _
				& Format(.FreeSpace, True) & "     " & sActive & "     " & sPrimary
			End With
		Next
	Next
	WScript.Echo
Next

Function Format(n, bPad)
	n = FormatNumber(n/1000000, 0, -1, 0, -1)
	if bPad then Format = Space(7-Len(n)) & n Else Format = n
End Function

2、运行

@REM do not use wscript GetHDInfo.vbs
@cscript GetHDInfo.vbs

Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.

Disk #1(IDE): Hitachi HTS545032B9A302 ATA Device, Size=320,070 MBytes
Part.  Drive          F/S   Size(MBytes)  Free(MBytes)  Active  Primary
-----------------------------------------------------------------------
 #1    D: WinAPP      NTFS    319,862       182,455     No      Yes

Disk #0(IDE): Samsung SSD 850 EVO 500GB ATA Device, Size=500,105 MBytes
Part.  Drive          F/S   Size(MBytes)  Free(MBytes)  Active  Primary
-----------------------------------------------------------------------
 #1    E: MacSSD      HFS     256,114        89,747     No      Yes
 #3    C: BOOTCAMP    NTFS    242,999        45,934     Yes     Yes

Press any key to continue . . .

Leave a Reply

Your email address will not be published. Required fields are marked *

*