Video Screencast Help

Need customer inventory PowerShell Example

Created: 04 Apr 2013 • Updated: 08 Apr 2013 | 5 comments
This issue has been solved. See solution.

If anyone would be so kind to please post an example of a custom inventory for 7.1 SP2 using PowerShell.  Specifically, the PowerShell script must return more than one row of data.  I'm clear on how to create a custom inventory which populates the outgoing NSE with one row of data.  For example if a service is running or not.  I'm now moving on to a powershell script that could return 20 or 200 rows.  I just need an example to see how you're setting multiple rows of data.

Thank you

Operating Systems:

Comments 5 CommentsJump to latest comment

jason.f's picture

Here's one I wrote up for inventoring Access Database files. You can see that it adds a new row for each time we loop through the array of data files and then populates the 5 columns with info. Let me know if you need any help tweaking it to what you're inventorying.

This is the line that adds a new row: $objDataRow = $objDataClass.AddRow()

Make sure when you build your custom data class you specify more than one row can be returned from each computer.

#************************DO NOT EDIT********************************
$nse = new-object -comobject Altiris.AeXNSEvent
$nse.priority = 1
$nse.To = "{1592B913-72F3-4C36-91D2-D4EDA21D2F96}"
#************************DO NOT EDIT********************************

#Modify this varaible with the custom data class guid
$objDCInstance = $nse.AddDataClass("{5f939d49-f82a-497f-8d48-025cfe89edae}")

$objDataClass = $nse.AddDataBlock($objDCInstance)

$datafiles = Get-WmiObject -Query "SELECT * FROM CIM_DataFile where drive='c:' and extension='mdb'"

#loop through each data file we found and populate rows and columns.
foreach ($datafile in $datafiles)
    if (-not (($datafile.Path -like "*Altiris*") -or ($datafile.FileName -like "*Sample*")))
        #Add new row of data
        $objDataRow = $objDataClass.AddRow()
        #popluate columns
        $objDataRow.SetField(0, $datafile.FileName)
        $objDataRow.SetField(1, $datafile.Path)
        $objDataRow.SetField(2, $datafile.FileSize.ToString())
        $objDataRow.SetField(3, ([System.Management.ManagementDateTimeconverter]::ToDateTime($datafile.CreationDate)).tostring())
        $objDataRow.SetField(4, ([System.Management.ManagementDateTimeconverter]::ToDateTime($datafile.LastAccessed)).tostring())

#Send the data

Lery's picture

Thanks jason.f.  I'll look at this shortly and see if I can figure things out.  I'm having trouble with the piece that populates columns.  I have no issue when when I return a single value like for example:

get-itemproperty -path HKLM:\Software\altiris\"altiris agent" -name machineguid

The SetField is simply this:

$objDataRow.SetField(0, $data.machineguid)

However, when you have something like for example, get-hotfix, it's more complicated.  

I should be able to figure out based on your example.  I hope :-)

jason.f's picture

When you setup your custom data class, add all the attributes you want to collect for the Hotfix (eg: HotFixID, Description, InstalledDate, etc...) As you build them, note what column number the attribute is, starting at 0.  When you go to set your fields, the number after the opening bracket is the column number. For Hotfixes you build your data class as above and then loop through each hotfix and insert the data like this:

$hotfixes = Get-HotFix
foreach ($hotfix in $hotfixes)
        #Add new row of data
        $objDataRow = $objDataClass.AddRow()
        #popluate columns

Let me know if you need any other help with it. If this solved your problem can you mark me as solution.



Lery's picture


I apologize for not responding sooner.  I was able to get what I needed working with your script example.  

Taking this a step further, just using your latest example, I'm also able to get most of that working. However, $objDataRow.SetField(2,$hotfix.InstalledOn) is not working.  It looks as if it's not allowing for a string.  Although, the format technically could be a string.  The output of InstalledOn is:  10/17/2012 12:00:00 AM.  So, I simply force it ToString and the world is happy once again: $objDataRow.SetField(2,$hotfix.InstalledOn.ToString())

I'll go ahead and mark your script examples as a solution.  I thank you again.