Share This Post

Using the sms_client TriggerSchedule method against a SCCM Client

The ConfigMgr SDK contains a sample to trigger an sms_client method using the c sharp system.management class, or in other words by using WMI natively from C#.

http://msdn.microsoft.com/en-us/library/cc146163.aspx#footerLink

The sample uses the GetAssignedSite and SetAssignedSite methods, which are very useful samples, just not as useful as the TriggerSchedule method according to me.

The sample is easilly adjusted to use the triggerschedule method though, all you need to know is the triggerschedules to use, and your set to go.

The triggerschedules have been documented quite a bit, I documented them in my wmic list in an earlier post over at Scug here:

http://scug.be/blogs/sccm/archive/2009/09/15/adding-wmic-right-click-actions-to-the-configmgr-2007-admin-console-1.aspx

One other adjustment I made to the sample is modify the error-handling catch block to accommodate for COM exception handling. There is a fair chance that triggering these methods fails. It might fail because you don’t have the necessary permissions, because a firewall is blocking access to WMI, or because the machine you are targeting is not online. Any of these type of failures will result in a hard error in the existing SDK sample as it will trigger a COM exception which isn’t handled by the catch block in the sample. To trap COM exceptions you need to use a specific exception type called System.Runtime.InteropServices.COMException.

So without further ado here is the slightly reworked sample.

using System;
using System.Management;

namespace TriggerSchedule
{
    class Program
    {
        static void Main(string[] args)
        {
            TriggerSchedule("sccm06");
         }
        static void TriggerSchedule(string computerName)
        {
            try  // Get the client's SMS_Client class.
            {
                ManagementScope scp = new ManagementScope(string.Format(@"\{0}\root\ccm", computerName));
                ManagementClass cls = new ManagementClass(scp.Path.Path, "sms_client", null);
                ManagementBaseObject inParams;

                // Set up the machine policy & evaluation string as input parameter for TriggerSchedule.
                    inParams = cls.GetMethodParameters("TriggerSchedule");
                    inParams["sScheduleID"] = "{00000000-0000-0000-0000-000000000021}";

                // Trigger the machine policy and evaluation cycle.
                ManagementBaseObject outMPParams = cls.InvokeMethod("TriggerSchedule", inParams, null);
            }
            catch (System.Runtime.InteropServices.COMException ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

    }
}

Share This Post

Leave a Reply