0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

vbsの作法 その80

Posted at

概要

vbsの作法、調べてみた。
vbs見つけたので、やってみた。

参考にしたページ

環境

windows11

サンプルコード

Option Explicit
Dim InfoStr
GetSystemInfo InfoStr
GetDrivesInfoByType InfoStr
WScript.Echo InfoStr

Function GetSystemInfo(ByRef Str)
	Dim oLocator
	Dim oService
	Dim oClassSet
	Dim oClass
	Dim objShell
	Dim strComputer
	Dim strValue
	Dim makerName
	Dim modelName
	Dim objRegistry
	Dim strKeyPath
	Dim strValueName
	const HKEY_LOCAL_MACHINE = &H80000002
	GetSystemInfo = False
	Set oLocator = Wscript.CreateObject("WbemScripting.SWbemLocator")
	Set oService = oLocator.ConnectServer
	Set oClassSet = oService.ExecQuery("Select * From Win32_ComputerSystem")
	For Each oClass In oClassSet
		makerName = oClass.Manufacturer
		modelName = oClass.Model
	Next
	strComputer = "."
	Set objShell = WScript.CreateObject("WScript.Shell")
	Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
	strKeyPath = "Software\Microsoft\Windows\CurrentVersion\OEMInformation"
	strValueName = "Manufacturer"
	objRegistry.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
	If Not IsNull(strValue) Then
		makerName = objShell.RegRead("HKEY_LOCAL_MACHINE\" & strKeyPath & "\" & strValueName)
	End If
	strValueName = "Model"
	objRegistry.GetStringValue HKEY_LOCAL_MACHINE, strKeyPath, strValueName, strValue
	If Not IsNull(strValue) Then
		modelName = objShell.RegRead("HKEY_LOCAL_MACHINE\" & strKeyPath & "\" & strValueName)
	End If
	Str = Str & "メーカー: " & makerName & vbCrLf & "モデル: " & modelName & vbCrLf & vbCrLf
	Set oLocator = Nothing
	Set oService = Nothing
	Set oClassSet = Nothing
	Set objShell = Nothing
	GetSystemInfo = True
End Function

Function GetDrivesInfoByType(ByRef Str)
	Dim strComputer
	Dim objWMIService
	Dim DiskDrives
	Dim DiskDrive
	Dim strDeviceID
	Dim Query
	Dim DiskPartitions
	Dim DiskPartition
	Dim LogicalDisks
	Dim LogicalDisk
	Dim colDiskDrives
	Dim objDiskDrive
	GetDrivesInfoByType = False
	strComputer = "."
	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Query = "SELECT * FROM Win32_DiskDrive"
	Set DiskDrives = objWMIService.ExecQuery(Query)
	For Each DiskDrive in DiskDrives
		strDeviceID = Replace(DiskDrive.DeviceID, "\", "\\")
		Query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & strDeviceID
		Query = Query & """} WHERE AssocClass=Win32_DiskDriveToDiskPartition"
		Set DiskPartitions = objWMIService.ExecQuery(Query)
		For Each DiskPartition in DiskPartitions
			Query = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & DiskPartition.DeviceID
			Query = Query & """} WHERE AssocClass=Win32_LogicalDiskToPartition"
			Set LogicalDisks = objWMIService.ExecQuery(Query)
			For Each LogicalDisk In LogicalDisks
				LinesAdd Str, "ドライブ:" & LogicalDisk.DeviceID
				Query = "Select * from Win32_DiskDrive WHERE DeviceID = '" & strDeviceID & "'"
				Set colDiskDrives = objWMIService.ExecQuery(Query)
				For Each objDiskDrive in colDiskDrives
					LinesAdd Str, "HDDの型番: " & objDiskDrive.InterfaceType
					LinesAdd Str, "モデル: " & objDiskDrive.Model
					LinesAdd Str, "サイズ: " & Int(objDiskDrive.Size / 1000000000) & " GB"
					LinesAdd Str, "状態: " & objDiskDrive.Status
					LinesAdd Str, ""
				Next
				GetDrivesInfoByType = True
			Next
			Set LogicalDisks = Nothing
		Next
		Set DiskPartitions = Nothing
		Exit For
	Next
	Set DiskDrives = Nothing
	Set objWMIService = Nothing
End Function

Sub LinesAdd(ByRef S, V)
	S = S & V & vbNewLine
End Sub





写真

image.png

以上。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?