Submitting hardware inventory records to an SMS 2003 MP using the MP API
There are several supported ways to add data to the SMS database but they typically come from an SMS 2003 advanced client. If you want to add data to an existing client without having to make the agent on the client send it up, or add data to the database for a non-SMS client, then you can use the MP API.
The SMS SDK contains documentation and some sample code for submitting data via the MP API. There are some other things you'll need to do in order to get the MP API working with SMS 2003 SP1 and above.
Below is an example of using the MP API to submit hardware inventory data to a Management Point. This data gets converted from XML to a MIF by the MP, then moved to the site server, and finally loaded into the database. Keep in mind that the hardware inventory data I'm loading into the database in this example is the minimum so much more can be added. Also, you can create your own GUIDs using CoCreateGUID().
Here are some tips when developing/testing submitting discovery data using the MP API.
- Enable verbose and debug logging on the SMS 2003 Management Point you're working with.
- Have an SMS 2003 advanced client already submitting data to this MP.
- Turn on report archival on the advanced client so you can see what a properly formatted XML looks like.
- View the following logs in this order: IIS logs (shows IIS handling the message), ccmisapi.log (SMS ISAPI filter), MP_Hinv.log (converts the XML to a MIF), mpfdm.log (moves the MIF from the MP to site server), and dataldr.log (loads the MIF into the database).
- Make sure you've installed the hotfix from KB917873 onto your SDK. This article is not published as of 06/29/2006 but the hotfix is still available by calling Microsoft support.
- Don't forget to set the Byte Order Mark in your XML attachment.
- Make sure you call SetClientID, which is only usable if you've included the .h/.c files from KB917873 in your project.
- If the program fails when running on the MP with https:// in front of the MP Name, try running it without the https:// (this goes through COM instead of IIS).
- Run the DDRExample program first to create a discovery record in the database prior to using this application
// MPAPI-HinvExample.cpp : Defines the entry point for the console application.
// Rslaten 06/2006
#include "stdafx.h"
#include "MPAPI-HinvExample.h"
#include "SmsMsgAPI.h"
#include "SmsMsgAPI_i.c"
#include "strsafe.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
#define MAX_BUFFER_LENGTH 0x1000
CWinApp theApp;
using namespace std;
// Macros to perform error handling and make the code cleaner.
#define _BEGIN \
HRESULT __hrRetVal = S_OK; \
{ \
#define _END \
} \
__End: \
#define _RETVAL __hrRetVal
#define _CHECKHR(expr) if(FAILED(expr)) { __hrRetVal=expr; goto __End; }
//Main
int wmain( int argc, wchar_t *argv[], wchar_t *envp[] )
{
int nRetCode = 0;
// initialize MFC and print and error on failure
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: change error code to suit your needs
_tprintf(_T("Fatal Error: MFC initialization failed\n"));
nRetCode = 1;
}
else
{
if (argc < 3)
{
printf("Usage:MPAPI-HinvExample.exe <https://mpname> <sitecode>\n");
}
else
{
static WCHAR HINVBody[MAX_BUFFER_LENGTH];
static WCHAR HINVAttach[MAX_BUFFER_LENGTH];
ISmsMessaging *pMessaging = NULL;
ISmsMessage *pRequest = NULL;
ISmsMessage *pReply = NULL;
ISmsMessage2 *pRequest2 = NULL;
WCHAR *pszRequestMessage = NULL;
WCHAR *pszReplyMessage = NULL;
WCHAR *sMPName = NULL;
WCHAR *sSiteCode = NULL;
WCHAR *pszTrackingID = NULL;
LPCWSTR sClientID = NULL;
LPCWSTR sReportID = NULL;
LPCWSTR sEndPoint = NULL;
//Set variables
sReportID = L"{00000000-0000-0000-0000-000000000001}";
sEndPoint = L"MP_HinvEndpoint";
sClientID = L"GUID:8081C07C-4674-48E4-A525-5CAD419A4EB3";
sMPName = argv[1];
sSiteCode = argv[2]; //Not used
CoInitialize(NULL);
//Build Report Header for Hinv
wcscat(HINVBody,L"<ReportHeader>");
wcscat(HINVBody,L"<Identification>");
wcscat(HINVBody,L"<Machine>");
wcscat(HINVBody,L"<ClientID>GUID:8081C07C-4674-48E4-A525-5CAD419A4EB3</ClientID>");
wcscat(HINVBody,L"</Machine>");
wcscat(HINVBody,L"</Identification>");
wcscat(HINVBody,L"</ReportHeader>");
//Build Message Attachment for Hinv
HINVAttach[0] = 0xFEFF; //Add BOM
wcscat(HINVAttach,L"<Report>");
wcscat(HINVAttach,L"<ReportHeader>");
wcscat(HINVAttach,L"<Identification>");
wcscat(HINVAttach,L"<Machine>");
wcscat(HINVAttach,L"<ClientInstalled>1</ClientInstalled>");
wcscat(HINVAttach,L"<ClientType>1</ClientType>");
wcscat(HINVAttach,L"<ClientID>GUID:8081C07C-4674-48E4-A525-5CAD419A4EB3</ClientID>");
wcscat(HINVAttach,L"<ClientVersion>1.0</ClientVersion>");
wcscat(HINVAttach,L"<NetBIOSName>MPAPITestClient</NetBIOSName>");
wcscat(HINVAttach,L"<CodePage>437</CodePage>");
wcscat(HINVAttach,L"<SystemDefaultLCID>1033</SystemDefaultLCID>");
wcscat(HINVAttach,L"</Machine>");
wcscat(HINVAttach,L"</Identification>");
wcscat(HINVAttach,L"<ReportDetails>");
wcscat(HINVAttach,L"<ReportContent>Inventory\\x0020Data</ReportContent>");
wcscat(HINVAttach,L"<ReportType>Full</ReportType>");
wcscat(HINVAttach,L"<Date>20060120124946.000000-360</Date>");
wcscat(HINVAttach,L"<Version>1.3</Version>");
wcscat(HINVAttach,L"<Format>1.0</Format>");
wcscat(HINVAttach,L"</ReportDetails>");
wcscat(HINVAttach,L"<InventoryAction ActionType=\"Predefined\">");
wcscat(HINVAttach,L"<InventoryActionID>{00000000-0000-0000-0000-000000000001}</InventoryActionID>");
wcscat(HINVAttach,L"<Description>Hardware</Description>");
wcscat(HINVAttach,L"<InventoryActionLastUpdateTime>20050913162140.000000+000</InventoryActionLastUpdateTime>");
wcscat(HINVAttach,L"</InventoryAction>");
wcscat(HINVAttach,L"</ReportHeader>");
wcscat(HINVAttach,L"<ReportBody>");
wcscat(HINVAttach,L"<Instance ParentClass=\"CCM_System\" Class=\"CCM_System\" Namespace=\"\\\\MPAPITestClient");
wcscat(HINVAttach,L"//root//ccm//invagt/" Content=\"New\">");
wcscat(HINVAttach,L"<CCM_System>");
wcscat(HINVAttach,L"<Domain>MPAPITestDOM.com</Domain>");
wcscat(HINVAttach,L"<LocalDateTime>20060120124932.664000-360</LocalDateTime>");
wcscat(HINVAttach,L"<Name>MPAPITestClient</Name>");
wcscat(HINVAttach,L"<SMSID>GUID:8081C07C-4674-48E4-A525-5CAD419A4EB3</SMSID>");
wcscat(HINVAttach,L"<SystemRole>Workstation</SystemRole>");
wcscat(HINVAttach,L"<SystemType>X86-based\\x0020PC</SystemType>");
wcscat(HINVAttach,L"</CCM_System>");
wcscat(HINVAttach,L"</Instance>");
wcscat(HINVAttach,L"</ReportBody>");
wcscat(HINVAttach,L"</Report>");
_BEGIN
//Create root messaging object
_CHECKHR( ::CoCreateInstance(
CLSID_SmsMessaging,
NULL,
CLSCTX_INPROC,
IID_ISmsMessaging,
(LPVOID*)&pMessaging) );
//Create message object for request
_CHECKHR( pMessaging->CreateMessage(&pRequest) );
//Use the SetClientID method (must have .c/.h files from KB917873)
_CHECKHR( pRequest->QueryInterface(IID_ISmsMessage2, (void **) &pRequest2) );
_CHECKHR( pRequest2->SetClientID(sClientID) );
//Set Body (Header)
_CHECKHR( pRequest2->SetBodyFromString(HINVBody) );
//Get length of attachment
size_t pcch;
StringCchLengthW(HINVAttach,MAX_BUFFER_LENGTH,&pcch);
ULONG uSize = pcch * 2;
//Set Attachment
_CHECKHR( pRequest2->SetAttachmentFromBuffer(
sReportID,
(const BYTE*)HINVAttach,
uSize) );
//Set EndPoint
_CHECKHR( pRequest2->SetTargetEndpoint(sEndPoint) );
//Post Message
_CHECKHR( pMessaging->Post(sMPName, pRequest2, &pszTrackingID) );
_END
printf("\n\n%s%X", "Return Code=", __hrRetVal);
if(pszTrackingID)
{
::CoTaskMemFree(pszTrackingID);
}
return S_OK;
pRequest2->Release();
}
}
CoUninitialize();
return nRetCode;
}
Comments
- Anonymous
June 29, 2006
There are several supported ways to add data to the SMS database but they typically come from an SMS... - Anonymous
June 29, 2006
Great post! - Anonymous
July 07, 2006
A very good read... Thanks for making us aware Rod, by posting it at ITForum.