VBS でコンピュータ情報を取得する 2014/10/23


VBS でコンピュータ情報を取得する
参考サイト
WMI に興味がある方・システム管理者は必見です!

vbsでコンピュータ名を表示する

Option Explicit

'WMIにて使用する各種オブジェクトを定義・生成する。
Dim oClassSet
Dim oClass
Dim oLocator
Dim oService
Dim sMesStr

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

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

sMesStr = sMesStr & "コンピュータ名:" & oClass.CSName & vbCrLf & _
"コンピュータの説明:" & oClass.Description & vbCrLf & vbCrLf

Next

vbsでコンピュータの物理メモリを表示する

Option Explicit

Option Explicit

'WMIにて使用する各種オブジェクトを定義・生成する。
Dim oClassSet
Dim oClass
Dim oLocator
Dim oService
Dim lRet

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

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

lRet = oClass.TotalPhysicalMemory

Next

MsgBox "物理メモリ容量は、" & CStr(lRet) & "Byteです。"

'使用した各種オブジェクトを後片付けする。
Set oClassSet = Nothing
Set oClass = Nothing
Set oService = Nothing
Set oLocator = Nothing

vbsでコンピュータのメモリ情報をバンク別に表示する

Option Explicit

'WMIにて使用する各種オブジェクトを定義・生成する。
Dim oClassSet
Dim oClass
Dim oLocator
Dim oService
Dim sMesStr

'ローカルコンピュータに接続する。
Set oLocator = WScript.CreateObject("WbemScripting.SWbemLocator")
Set oService = oLocator.ConnectServer
'クエリー条件をWQLにて指定する。
Set oClassSet = oService.ExecQuery("Select * From Win32_PhysicalMemory")
'コレクションを解析する。
For Each oClass In oClassSet

sMesStr = sMesStr & "バンク名:" & oClass.BankLabel & vbCrLf & _
"容量:" & oClass.Capacity & vbCrLf & vbCrLf

Next

MsgBox "搭載されている物理メモリに関する情報です。" & vbCrLf & vbCrLf & sMesStr

'使用した各種オブジェクトを後片付けする。
Set oClassSet = Nothing
Set oClass = Nothing
Set oService = Nothing
Set oLocator = Nothing


vbsでIPアドレスNICの情報を表示する

Option Explicit

'WMIにて使用する各種オブジェクトを定義・生成する。
Dim oClassSet
Dim oClass
Dim oLocator
Dim oService
Dim sMesStr

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

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

If oClass.IPEnabled = True Then

sMesStr = sMesStr & "種類:" & oClass.Description & vbCrLf & _
"IPアドレス:" & oClass.IPAddress(0) & vbCrLf & _
"サブネットマスク:" & oClass.IPSubnet(0) & vbCrLf & _
"DHCPの状態:" & CStr(oClass.DHCPEnabled) & vbCrLf & vbCrLf

End If

Next

MsgBox "NICに関する情報です。" & vbCrLf & vbCrLf & sMesStr

'使用した各種オブジェクトを後片付けする。
Set oClassSet = Nothing
Set oClass = Nothing
Set oService = Nothing
Set oLocator = Nothing

CPUに関する情報を表示する

Option Explicit

'WMIにて使用する各種オブジェクトを定義・生成する。
Dim oClassSet
Dim oClass
Dim oLocator
Dim oService
Dim sMesStr

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

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

sMesStr = sMesStr & "種類:" & oClass.Description & vbCrLf & _
"名前:" & oClass.Name & vbCrLf & _
"製造元:" & oClass.Manufacturer & vbCrLf & "コア数 :" & oClass.NumberOfCores & vbCrLf & _
"現在の周波数:" & CStr(oClass.CurrentClockSpeed) & vbCrLf & _
"最大周波数:" & CStr(oClass.MaxClockSpeed) & vbCrLf & _
"L2キャッシュサイズ:" & CStr(oClass.L2CacheSize) & vbCrLf & vbCrLf

Next

MsgBox("Processorに関する情報です。" & vbCrLf & vbCrLf & sMesStr)v
'使用した各種オブジェクトを後片付けする。
Set oClassSet = Nothing
Set oClass = Nothing
Set oService = Nothing
Set oLocator = Nothing


コンピュータにに関する情報をとりあえず表示する

Option Explicit

'WMIにて使用する各種オブジェクトを定義・生成する。
Dim oClassSet
Dim oClass
Dim oLocator
Dim oService
Dim sMesStr
Dim Pcname
Dim Oss
Dim Bitt

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

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

Pcname = oClass.CSName
Oss = oClass.Caption
Spp = oClass.ServicePackMajorVersion
Bitt = oClass.OSArchitecture

'sMesStr = sMesStr & "" & oClass.CSName & _
'"" & oClass.Description & vbCrLf & vbCrLf
'Set Pcname = oClass.CSName

Next

'MsgBox "" & vbCrLf & vbCrLf & sMesStr

'使用した各種オブジェクトを後片付けする。
Set oClassSet = Nothing
Set oClass = Nothing
Set oService = Nothing
Set oLocator = Nothing

'WMIにて使用する各種オブジェクトを定義・生成する。
Dim lRet

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

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

lRet =round (( oClass.TotalPhysicalMemory) / 1024/1024,0)
Next

'sMesStr = sMesStr & "" & oClass.CSName & vbCrLf & _
'"コンピュータの説明:" & oClass.Description & vbCrLf & vbCrLf
'MsgBox "Memory :" & CStr(lRet) & "MBeです。"

'使用した各種オブジェクトを後片付けする。
Set oClassSet = Nothing
Set oClass = Nothing
Set oService = Nothing
Set oLocator = Nothing

'WMIにて使用する各種オブジェクトを定義・生成する。
Dim Ipadd
Dim Spp

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

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

If oClass.IPEnabled = True Then
Ipadd = oClass.IPAddress(0)

sMesStr = CStr(Pcname) & " [ " & Ipadd & " ] " & vbCrLf & CStr(Oss) & ":" & _
CStr(Bitt) & vbCrLf & "SP:" & CStr(Spp) & vbCrLf & "Memory :"& CStr(lRet) & "MB" & vbCrLf &
vbCrLf
'"IPアドレス:" & oClass.IPAddress(0) & vbCrLf & _
'"サブネットマスク:" & oClass.IPSubnet(0) & vbCrLf & _
'"DHCPの状態:" & CStr(oClass.DHCPEnabled) & vbCrLf & vbCrLf

End If

Next

MsgBox "" & vbCrLf & vbCrLf & sMesStr

'使用した各種オブジェクトを後片付けする。
Set oClassSet = Nothing
Set oClass = Nothing
Set oService = Nothing
Set oLocator = Nothing

vbsでディスク容量をチェックする

下記のサイトを参考にさせていただいております。
続・Windows標準機能だけで、ディスクの残容量を監視する

Option Explicit

'WMIにて使用する各種オブジェクトを定義・生成する。
Dim oClassSet
Dim oClass
Dim oLocator
Dim oService
Dim sMesStr
Dim CPUInfo
Dim CPUCore
Dim Sendto
Dim UsageThreshold

'通知先
Sendto = "hogataro.kimura@hoge.com"
'ディスク使用容量しきい値 使用領域設定:単位(%)
UsageThreshold = 80


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

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

CPUInfo = oClass.Name
CPUCore = oClass.NumberOfCores

'sMesStr = sMesStr & "種類:" & oClass.Description & vbCrLf & _
'"名前:" & oClass.Name & vbCrLf & _
'"製造元:" & oClass.Manufacturer & vbCrLf & "コア数 :" & oClass.NumberOfCores & vbCrLf & _
'"現在の周波数:" & CStr(oClass.CurrentClockSpeed) & vbCrLf & _
'"最大周波数:" & CStr(oClass.MaxClockSpeed) & vbCrLf & _
'"L2キャッシュサイズ:" & CStr(oClass.L2CacheSize) & vbCrLf & vbCrLf

sMesStr = sMesStr & "" & vbCrLf & _
"CPU名前:" & oClass.Name & vbCrLf & "コア数 :" & oClass.NumberOfCores & vbCrLf & _
vbCrLf


Next

'MsgBox("Processorに関する情報です。" & vbCrLf & vbCrLf & sMesStr)

'使用した各種オブジェクトを後片付けする。
Set oClassSet = Nothing
Set oClass = Nothing
Set oService = Nothing
Set oLocator = Nothing


'###########################

'Dim oClassSet
'Dim oClass
'Dim oLocator
'Dim oService
'Dim sMesStr
Dim Pcname
Dim Oss
Dim Bitt

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



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

Pcname = oClass.CSName
Oss = oClass.Caption
Spp = oClass.ServicePackMajorVersion
Bitt = oClass.OSArchitecture

'sMesStr = sMesStr & "" & oClass.CSName & _
'"" & oClass.Description & vbCrLf & vbCrLf
'Set Pcname = oClass.CSName


Next

'MsgBox "" & vbCrLf & vbCrLf & sMesStr

'使用した各種オブジェクトを後片付けする。
Set oClassSet = Nothing
Set oClass = Nothing
Set oService = Nothing
Set oLocator = Nothing



'WMIにて使用する各種オブジェクトを定義・生成する。

Dim lRet

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

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

lRet =round (( oClass.TotalPhysicalMemory) / 1024/1024,0)

Next

'sMesStr = sMesStr & "" & oClass.CSName & vbCrLf & _
'"コンピュータの説明:" & oClass.Description & vbCrLf & vbCrLf
'MsgBox "Memory :" & CStr(lRet) & "MBeです。"

'使用した各種オブジェクトを後片付けする。
Set oClassSet = Nothing
Set oClass = Nothing
Set oService = Nothing
Set oLocator = Nothing


'WMIにて使用する各種オブジェクトを定義・生成する。
Dim Ipadd
Dim Spp
Dim Bbody
'ローカルコンピュータに接続する。
Set oLocator = WScript.CreateObject("WbemScripting.SWbemLocator")
Set oService = oLocator.ConnectServer
'クエリー条件をWQLにて指定する。
Set oClassSet = oService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration")

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

If oClass.IPEnabled = True Then
Ipadd = oClass.IPAddress(0)


body = CStr(Pcname) & " [" & Ipadd & "] " & vbCrLf & CStr(Oss) & " " & _
CStr(Bitt) & vbCrLf & "SP:" & CStr(Spp) & vbCrLf & "Memory :"& CStr(lRet) & "MB" & vbCrLf & vbCrLf
'"IPアドレス:" & oClass.IPAddress(0) & vbCrLf & _
'"サブネットマスク:" & oClass.IPSubnet(0) & vbCrLf & _
'"DHCPの状態:" & CStr(oClass.DHCPEnabled) & vbCrLf & vbCrLf

End If

Next

'MsgBox "" & vbCrLf & vbCrLf & sMesStr

'使用した各種オブジェクトを後片付けする。
Set oClassSet = Nothing
Set oClass = Nothing
Set oService = Nothing
Set oLocator = Nothing




'##############################################

'Option Explicit

Dim WarningCount

Dim MailSubject
Dim Body
Dim MailBody
Dim SmtpHost
Dim SmtpPort
Dim SmtpFrom

Dim net



'変数値set
Set net = CreateObject("WScript.Network")
Pcname = net.ComputerName
SmtpFrom = Pcname & "@hoge.com" & "<" & Pcname & "@hoge.com>"
'メールあて先

SmtpHost = "smtp.hoge.com"
'smtp auth or ssl の場合は以下必須
'SmtpSSL = True
'smtp port番号
SmtpPort = 25
'SmtpUser = "servernotice@hoge.com"
'SmtpPassword = "from_at_example_com_password"

MailSubject = "[" & Pcname & " Disk Warning ] ディスク容量監視警告 (" & UsageThreshold & "% 超過): " & Pcname
WarningCount = 0

'***
'ローカルコンピュータに接続する。
Set oLocator = Wscript.CreateObject("WbemScripting.SWbemLocator")

Set oService = oLocator.ConnectServer
'クエリー条件をWQLにて指定する。
Set oClassSet = oService.ExecQuery("Select * From Win32_OperatingSystem")
'コレクションを解析する。
For Each oClass In oClassSet
sMesStr = sMesStr & "OS:" & CStr(oClass.Caption) & CStr(Bitt) & vbCrLf _
& "SP(R) : SP(R)" & CStr(oClass.ServicePackMajorVersion) & vbCrLf _
& "Build:" & CStr(oClass.Version) & vbCrLf _
& "Memory :" & CStr(lRet) & "MB" & vbCrLf

Next




Dim objWMIService
Dim objIpSet
Dim objConfig
Dim objMessage
Dim Bill
Dim UsageSpace

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Dim objSWbemObjectCollection
Set objSWbemObjectCollection = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = 3")
Dim objSWbemObject
Mailbody = Mailbody & Pcname & " [" & CStr(Ipadd) & "]" & vbCrLf _
& sMesStr & vbCrLf & "ディスク監視閾値を超えました。ディスク使用閾値:"& UsageThreshold & "% 以上" & vbCrLf
For Each objSWbemObject in objSWbemObjectCollection
Dim DeviceID
Dim Size
Dim FreeSpace
Dim ExistPersent
Dim Caution
DeviceID = objSWbemObject.DeviceID
Size = objSWbemObject.Size
FreeSpace = objSWbemObject.FreeSpace
UsageSpace = Size - FreeSpace
ExistPersent = 100 - FreeSpace / Size * 100
If ( ExistPersent > CDbl(UsageThreshold) ) Then
Caution = "CAUTION!!!"
WarningCount = WarningCount + 1
Else
Caution = ""
End If

MailBody = MailBody & DeviceID & vbTab & "空き" & round((FreeSpace / 1024 / 1024 / 1024),1) _
& "GB" & vbTab & "全体" & round((Size / 1024 / 1024 / 1024),1) & "GB" _
& "" & vbTab & "利用" & round((UsageSpace / 1024 / 1024 / 1024),1) & "GB" & vbTab _
& "使用域" & round((ExistPersent),1) & "%" & vbTab & Caution & vbCrLf
Next

Mailbody = Mailbody & vbCrLf & "" & vbCrLf & "---" & Pcname & " Disk Usage Check cron mail" & vbCrLf

If ( WarningCount > 0 ) Then
Call SendMail(SmtpFrom, Sendto, MailSubject , MailBody )
End IF

Sub SendMail(FromMailAddress, ToMailAddress, Subject, Body )
Dim objConfig
Set objConfig = CreateObject("CDO.Configuration")
objConfig.Fields.Item( "http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
' objConfig.Fields.Item( "http://schemas.microsoft.com/cdo/configuration/smtpusessl") = SmtpSSL
objConfig.Fields.Item( "http://schemas.microsoft.com/cdo/configuration/smtpserver") = SmtpHost
objConfig.Fields.Item( "http://schemas.microsoft.com/cdo/configuration/smtpserverport") = SmtpPort

'ユーザー認証
' objConfig.Fields.Item( "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
' objConfig.Fields.Item( "http://schemas.microsoft.com/cdo/configuration/sendusername") = SmtpUser
' objConfig.Fields.Item( "http://schemas.microsoft.com/cdo/configuration/sendpassword") = SmtpPassword

objConfig.Fields.Update

Dim objMessage
Set objMessage = CreateObject("CDO.Message")
Set objMessage.Configuration = objConfig

objMessage.Send

End Sub

Set WarningCount =Nothing
Set Pcname =Nothing
Set MailSubject =Nothing
Set MailBody =Nothing
Set UsageThreshold =Nothing
Set SmtpHost =Nothing
Set SmtpPort =Nothing
Set SmtpFrom =Nothing
Set Sendto =Nothing
Set net =Nothing
Set oLocator =Nothing
Set oService =Nothing
Set oClassSet =Nothing
Set oClass =Nothing
Set sMesStr =Nothing

Set oLocator = Nothing
Set objWMIService = Nothing
Set objSWbemObjectCollection = Nothing
Set DeviceID = Nothing
Set Size = Nothing
Set FreeSpace = Nothing
Set ExistPersent = Nothing
Set Caution = Nothing
Set MailBody = Nothing
Set oClassSet =Nothing
Set oService = Nothing
Set oClassSet = Nothing
Set Pcname = Nothing
Set Oss = Nothing
Set Bitt = Nothing
Set Bill = Nothing
Set Ipadd = Nothing
Set Spp = Nothing
Set lRet = Nothing