Custom inventory can parse INI files, as well as query WMI, the registry and retrieve file header information. Typically, there's not a lot of demand for support for this feature but there have been a few. Here's a few insights into parsing ini files. This example shows how to retrieve specific values from known sections and name/value pairs. And, it shows how to use a retrieved value from a name/value pair as a variable to retrieve other values from the same, or possible a different, ini file.
INI File Format
Ini files consist of section headers and name/value pairs. The section headers are enclosed in square brackets. Following is a simple ini file. This file has a [Contents] section that names other header sections that appear later in the file. Notice that some of the sections have identically-named name/value pairs, which are: Att1 & Att2.
[Contents]
0=Sect0
1=Sect1
2=Sect2
[Sect0]
Att1=Sect0Att1
Att2=Sect0Att2
[Sect1]
Att1=Sect1Att1
Att2=Sect1Att2
[Sect2]
Att1=Sect2Att1
Att2=Sect2Att2
It is possible to use a particular value queried from the [Contents] section as a variable to identify another section in the ini file. By retrieving the value of [Contents]/0, which is 'Sect0', it is possible to retrieve data from another, previously unknown section of the ini file. Here's the custom inventory logic:
*******************************************************************************
<rs:data>
<%set inipath= ".\inivars.ini" %>
<%set CurSection="ini:%inipath%\[Contents]\0" %>
<%if %CurSection%="Sect0"%>
<z:row
c0="<%writexml "%CurSection%"%>"
c1="<%writexml "ini:%inipath%\[%CurSection%]\Att1"%>"
c2="<%writexml "ini:%inipath%\[%CurSection%]\Att2"%>"
/>
<%endif%>
<%set CurSection="ini:%inipath%\[Contents]\1" %>
<%if %CurSection%="WrongSection"%>
<z:row
c0="<%writexml "%CurSection%"%>"
c1="<%writexml "ini:%inipath%\[%CurSection%]\Att1"%>"
c2="<%writexml "ini:%inipath%\[%CurSection%]\Att2"%>"
/>
<%endif%>
<%set CurSection="ini:%inipath%\[Contents]\2" %>
<%if %CurSection%="sect2"%>
<z:row
c0="<%writexml "%CurSection%"%>"
c1="<%writexml "ini:%inipath%\[%CurSection%]\Att1"%>"
c2="<%writexml "ini:%inipath%\[%CurSection%]\Att2"%>"
/>
<%endif%>
</rs:data>
*******************************************************************************
Notice that the following occurs in this custom inventory logic:
- The path to the ini file is declared. The variable 'inipath' will contain this file path.
- The value for [Contents]/0 is retrieved and stored in the CurSection variable. This value is 'Sect0'.
- The CurSection value is compared to the hard-coded value of 'Sect0'.
- Since the if statement evaluates to true, the value for CurSection is written and the values for [Sect0]/Att1 and [Sect0]/Att2 are retrieved and written.
- The value for [Contents]/1 is retrieved. [Sect1] does NOT match the hard-coded value so nothing is returned for that section.
- The value for [Contents]/2 is retrieved. [Sect2] DOES match the hard-coded value, so the value for CurSection is written and the values for [Sect2]/Att1 and [Sect2]/Att2 are retrieved and written.
The output of the NSI would be:
<rs:data>
<z:row
c0="Sect0"
c1="Sect0Att1"
c2="Sect0Att2"
/>
<z:row
c0="Sect2"
c1="Sect2Att1"
c2="Sect2Att2"
/>
</rs:data>
The associated files are attached below.