Windowsでのスペック収集バッチ例

単発小ネタ　Windowsバッチ

# 20240929_Windowsでのスペック収集バッチ例  
  
## 機能  
- 以下二つのスクリプトを同じ階層に配置します。
- batを管理者権限で起動すると、色々と調べてログに書きます。
- 例での収集項目はsysteminfo, serial, ip, win32, powercfg, winsat。

```get_sysinfo_v001.bat
@echo off

REM 端末基本情報の書き出しツール

echo.
echo ######################################################################
echo.
echo 情報収集バッチです。実行しますか？　＃一部管理者権限が必要です。
echo 中止する場合はCtl+C
echo 実行する場合は
pause

echo.
echo 実行します

echo.
echo ************ 処理開始 ************
echo.

set CDIR=%~dp0

set CDATE=%date:~-10,4%-%date:~-5,2%-%date:~-2,2%
set CTIME=%time: =0%
set CTIME=%CTIME:~0,2%-%CTIME:~3,2%-%CTIME:~6,2%

rem ログフォルダ設定
set logPath=%CDIR%log_%CDATE%_%CTIME%\

mkdir %logPath% 2> nul 1>&2
echo %logPath%

echo.
echo ************ いろいろ調べてます ************
echo.
rem chk systeminfo, serial, ip, win32
cd /d %CDIR%
systeminfo > %logPath%\sysinf.txt
wmic csproduct get identifyingnumber > %logPath%\serial.txt
ipconfig /all > %logPath%\ipinfo1.txt
netsh interface ip show addresses > %logPath%\ipinfo2.txt
netsh interface ip show ipaddresses > %logPath%\ipinfo3.txt
start /wait cscript get_win32_info_v001.vbs "%logPath%"

rem chk power
echo. >> %logPath%\powerlog.txt
echo *** start *** >> %logPath%\powerlog.txt
echo. >> %logPath%\powerlog.txt
POWERCFG /BATTERYREPORT /OUTPUT "%logPath%\batteryreport.html" >> %logPath%\powerlog.txt
echo. >> %logPath%\powerlog.txt
echo **** >> %logPath%\powerlog.txt
echo. >> %logPath%\powerlog.txt
POWERCFG /ENERGY /OUTPUT "%logPath%\longtrace.html" /DURATION 20 >> %logPath%\powerlog.txt
echo. >> %logPath%\powerlog.txt
echo *** end *** >> %logPath%\powerlog.txt
echo. >> %logPath%\powerlog.txt

rem chk winsat
if exist "C:\Windows\Performance\WinSAT\DataStore\%CDATE%*Formal*" goto WINSAT_ALREADY
start /wait winsat formal -restart clean
:WINSAT_ALREADY
copy C:\Windows\Performance\WinSAT\DataStore\%CDATE%*Formal* %logPath%

echo.
echo ************ 処理終了 ************
echo.

echo 10秒後に閉じます
ping 127.0.0.1 -n 10 2> nul 1>&2
rem explorer %TDIR%
exit

```

```get_win32_info_v001.vbs

' Option Explicit
On Error Resume Next

Dim wshShell
Set wshShell = WScript.CreateObject("WScript.Shell")

Dim objFSO
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")

Dim objNetwork
Set objNetwork = WScript.CreateObject("WScript.Network")

Dim Path
Path = Replace(WScript.ScriptFullName,WScript.ScriptName,"")

'引数の取得
Dim oParam
Set oParam = WScript.Arguments

Dim logFile

'■ログ記録開始
Dim objTSW
logFile = "BasicInfo_" & GetLogDateTime() & ".log"
Set objTSW = objFSO.OpenTextFile(oParam(0) & logFile,2,True)
If Not objFSO.FileExists(oParam(0) & logFile) Then
	'ログファイルが作成できていなければ処理終了
	WScript.Echo "ログファイル作成に失敗しました"

	Set wshShell = Nothing
	Set objFSO = Nothing
	Set objNetwork = Nothing
	
	WSCript.Quit(0)
End If

LogOutput(" ")
LogOutput("□端末情報取得開始")

'■端末情報取得

If ( DeterDevice() = 1 ) Then

	LogOutput("■端末情報取得に失敗しました")

	Set wshShell = Nothing
	Set objFSO = Nothing
	Set objNetwork = Nothing

	WSCript.Quit(0)

End if

LogOutput(" ")
LogOutput("□端末情報取得終了")

Set wshShell = Nothing
Set objFSO = Nothing
Set objNetwork = Nothing

WScript.Quit(0)

'■Terminated

Function LogOutput(strLog)

	objTSW.WriteLine(strLog)

End Function

Function GetLogDateTime()
	Dim sDateTime
	
	sDateTime = Year(Now())
	sDateTime = sDateTime & Right( "0" & Month(Now()), 2)
	sDateTime = sDateTime & Right( "0" & Day(Now()), 2)
	sDateTime = sDateTime & Right( "0" & Hour(Now()), 2)
	sDateTime = sDateTime & Right( "0" & Minute(Now()), 2)
	sDateTime = sDateTime & Right( "0" & Second(Now()), 2)

	GetLogDateTime = sDateTime

End Function

Function DeterDevice()

	DeterDevice = 0

'■ BaseBoard
LogOutput(" ")
LogOutput("□BaseBoard")
Set BBSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery( _
"SELECT Manufacturer FROM Win32_BaseBoard")
For Each obj In BBSet
    LogOutput("Win32_BaseBoard.Manufacturer = " & obj.Manufacturer)
Next

'■ Processor
LogOutput(" ")
LogOutput("□Processor")
Set PRSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery( _
"SELECT MaxClockSpeed,Name,NumberOfCores,NumberOfLogicalProcessors FROM Win32_Processor")
For Each obj In PRSet
    LogOutput("Win32_Processor.MaxClockSpeed = " & obj.MaxClockSpeed)
    LogOutput("Win32_Processor.Name = " & obj.Name)
    LogOutput("Win32_Processor.NumberOfCores = " & obj.NumberOfCores)
    LogOutput("Win32_Processor.NumberOfLogicalProcessors = " & obj.NumberOfLogicalProcessors)
Next

'■ BIOS
LogOutput(" ")
LogOutput("□BIOS")
Set BISet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery( _
"SELECT Caption,SerialNumber FROM Win32_BIOS")
For Each obj In BISet
    LogOutput("Win32_BIOS.Caption = " & obj.Caption)
    LogOutput("Win32_BIOS.SerialNumber = " & obj.SerialNumber)
Next

'■ PhysicalMemory
LogOutput(" ")
LogOutput("□PhysicalMemory")
Set PMSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery( _
"SELECT Capacity FROM Win32_PhysicalMemory")
For Each obj In PMSet
    LogOutput("Win32_PhysicalMemory.Capacity = " & obj.Capacity)
Next

'■ CDROMDrive
LogOutput(" ")
LogOutput("□CDROMDrive")
Set CDSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery( _
"SELECT Caption FROM Win32_CDROMDrive")
For Each obj In CDSet
    LogOutput("Win32_CDROMDrive.Caption = " & obj.Caption)
Next

'■ DiskDrive
LogOutput(" ")
LogOutput("□DiskDrive")
Set DDSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery( _
"SELECT Caption,Size FROM Win32_DiskDrive")
For Each obj In DDSet
    LogOutput("Win32_DiskDrive.Caption = " & obj.Caption)
    LogOutput("Win32_DiskDrive.Size = " & obj.Size)
Next

'■ LogicalDisk
LogOutput(" ")
LogOutput("□LogicalDisk")
Set LDSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery( _
"SELECT Caption,FreeSpace,Size FROM Win32_LogicalDisk")
For Each obj In LDSet
    LogOutput("Win32_LogicalDisk.Caption = " & obj.Caption)
    LogOutput("Win32_LogicalDisk.FreeSpace = " & obj.FreeSpace)
    LogOutput("Win32_LogicalDisk.Size = " & obj.Size)
Next

'■ VideoController
LogOutput(" ")
LogOutput("□VideoController")
Set VCSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery( _
"SELECT Caption,VideoModeDescription FROM Win32_VideoController")
For Each obj In VCSet
    LogOutput("Win32_VideoController.Caption = " & obj.Caption)
    LogOutput("Win32_VideoController.VideoModeDescription = " & obj.VideoModeDescription)
Next

'■ DesktopMonitor
LogOutput(" ")
LogOutput("□DesktopMonitor")
Set DMSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery( _
"SELECT MonitorManufacturer FROM Win32_DesktopMonitor")
For Each obj In DMSet
    LogOutput("Win32_DesktopMonitor.MonitorManufacturer = " & obj.MonitorManufacturer)
Next

'■ NetworkAdapter
LogOutput(" ")
LogOutput("□NetworkAdapter")
Set NASet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery( _
"SELECT Description,MACAddress FROM Win32_NetworkAdapter")
For Each obj In NASet
    LogOutput("Win32_NetworkAdapter.Description = " & obj.Description)
    LogOutput("Win32_NetworkAdapter.MACAddress = " & obj.MACAddress)
Next

'■ OperatingSystem
LogOutput(" ")
LogOutput("□OperatingSystem")
Set OSSet = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery( _
"SELECT Caption,CSDVersion,SerialNumber FROM Win32_OperatingSystem")
For Each obj In OSSet
    LogOutput("Win32_OperatingSystem.Caption = " & obj.Caption)
    LogOutput("Win32_OperatingSystem.CSDVersion = " & obj.CSDVersion)
    LogOutput("Win32_OperatingSystem.SerialNumber = " & obj.SerialNumber)
Next

'■ S.M.A.R.T
LogOutput(" ")
LogOutput("□S.M.A.R.T")
Dim oClassSet
Dim oClass
Dim oLocator
Dim oService
Dim sMesStr
Dim i

'■ローカルコンピュータに接続する。
Set oLocator = WScript.CreateObject("WbemScripting.SWbemLocator")
Set oService = oLocator.ConnectServer(, "Root\WMI")
'クエリー条件をWQLにて指定する。
Set oClassSet = oService.ExecQuery("Select * From MSStorageDriver_FailurePredictData")

'■コレクションを解析する。
For Each oClass In oClassSet

sMesStr = sMesStr & "デバイス名：" & oClass.InstanceName & vbCrLf

For i = 1 To UBound(oClass.VendorSpecific)

Select Case i

Case 1
sMesStr = sMesStr & "Raw Read Error Rate: " & oClass.VendorSpecific(i) & VbCrLf
Case 3
sMesStr = sMesStr & "Spin Up Time: " & oClass.VendorSpecific(i) & VbCrLf
Case 9
sMesStr = sMesStr & "Power-On Hours: " & oClass.VendorSpecific(i) & VbCrLf
Case 10
sMesStr = sMesStr & "Spin Retry Count: " & oClass.VendorSpecific(i) & VbCrLf
Case 194
sMesStr = sMesStr & "Temperature: " & oClass.VendorSpecific(i) & VbCrLf

End Select
Next

Next
LogOutput("HDD/SSDのS.M.A.R.T.情報です。" & vbCrLf & vbCrLf & sMesStr)
'MsgBox ("HDD/SSDのS.M.A.R.T.情報です。" & vbCrLf & vbCrLf & sMesStr)

'■後片付け
Set oClassSet = Nothing
Set oClass = Nothing
Set oService = Nothing
Set oLocator = Nothing

End Function

```

## その他
- 確認したい項目にあわせて改変してみてください
- reg queryを使えばレジストリの確認も可能です
- batからvbs呼び出す練習だったりもしました
- スクリプトにしなくても、コマンドラインでも簡単な情報収集は可能です
- wmicはもう非推奨ということで、PowerShell利用しないとかもです
```
wmic CPU get Name
wmic memorychip get Capacity
wmic diskdrive get Model
ver
```
以上