The "quiet shy" WMI guy aka Kim Oppalfens

Schedule's are boring, events are sexy!

Using the sms_client TriggerSchedule method against a SCCM Client

Hi All,

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);
            }
        }

    }
}
.csharpcode, .csharpcode pre { font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/ } .csharpcode pre { margin: 0em; } .csharpcode .rem { color: #008000; } .csharpcode .kwrd { color: #0000ff; } .csharpcode .str { color: #006080; } .csharpcode .op { color: #0000c0; } .csharpcode .preproc { color: #cc6633; } .csharpcode .asp { background-color: #ffff00; } .csharpcode .html { color: #800000; } .csharpcode .attr { color: #ff0000; } .csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em; } .csharpcode .lnum { color: #606060; }

 

"The M in WMI stands for Magic"
Kim Oppalfens - Sms Expert for lack of any other expertise
Windows Server System MVP - SMS
http://www.scug.be/blogs/sccm/default.aspx

http://www.linkedin.com/in/kimoppalfens

http://twitter.com/thewmiguy

 

del.icio.us Tags: ,,,

Comments

No Comments