updated sofware_installed.vbs to avoid badxml on execution interruption

This commit is contained in:
fbsanchez 2015-09-08 10:42:48 +02:00
parent 3e98a1c455
commit 2c62e6970b

View File

@ -2,9 +2,8 @@
' (c) 2015 Sancho Lerena <slerena@artica.es> ' (c) 2015 Sancho Lerena <slerena@artica.es>
' (c) 2015 Borja Sanchez <fborja.sanchez@artica.es> ' (c) 2015 Borja Sanchez <fborja.sanchez@artica.es>
' This plugin extends agent inventory feature. Only enterprise version ' This plugin extends agent inventory feature. Only enterprise version
' Warning: If the system has the WMI corrupted, call this script with nowmi argument ' --------------------------------------------------------------------------
' ------------------------------------------------------------------------------------ 'on error resume next
on error resume next
Class ObjectList Class ObjectList
Public List Public List
@ -32,63 +31,58 @@ Class ObjectList
End Class End Class
class AppClass class AppClass
dim InstallDate,Caption,Version,Vendor dim InstallDate,Caption,Version,Vendor
end class end class
' Print the XML structure
Wscript.StdOut.WriteLine "<inventory>"
Wscript.StdOut.WriteLine "<inventory_module>"
Wscript.StdOut.WriteLine "<name>Software</name>"
Wscript.StdOut.WriteLine "<type><![CDATA[generic_data_string]]></type>"
Wscript.StdOut.WriteLine "<datalist>"
'------ Checks if an item exists on the main collection '------ Checks if an item exists on the main collection
function isItemInArray(objeto,coleccion) function isItemInArray(objeto,coleccion)
for each id in coleccion.List for each id in coleccion.List
if (strComp(objeto,coleccion.List(id).caption) = 0) then if (strComp(objeto,coleccion.List(id).caption) = 0) then
isItemInArray=true isItemInArray=true
exit function exit function
end if end if
next next
isItemInArray=false isItemInArray=false
end function end function
'------ main collection definition '------ main collection definition
dim colObjSW : set colObjSW = new ObjectList dim colObjSW : set colObjSW = new ObjectList
strComputer = "." strComputer = "."
' Disable by arguments WMI queries - corrupted WMI host '------ Retrieve the WMI registers first
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
If (not WScript.Arguments(0) = "nowmi") Then Set colSoftware = objWMIService.ExecQuery ("SELECT installstate,caption,installdate,Version,vendor FROM Win32_Product")
'------ Retrieve the WMI registers first
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSoftware = objWMIService.ExecQuery ("SELECT installstate,caption,installdate,Version,vendor FROM Win32_Product",,48)
'------ Check all '------ Check all
'-- first) add all unique WMI (unique) entries to main collector '-- first) add all unique WMI (unique) entries to main collector
'-- second) add all unique REGISTRY items to main collector '-- second) add all unique REGISTRY items to main collector
for each objSoftware in colSoftware on error resume next
if ( objSoftware.installstate = 5 ) then flag = colSoftware.Count
if ( isItemInArray(objSoftware.caption, colObjSW) = false ) then If (err.number <> 0) Then
' It doesn't exists, added. flag = true
With colObjSW.Append(New AppClass) Else
.caption = objSoftware.caption flag = false
.InstallDate = objSoftware.InstallDate
.version = objSoftware.version
.vendor = objSoftware.vendor
End with
' Add to XML the verified ones
Wscript.StdOut.WriteLine "<data><![CDATA[" _
& objSoftware.caption & ";" _
& objSoftware.version _
& "]]></data>"
end if
end if
next
End If End If
on error goto 0
If (NOT flag) Then
for each objSoftware in colSoftware
if ( objSoftware.installstate = 5 ) then
if ( isItemInArray(objSoftware.caption, colObjSW) = false ) then
' It doesn't exists, added.
With colObjSW.Append(New AppClass)
.caption = objSoftware.caption
.InstallDate = objSoftware.InstallDate
.version = objSoftware.version
.vendor = objSoftware.vendor
End with
' Add to XML the verified ones
end if
end if
next
End If
' ------ Getting the REGISTRY ' ------ Getting the REGISTRY
Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE Const HKLM = &H80000002 'HKEY_LOCAL_MACHINE
@ -128,19 +122,33 @@ For Each strSubkey In arrSubkeys
' foreach registry item, check if exists in the main collector ' foreach registry item, check if exists in the main collector
' it it exists, it doesn't be added. ' it it exists, it doesn't be added.
if ( isItemInArray(appname, colObjSW) = false ) then if ( isItemInArray(appname, colObjSW) = false ) then
' as item doesn't exist, we add it to main collector and to XML ' as item doesn't exist, we add it to main collector and to XML
With colObjSW.Append(New AppClass) With colObjSW.Append(New AppClass)
.caption = appname .caption = appname
.version = appversion .version = appversion
End with End with
Wscript.StdOut.WriteLine "<data><![CDATA[" & appname & ";" & appversion & "]]></data>"
end if end if
end if end if
next next
' Print the XML structure
Wscript.StdOut.WriteLine "<inventory>"
Wscript.StdOut.WriteLine "<inventory_module>"
Wscript.StdOut.WriteLine "<name>Software</name>"
Wscript.StdOut.WriteLine "<type><![CDATA[generic_data_string]]></type>"
Wscript.StdOut.WriteLine "<datalist>"
' Print software installed
For Each i in colObjSW.List
Wscript.StdOut.WriteLine "<data><![CDATA[" _
& colObjSW.item(i).caption & ";" _
& colObjSW.item(i).version _
& "]]></data>"
Next
' Closing the XML structure ' Closing the XML structure
Wscript.StdOut.WriteLine "</datalist>" Wscript.StdOut.WriteLine "</datalist>"
Wscript.StdOut.WriteLine "</inventory_module>" Wscript.StdOut.WriteLine "</inventory_module>"
Wscript.StdOut.WriteLine "</inventory>" Wscript.StdOut.WriteLine "</inventory>"