Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to detect installed software and versions through vbs scripts

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how to use vbs scripts to detect installed software and versions. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

'/

'FileName: SoftwareMeteringCLS.vbs

'/

If (WScript.ScriptName = "SoftwareMeteringCLS.vbs") Then Call demo_SoftwareMeteringCLS ()

'=

Function getSoftwareList (sHost)

'Callable by * .wsf; will return list (safe array) of installed

'software on the sHost system (sHost is ComputerName or IP address).

'

'The assumption is that sHost is available and has WMI installed.

Set oSoftMeter = new SoftwareMeteringCLS

SProgsAry = oSoftMeter.getList (sHost)

Set oSpftMeter = Nothing

GetSoftwareList = sProgsAry

End Function

'= = CLASS = =

Class SoftwareMeteringCLS

'Author: Branimir Petrovic

'Date: 6 Sept 2002

'Version: 1.0.3

'

'Revision History:

'30 March 2002 V 1.0.0

'

'08 April 2002 V 1.0.1

'Added error handling-if the target system is not present

'or does not have WMI, getList (sHost) will return empty list.

'

'Added global function getSoftwareList (sHost) to be used

'from *. Wsf scripts when caller script is JScript (since

'JScript can not instantiate VBS classes directly).

'

'21 April 2002 V 1.0.2

'Replacing "[" with "(" and "]" with ")" in "DisplayName"

'Some strings like: [See Q311401 for more information]

'can cause troubles, therefore replacement.

'

'6 Sept 2002 V 1.0.3

'Win2K's SP3 for Windows 2000 introduced slight (but silent)

'' improvement' in a way registry provder's EnumValues method

'deals with empty keys. EnumValues method called against

'keys without any values (except the Default, empty value)

'will now return Null value (previously array of size 0 was)

'returned). Added (previously unneeded) type checking...

'

'

'Dependancies:

'WSH 5.6

'

'Methods:

'- getClassName ()

'- getVersion ()

'- getList (sHost) sHost parameter can be computer name or IP address

'Enumerates all subkeys in:

'"Software\ Microsoft\ Windows\ CurrentVersion\ Uninstall"

'Returns array of strings, each string item containing:

'"DisplayNameKeyValue [--Version: DisplayVersionKeyValue]"

'

'If sHost parameter is empty string or non-string value

'function returns list of installed software on this host.

'Otherwise it will connect to host pointed to by sHost string

'(provided sufficient level of permissions)

'

'- getHostString () Returns name of the system or IP address

'- Private data members

Private HKLM 'Points to HKEY_LOCAL_MACHINE hive

Private UNINSTALL_ROOT 'Software\ Microsoft\ Windows\ CurrentVersion\ Uninstall

Private SUPRESS_HOTFIX_ENTRIES'By default is TRUE (set in Class_Initialize)

'(supressess listing of installed hotfixes)

Private CLASS_NAME

Private VERSION

Private REG_SZ

Private oReg

Private sComputerName

'- Public

Public Function getClassName ()

GetClassName = CLASS_NAME

End Function

Public Function getVersion ()

GetVersion = VERSION

End Function

Public Function getList (sHost)

If TypeName (sHost) = "String" AND sHost "" Then

SComputerName = sHost

Else

SComputerName = WScript.CreateObject ("WScript.Network") .ComputerName

End If

On Error Resume Next

Set oReg = GetObject ("winmgmts: {impersonationLevel=impersonate} / /" & _

SComputerName & "/ root/default:StdRegProv")

If Err.Number0 Then

'Computer is not accessable or does not have WMI, return empty array

GetList = Array ()

Else

'Computer is on the network and does have working WMI

'return the list (safe array) of installed software

GetList = listInstalledProgs (oReg)

End If

On Error GoTo 0

End Function

Public Function getHostString ()

GetHostString = sComputerName

End Function

'- Private helper routines

Private Sub Class_Initialize

'Initialize various values used by this class

HKLM = & H80000002 'Hive: HKEY_LOCAL_MACHINE

UNINSTALL_ROOT = "Software\ Microsoft\ Windows\ CurrentVersion\ Uninstall"

REG_SZ = 1

SUPRESS_HOTFIX_ENTRIES = true

CLASS_NAME = "SoftwareMeteringCLS"

VERSION = "1.0.3"

End Sub

Private Function listInstalledProgs (oReg)

'returns array of strings DisplayName & "& DisplayVersion

Dim oRegX, nCnt, sSubKeysAry, sProgName

Dim sProgsAry (): ReDim sProgsAry (1)

SSubKeysAry = getKeys (oReg, HKLM, UNINSTALL_ROOT)

If SUPRESS_HOTFIX_ENTRIES Then

'Supress looking into all hot fix related sub keys (like Q252795, etc...)

Set oRegX = new RegExp

ORegX.Pattern = "^ Q\ Dai $" 'will detect patterns like: Q252795

ORegX.IgnoreCase = true

For nCnt = 0 To UBound (sSubKeysAry)

If NOT oRegX.Test (sSubKeysAry (nCnt)) Then

SProgName = getProgNameAndVersion (oReg, HKLM, _

UNINSTALL_ROOT & "\" & sSubKeysAry (nCnt))

If NOT (IsEmpty (sProgName) OR sProgName= "") Then

If NOT IsEmpty (sProgsAry (UBound (sProgsAry)-1)) Then

ReDim Preserve sProgsAry (UBound (sProgsAry) + 1)

End If

SProgsAry (UBound (sProgsAry)-1) = sProgName

End If

End If

Next

Else

'List all sub keys including hotfix related ones (like Q252795, etc...)

For nCnt = 0 To UBound (sSubKeysAry)

SProgName = getProgNameAndVersion (oReg, HKLM, _

UNINSTALL_ROOT & "\" & sSubKeysAry (nCnt))

If NOT (IsEmpty (sProgName) OR sProgName= "") Then

If NOT IsEmpty (sProgsAry (UBound (sProgsAry)-1)) Then

ReDim Preserve sProgsAry (UBound (sProgsAry) + 1)

End If

SProgsAry (UBound (sProgsAry)-1) = sProgName

End If

Next

End If

ListInstalledProgs = sProgsAry

End Function

Private Function getKeys (oReg, HIVE, sKeyRoot)

'Returns array of strings of subkey names

Dim vKeysAry

Call oReg.EnumKey (HIVE, sKeyRoot, vKeysAry)

GetKeys = vKeysAry'>

End Function

Private Function getProgNameAndVersion (oReg, HIVE, sKeyRoot)

'If both values "DisplayName" and "DisplayVersion" exist in sKeyRoot, return:

'"DisplayNameKeyValue-- Version: DisplayVersionKeyValue"

'

'If only "DisplayName" exists, return:

'' DisplayNameKeyValue''

'

'Otherwise EMPTY is returned

Dim sKeyValuesAry, iKeyTypesAry, nCnt, sValue, sDisplayName, sDisplayVersion

OReg.EnumValues HIVE, sKeyRoot, sKeyValuesAry, iKeyTypesAry 'fill the arrays

'6 Sept 2002

'SP3 for Win2K altered behavior of registry provider's EnumValues method!

'EnumValues method after SP3 does not return empty array any more for all

'those registry keys that have only empty Default value.

'Therefore sKeyValuesAry must be tested to see if it is an array or not.

If NOT IsArray (sKeyValuesAry) Then

Exit Function'>

End If

For nCnt = 0 To UBound (sKeyValuesAry)

If InStr (1, sKeyValuesAry (nCnt), "DisplayName", vbTextCompare) Then

If iKeyTypesAry (nCnt) = REG_SZ Then

OReg.GetStringValue HIVE, sKeyRoot, sKeyValuesAry (nCnt), sValue

If sValue "" Then

SDisplayName = sValue

SDisplayName = Replace (sDisplayName, "[," (")

SDisplayName = Replace (sDisplayName, "]", ")")

End If

End If

ElseIf InStr (1, sKeyValuesAry (nCnt), "DisplayVersion", vbTextCompare) Then

If iKeyTypesAry (nCnt) = REG_SZ Then

OReg.GetStringValue HIVE, sKeyRoot, sKeyValuesAry (nCnt), sValue

If sValue "" Then sDisplayVersion = sValue

End If

End If

If (sDisplayName "") AND (sDisplayVersion "") Then

GetProgNameAndVersion = sDisplayName & "--Version:" & sDisplayVersion

Exit Function'>

End If

Next

If sDisplayName "" Then

GetProgNameAndVersion = sDisplayName

Exit Function'>

End If

End Function

End Class

'= = END OF CLASS = =

Function demo_SoftwareMeteringCLS ()

Dim oSoftMeter, sProgsAry, sComputer

'sComputer = "W-BRANIMIR-666"

'sComputer = "W-Branimir-079"

SComputer = "" 'query local host

SProgsAry = getSoftwareList (sComputer)

Call WScript.Echo (Join (sProgsAry, vbCrLf))

End Function

Save the above code as a vbs file and execute it.

Thank you for reading! This is the end of the article on "how to detect installed software and versions through vbs scripts". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report