Search code examples
c#visual-studiosccmwql

Formatting a WQL Output in C#


I'm trying to build a query, to list all the known computers in SCCM with a specific name. The query looks like this:

string query = string.Format("Select Name From SMS_R_System Where Name like '" + "%" + computerName + "%" + "'");

If results are found, it puts the result(s) in a dropdown box.

My problem in these case, the output looks like this:

"instance of SMS_R_System{Name = "DC01";};"

But of course, for our use case we only need DC01 as output.

Any tips?

The full Code for the ButtonEvent:

   private void ChkBtn_Click(object sender, EventArgs e)
{
    string computerName = PCDropDown.Text;
    lBox.Items.Clear();

    SmsNamedValuesDictionary namedValues = new SmsNamedValuesDictionary();
    WqlConnectionManager connection = new WqlConnectionManager(namedValues);
    // Connect to remote computer.
    try
    {
        connection.Connect(PrimarySiteServer.ToString());

        // Set the query.
        string query1 = string.Format("Select Name From SMS_R_System Where Name like '" + "%" + computerName + "%" + "'");
        string query2 = string.Format("Select * From SMS_UserMachineRelationship WHERE ResourceName like '" + "%" + computerName + "%" + "' AND IsActive = '1' AND Types = '1'");


        // Get the query results
        IResultObject queryResults = connection.QueryProcessor.ExecuteQuery(query1);

        // Check for results and display in infobox
        bool resultsFound = false;
        foreach (IResultObject queryResult in queryResults)
        {
            resultsFound = true;
            lBox.Items.Add("Rechner ist vorhanden");
            PCDropDown.Items.Add(queryResult.ToString());
        }
        if (resultsFound == false)
        {
            lBox.Items.Add("Rechnername nicht gefunden");
        }
    }
    catch
    {
        MessageBox.Show("No Connection to Config-Manager - Als ZZA ausgeführt? SCCM-Servername richtig?");
    }
}

Solution

  • Instead of adding queryResult.ToString() like you do here:

    PCDropDown.Items.Add(queryResult.ToString());
    

    you need to add the correct field of queryResult, so in this case:

    PCDropDown.Items.Add(queryResult["Name"].StringValue);
    

    Also a quick note. I don't know for who you are writing this and what the next step would be but if this is a read only application that is only used by SCCM Admins I would consider ignoring WMI and going to the SCCM DB via SQL instead. It is a lot faster, SQL has far more powerful options for queries and it does not need the integration of those strange sccm console Dlls (although that is not 100% necessary for WMI either).

    If you need write access to create devices or collections etc., or you need to work with the roles the sccm access rights systems implements however WMI is the better or only choice. (And in this case I'd rather really use those strange dlls because all of the MS examples rely on them and it can be hard to translate those tutorials to the vanilla WMI solution C# offers.