I am querying an SCCM database for package and application properties. What it seems like is that if I use a simple query, I can view and access the properties. On a more complex query where I am assigning variable names to keep the properties separate I cannot.
In the example code below, the query for packages will print out those properties as expected. In the query for applications, all it seems to know about is either AL or DT for the name and the value comes back as a ManagementBaseObject. In fact, the code as posted will crash every time on printing out those property values.
Very confusing, appreciate any input to get this working.
using System;
using System.Management;
namespace Application
{
public class MainApplication
{
static void Main(string[] args)
{
ManagementObjectCollection packages = GetPackages();
ManagementObjectCollection applications = GetApplications();
// * Works! *
foreach (var package in packages)
{
Console.WriteLine("Package Name: {0} ", package.Properties["PackageName"].Value.ToString());
Console.WriteLine("Package ID: {0} ", package.Properties["PackageID"].Value.ToString());
Console.WriteLine("Setup Command: {0} ", package.Properties["CommandLine"].Value.ToString());
}
// * Does not work! *
foreach (var application in applications)
{
Console.WriteLine("Package Name: {0} ", application.Properties["AL.LocalizedDisplayName"].Value.ToString());
Console.WriteLine("Package ID: {0} ", application.Properties["DT.ContentID"].Value.ToString());
Console.WriteLine("Setup Command: {0} ", application.Properties["DT.LocalizedDescription"].Value.ToString());
}
}
private static ManagementObjectCollection GetPackages()
{
String queryString = "SELECT PackageName,PackageID,CommandLine,Comment FROM SMS_Program WHERE Comment LIKE '%LISTMANUAL%' ORDER BY PackageName";
ObjectQuery query = new ObjectQuery(queryString);
ManagementScope scope = new ManagementScope("\\\\somebox.somedomain.com\\root\\sms\\site_DC1");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection objectCollection = searcher.Get();
Console.WriteLine(objectCollection.Count);
return objectCollection;
}
private static ManagementObjectCollection GetApplications()
{
String queryString = "SELECT DT.LocalizedDisplayName,DT.LocalizedDescription,DT.AppModelName,DT.ContentID,AL.LocalizedDisplayName,AL.ModelName FROM SMS_DeploymentType AS DT JOIN SMS_ApplicationLatest AS AL on AL.ModelName=DT.AppModelName JOIN SMS_PackageToContent AS PTC on PTC.ContentUniqueID = DT.ContentID WHERE DT.IsLatest='TRUE' AND DT.LocalizedDescription LIKE '%LISTMANUAL%' ORDER BY AL.LocalizedDisplayName";
ObjectQuery query = new ObjectQuery(queryString);
ManagementScope scope = new ManagementScope("\\\\somebox.somedomain.com\\root\\sms\\site_DC1");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
ManagementObjectCollection objectCollection = searcher.Get();
Console.WriteLine(objectCollection.Count);
return objectCollection;
}
}
}