Client Management Suite

 View Only

{CWoC} Calling Basic Inventory from C# 

Jan 11, 2010 02:16 PM

The Altiris Agent provides a COM interface which can be called to invoke certain agent functions. Two common examples are sending a basic inventory or requesting new policies. Normally these happen on a defined schedule, something like every four hours for new policies and twenty four hours for basic inventory depending on your environment, but it can be useful to call these functions on demand.

For instance, you may have a custom scenario where you want to force all agents to send basic inventory when something happens on the machine. It’s possible to do this by calling the COM interface and it’s very easy to do using VBScript. The code to do this is as follows (Source: Altiris KB 36717)

dim client set client = wscript.createobject ( "Altiris.AeXNSClient" )
ignoreBlockouts = 1
sendIfUnchanged = 1
client.SendBasicInventory sendIfUnchanged, ignoreBlockouts

In this code example we are instantiating an instance of a COM object by calling the createobject method and passing in the ProgID of the object. The ProgID in this case is Altiris.AexNSClient. This refers to an entry in the HKCR of the registry, which in turn contains a Class ID GUID which in turn points to the DLL or executable containing the code to run.

But we don’t really need to know all that. Just run the above vbscript on any client which has the Altiris agent installed and it will immediately send a basic inventory.

I used a COM explorer utility (http://www.japheth.de/COMView.html) to view which methods can be run for the ns agent and the following screenshot provides the full listing. Note that this is for a NS 7 agent so we have new methods relating to Maintenance Window functionality. The other methods appear to be the same as NS 6.

exportedMethodsjpg.JPG

Calling Basic Inventory from C#

So it’s really easy to start a basic inventory from a vbscript but what if our needs are a bit more complex? What if I want to listen for a change on the machine and then automatically send inventory as soon as it occurs? I’m going to need a more powerful language to do this and I prefer to do my coding in C#.

So to use the COM interfaces in C# we have to use the interoperability functionality available within .NET. This just means we have to jump through a few hoops to call the COM class from a .NET app.

The following code can be added to a C# project to launch a Basic Inventory. This uses something called late binding so you have to include using System.Reflection in the class header but I’ve attached the sample C# classes so you can inspect exactly how it’s done.

Type typeAgent = null;
object agent = null;
object[] args = { 1, 1 };
// Make a call to get type information for the given ProgID
typeAgent = Type.GetTypeFromProgID("Altiris.AeXNSClient");
// Create an instance based on this type
agent = Activator.CreateInstance(typeAgent);
// Invoke a method on this instance
typeAgent.InvokeMember("SendBasicInventory", BindingFlags.InvokeMethod, null, agent, args);
MessageBox.Show("Basic inventory sent");

So in this code we set up an object to store the Type Library information from the given ProgID. That is to say all the properties and methods which relate to that ProgID. In this case we’re saying go and get me the type library for “Altiris.AeXNSClient” and it will then return an object of this type with all it’s methods including, of course, SendBasicInventory.

We then create an instance of this object using Activator.CreateInstance and then use InvokeMember to call SendBasicInventory.

I’ve attached a sample executable. It displays a windows form with a button. When you click the button a basic inventory will be sent. It works for 6 and 7 version agents. I’ve also attached the C# source files as well.

This is a very basic example but I hope it shows how easily we can extend functionality and maybe it will give you some ideas on how you might expand this to solve problems in your company. In a future article I'm going to build on this with a more practical example.


 

 

Statistics
0 Favorited
2 Views
1 Files
0 Shares
0 Downloads
Attachment(s)
zip file
AltirisCOMExample.zip   6 KB   1 version
Uploaded - Feb 25, 2020

Tags and Keywords

Comments

Jan 13, 2010 04:49 AM

 Thanks guys. Sorry about the corrupt initial attachment. I have re-added and it and tested the download. Should be ok now.

Jan 12, 2010 04:33 PM

I like the late binding approach as it's much cleaner than using COMInterop which forces you to have an extra dll to interop with the COM classes.

But using the COMInterop has the benefits to allow you to see all properties and method (public) from Visual Studio so you when you build your class intelli-sense does it's work enumerating possible options, method overloads etc.

So thanks for this nice article David! 

Jan 12, 2010 01:54 PM

Can you check the download? It appears the zip file is corrupt. Great article!

Related Entries and Links

No Related Resource entered.