Custom inventory example
This example shows a custom inventory script that looks up the department of the computer’s default user in Active Directory. It writes the department to the custom inventory of the computer.
How custom inventory scripts work
Section titled “How custom inventory scripts work”- The Custom Inventory computer package runs the script in its
CustomInventory.cisfile. Put your own code in that file. - The script must contain a function named
CustomInventory(). If the function is missing, the package writes a message to its log and collects nothing. - Write each value with
Ini_WriteEntryto the file in the variablemsInventoryFile(<unit name>.csiin the agent folder). The second parameter is the category, the third is the entry name, and the fourth is the value. - After the script runs, the package uploads the file. The values appear as custom inventory for the computer in the console.
Example
Section titled “Example”In this example, replace the LDAP path cn=USERS,dc=Test,dc=local with the path to the users in your own domain. The script reads the default user from the logon inventory, so the Logon Inventory package must run on the computer first.
' Customer function for collecting custom inventory.
Dim sVersionsBuild="1"
Private Function GetDefaultUSer()Dim sxmlFile, oNodeList, sUser'Begin On Error Resume Next bStatus=True If bStatus Then bStatus=Job_WriteLog("GetDefaultUSer","Get default user",bStatus,True) If bStatus Then sxmlFile=gsWorkstationPath & "Agent\" & gsUnitName & ".lgi" Set oxmlDoc = CreateObject("Msxml2.DOMDocument") If isObject(oxmlDoc) Then oxmlDoc.Load (sxmlFile) Set oNodeList = oxmlDoc.selectNodes("//Inventory/Section [@Value= " & Chr(34) & "Default User" & Chr(34) & "]")
For each oNode in oNodelist sUser=Trim(Mid(oNode.Text,1,Instr(oNode.Text," ")-1)) Next End if End If If bStatus Then bStatus=Job_WriteLog("GetDefaultUSer","Found default user '" & sUser & "'",bStatus,True) GetDefaultUSer=sUserEnd Function
Function GetDepartment(sUser)Dim bStatus, objUser, sDept'Begin On Error Resume Next bStatus=True If bStatus Then bStatus=Job_WriteLog("GetDepartment","Get default user department",bStatus,True) Set objUser = GetObject("LDAP://cn=" & sUser & ",cn=USERS,dc=Test,dc=local") If isObject(objUser) Then objUser.GetInfo sDept=objUser.Get("department") Else sDept="" End If If bStatus Then bStatus=Job_WriteLog("GetDepartment","Default user department '" & sDept & "'",bStatus,True) GetDepartment=sDeptEnd Function
Private Function CustomInventory()Dim bStatus 'Begin On Error Resume Next 'Avoid runtime errors bStatus=True 'initialize as True If bStatus Then bStatus=Job_WriteLog("CustomInventory","Custom Inventory Script build " & sBuild,bStatus,True)
If bStatus Then sUser=GetDefaultUSer() If bStatus and (sUser<>"") Then If bStatus Then sDept=GetDepartment(sUser)
If bStatus and (sDept<>"") Then If bStatus Then bStatus=Ini_WriteEntry(msInventoryFile,"Customer Inventory","Department",sDept) End If End If
If bStatus Then bStatus=Job_WriteLog("CustomInventory","Custom Inventory Script end.",bStatus,True) CustomInventory=bStatusEnd Function