2013-07-04

取得電腦安裝軟體清單及基本資訊 Software List

微軟 TechNet 網站可以找到 List Installed Software 的 VBS Script
可惜執行後只能取得 32位元的軟體列表

在 Dave's Daydreams 的 Listing installed applications on Vista 64-bit with WMI
找到了列出 64位元軟體的語法, 不過取得的資訊不夠詳細
於是將上述兩者融合, 並加入一些其他功能後產生出以下符合自己需求的程式
本 VBScript 程式可取得以下資料存成 .csv 檔

CPU資訊
RAM資訊
OS: 作業系統版本
Install Date: 作業系統安裝日期時間
ComputerName: 電腦名稱
UserName: 登入使用者名稱
IPAddress: 網卡 IPAddress
MACAddress: 網卡 MACAddress
32-bit Applications: 32位元軟體清單
64-bit Applications: 64位元軟體清單

2013.08.28 增加 CPU 資訊 (廠牌,型號,名稱等) / BaseBoard 資訊 (廠牌,型號) / RAM 資訊 (容量,Bank,數量)
2014.10.03 將 (Tab) 改為 vbTab, 修正從網頁上複製的 Code 執行後字元不一致的問題

===== 程式開始 =====

on error resume next

' 預設以電腦名稱為檔名
' 存檔位置不設定時, 將與 VBS 存於相同路徑下
FileSavePath="\\SharePC\Info$\Data"

'----------------------------------------------------------------------------------------------------

strComputer = "."
ThirtyTwoApps=""
SixtyFourApps=""
SystemInfoToWrite=""

Set fso=CreateObject("Scripting.FileSystemObject")

if (FileSavePath="") then
wscript.quit
' FileSavePath =  left(Wscript.ScriptFullName,len(Wscript.ScriptFullName)-len(Wscript.ScriptName))
end if

FileSaveFolderExists = fso.FolderExists(FileSavePath)

if (FileSaveFolderExists <> -1) then
wscript.quit
end if

Set wshShell = WScript.CreateObject( "WScript.Shell" )
ComputerName = wshShell.ExpandEnvironmentStrings( "%COMPUTERNAME%" )
UserName = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )

Const HKLM = &h80000002
Set objCtx = CreateObject("WbemScripting.SWbemNamedValueSet")

strComputer = createobject("wscript.network").computername
Set objService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set getOperatingSystem = objService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in getOperatingSystem
OSCaption = objOperatingSystem.Caption
OSServicePack = objOperatingSystem.CSDVersion
OSBuild = objOperatingSystem.BuildNumber
OSVersion = objOperatingSystem.Version
OSInstallDate = objOperatingSystem.InstallDate
Next
Set colSettings = objService.ExecQuery("SELECT * FROM Win32_Processor")
For Each objProcessor In colSettings
CPUPlatform = objProcessor.Architecture
CPUAddressWidth = objProcessor.AddressWidth
CPUManufacturer = objProcessor.Manufacturer
CPUName = objProcessor.Name
CPUDescription = objProcessor.Description
CPUProcessorID = objProcessor.ProcessorID
CPUNumberOfCores = objProcessor.NumberOfCores
CPUDataWidth = objProcessor.DataWidth
CPUFamily = objProcessor.Family
CPUMaximumClockSpeed = objProcessor.MaxClockSpeed
Next

Select Case CPUPlatform
Case 0
CPUPlatformTranslate = "x86"
Case 1
CPUPlatformTranslate = "MIPS"
Case 2
CPUPlatformTranslate = "Alpha"
Case 3
CPUPlatformTranslate = "PowerPC"
Case 5
CPUPlatformTranslate = "ARM"
Case 6
CPUPlatformTranslate = "Itanium-based systems"
Case 9
CPUPlatformTranslate = "x64"
Case Else
CPUPlatformTranslate = CPUPlatform
End Select

Set BaseBoardItems = objService.ExecQuery("SELECT * FROM Win32_BaseBoard")
For Each BaseBoardObjItem in BaseBoardItems  
BaseBoardManufacturer = BaseBoardObjItem.Manufacturer
BaseBoardProduct = BaseBoardObjItem.Product
Next

PhysicalMemoryCapacity=0
for each memory in objService.InstancesOf("Win32_PhysicalMemory")
counter  = counter + 1
PhysicalMemoryData = PhysicalMemoryData & memory.BankLabel & " - " &  CStr(memory.capacity)/1024/1024 & " - " &  memory.Description & " - " &  memory.DeviceLocator  & " - " &  memory.DataWidth  & " - " &  memory.FormFactor & vbCrLf
PhysicalMemoryCapacity = PhysicalMemoryCapacity + CStr(memory.capacity)/1024/1024
next

SystemInfoToWrite = SystemInfoToWrite & "File Create Time" & vbTab & Now & vbcrlf & vbcrlf

SystemInfoToWrite = SystemInfoToWrite & "CPU" & vbcrlf
SystemInfoToWrite = SystemInfoToWrite & "Manufacturer" & vbTab & CPUManufacturer & vbcrlf
SystemInfoToWrite = SystemInfoToWrite & "Name" & vbTab & CPUName & vbcrlf
SystemInfoToWrite = SystemInfoToWrite & "Platform" & vbTab & CPUPlatformTranslate & vbcrlf
SystemInfoToWrite = SystemInfoToWrite & "Description" & vbTab & CPUDescription & vbcrlf
SystemInfoToWrite = SystemInfoToWrite & "ProcessorID" & vbTab & CPUProcessorID & vbcrlf
SystemInfoToWrite = SystemInfoToWrite & "NumberOfCores" & vbTab & CPUNumberOfCores & vbcrlf
SystemInfoToWrite = SystemInfoToWrite & "Address Width" & vbTab & CPUAddressWidth & vbcrlf
SystemInfoToWrite = SystemInfoToWrite & "DataWidth" & vbTab & CPUDataWidth & vbcrlf
SystemInfoToWrite = SystemInfoToWrite & "Family" & vbTab & CPUFamily & vbcrlf
SystemInfoToWrite = SystemInfoToWrite & "MaximumClockSpeed" & vbTab & CPUMaximumClockSpeed & vbcrlf & vbcrlf

SystemInfoToWrite = SystemInfoToWrite & "Base Board" & vbcrlf
SystemInfoToWrite = SystemInfoToWrite & "Manufacturer" & vbTab & BaseBoardManufacturer & vbcrlf
SystemInfoToWrite = SystemInfoToWrite & "Product" & vbTab & BaseBoardProduct & vbcrlf & vbcrlf

SystemInfoToWrite = SystemInfoToWrite & "RAM" & vbcrlf
SystemInfoToWrite = SystemInfoToWrite & "Physical Memory Capacity" & vbTab & PhysicalMemoryCapacity & vbcrlf
SystemInfoToWrite =  SystemInfoToWrite &  "Bank Label - Capacity - Description - DeviceLocator - DataWidth - FormFactor" & vbCrLf
SystemInfoToWrite =  SystemInfoToWrite &  PhysicalMemoryData & vbcrlf

SystemInfoToWrite = SystemInfoToWrite & "System" & vbcrlf
SystemInfoToWrite = SystemInfoToWrite & "OS" & vbTab & OSCaption & " build " & OSBuild & " (" & OSVersion & ") " & OSServicePack & vbcrlf
SystemInfoToWrite = SystemInfoToWrite & "Install Date" & vbTab & OSInstallDate & vbcrlf
SystemInfoToWrite = SystemInfoToWrite & "ComputerName" & vbTab & ComputerName & vbcrlf
SystemInfoToWrite = SystemInfoToWrite & "UserName" & vbTab & UserName & vbcrlf & vbcrlf

SystemInfoToWrite = SystemInfoToWrite & "Network" & vbcrlf
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")

For Each objItem in colItems
For Each IP in objItem.IPAddress
SystemInfoToWrite = SystemInfoToWrite & "IPAddress" & vbTab & IP & vbcrlf
Next
SystemInfoToWrite = SystemInfoToWrite & "MACAddress" & vbTab & objItem.MACAddress & vbcrlf
Next
SystemInfoToWrite = SystemInfoToWrite & vbcrlf

ThirtyTwoApps=GetSoftwareList(32)
SixtyFourApps=GetSoftwareList(64)

Set StdOut = fso.OpenTextFile(FileSavePath & "\" & ComputerName & ".csv", 2, true, -1)

StdOut.WriteLine SystemInfoToWrite
StdOut.WriteLine ThirtyTwoApps
StdOut.WriteLine SixtyFourApps

StdOut.close

Function GetSoftwareList(SystemBit)

objCtx.Add "__ProviderArchitecture", SystemBit
objCtx.Add "__RequiredArchitecture", TRUE
Set objLocator = CreateObject("Wbemscripting.SWbemLocator")
Set objServices = objLocator.ConnectServer("","root\default","","",,,,objCtx)
Set objStdRegProv = objServices.Get("StdRegProv")


ReturnValue = SystemBit & "-bit Applications" & vbcrlf & vbcrlf
ReturnValue = ReturnValue & "RegKey" & vbTab & "DisplayName" & vbTab & "DisplayVersion" & vbTab & "Publisher" & vbTab & "QuietDisplayName" & vbTab & "InstallDate" & vbTab & "InstallSource" & vbcrlf

Set Inparams = objStdRegProv.Methods_("EnumKey").Inparameters
Inparams.Hdefkey = HKLM
Inparams.Ssubkeyname = "Software\Microsoft\Windows\CurrentVersion\Uninstall\"

set Outparams = objStdRegProv.ExecMethod_("EnumKey", Inparams,,objCtx)

For Each strSubKey In Outparams.snames

Set Inparams = objStdRegProv.Methods_("GetStringValue").Inparameters
Inparams.Hdefkey = HKLM
Inparams.Ssubkeyname = "Software\Microsoft\Windows\CurrentVersion\Uninstall\" & strSubKey

Inparams.Svaluename = "DisplayName"
set Outparams = objStdRegProv.ExecMethod_("GetStringValue", Inparams,,objCtx)
strDisplayName = Outparams.SValue

If strDisplayName = "" Then
strDisplayName = strSubKey
End If

Inparams.Svaluename = "DisplayVersion"
set Outparams = objStdRegProv.ExecMethod_("GetStringValue", Inparams,,objCtx)
strDisplayVersion = Outparams.SValue

Inparams.Svaluename = "Publisher"
set Outparams = objStdRegProv.ExecMethod_("GetStringValue", Inparams,,objCtx)
strPublisher = Outparams.SValue

Inparams.Svaluename = "QuietDisplayName"
set Outparams = objStdRegProv.ExecMethod_("GetStringValue", Inparams,,objCtx)
strQuietDisplayName = Outparams.SValue

Inparams.Svaluename = "InstallDate"
set Outparams = objStdRegProv.ExecMethod_("GetStringValue", Inparams,,objCtx)
strInstallDate = Outparams.SValue

Inparams.Svaluename = "InstallSource"
set Outparams = objStdRegProv.ExecMethod_("GetStringValue", Inparams,,objCtx)
str = Outparams.SValue

If strDisplayName <> "" Then
ReturnValue=ReturnValue & strSubKey & vbTab & strDisplayName & vbTab & strDisplayVersion & vbTab & strPublisher & vbTab & strQuietDisplayName & vbTab & strInstallDate & vbTab & strInstallSource & vbcrlf
End If
Next

GetSoftwareList=ReturnValue

End Function

4 則留言:

匿名 提到...

想問一下,
我在 32bits OS 上,竟然會抓到 64bits 的軟體列表...
在 Win 7/8.1 的家用版上.
為什麼會這樣,是正確的嗎?

Dino9021 提到...

問微軟...
看一下原始碼, 這些資訊都是從 WMI 來的
反正重點是到底裝了哪些程式

grissom lin 提到...

請問我能否將文章貼到我自己FB備忘
因為之前發生過有某常參考的vbscript網站停站了
一堆好用的程式碼範例都沒了
謝謝

Dino9021 提到...

任意取用吧, 取之於網路回饋於網路
可以參考今天新增的幾個 Script 是我後來增修的, 比較完整
資料可以寫入資料庫以利查詢