2

I've seen many ways in powershell to force a computer to do a client action from the configuration manager.

Where I do work, it is not possible because we can't invoke commands on distant computer because it is blocked and the senior IT do not want to unlock it.

I did find a library in c# that allow me to do some action in sccm :

  • AdminUI.SmsTraceListener.dll

  • AdminUI.WqlQueryEngine.dll

I can add/remove computer to a collections, make queries and get the data, but I didn't find the way to force a computer to make an action from the configuration manager.

Is there someone here that knows if it is possible and how?

Thanks.

Edit 1: While searching in the MSDN documentation, I did find the TriggerSchedule Method in Class SMS_Client but I don't find the way to use it correctly. I think it might be the way to go, but i'm still stuck on this.

Wanceslas
  • 107
  • 1
  • 1
  • 10

3 Answers3

3

It is possible to trigger an Machine Policy Update via TriggerSchedule like this

    ManagementScope scope = new ManagementScope(@"\\.\root\ccm");
    ManagementClass cls = new ManagementClass(scope.Path.Path, "SMS_Client", null);

    ManagementBaseObject inParams = cls.GetMethodParameters("TriggerSchedule");
    inParams["sScheduleID"] = "{00000000-0000-0000-0000-000000000021}";

    ManagementBaseObject outMPParams = cls.InvokeMethod("TriggerSchedule", inParams, null);

You already found the other Parameters for the sScheduleID in the link you posted. This uses standard WMI. With WqlQueryEngine you would get access to some WMI wrappers that can basically do the same thing. I do not see many advantages however.

Using the scope like this

\\.\root\ccm

makes the whole thing only work locally which is what you want if I understood you correctly. Otherwise replacing the . With a hostname or IP would make it work remotely. Only thing I found a bit strange is that it needs administrative rights, which should in theory not be necessary for a policy update request.

Syberdoor
  • 2,521
  • 1
  • 11
  • 14
0

if someone is having the issue that nothing is happening, it is because WMI required higher rights. To leave triggering the actions also by the user, I switched to use the CPApplet:

TriggerSccmActions("Request & Evaluate", true);


private static List<string> TriggerSccmActions(string stringActions, bool boolContains)
        {
            List<string> actionName = new List<string>();

        try { 
            const string ProgID = "CPApplet.CPAppletMgr";

            Type foo = Type.GetTypeFromProgID(ProgID);

            dynamic COMobject = Activator.CreateInstance(foo);

            var oClientActions = COMobject.GetClientActions;

            foreach (var oClientAction in oClientActions)
            {
                if (oClientAction.Name.ToString().Contains(stringActions) && boolContains)
                {
                    var result = oClientAction.PerformAction();
                    actionName.Add(oClientAction.Name.ToString());
                }
                else if (!(oClientAction.Name.ToString().Contains(stringActions)) && !(boolContains))
                {
                    var result = oClientAction.PerformAction();
                    actionName.Add(oClientAction.Name.ToString());
                }
            }

        } catch(Exception e)
        {
            actionName.Add("Error: " + e.Message.ToString());
        }

        return actionName;

    }
Stephan
  • 335
  • 3
  • 12
0

For me, EvaluateMachinePolicy Method in Class SMS_Client class worked. Here is the code:

public static void RefreshMachinePolicy(string machineName)
{
    ManagementScope scope = new ManagementScope(string.Format(@"\\{0}\root\ccm", machineName));
    ManagementClass cls = new ManagementClass(scope.Path.Path, "SMS_Client", null);
    ManagementBaseObject inParams = cls.GetMethodParameters("EvaluateMachinePolicy");
    ManagementBaseObject outMPParams = cls.InvokeMethod("EvaluateMachinePolicy", inParams, null);
    Console.WriteLine("Policy refreshed successfully by EvaluateMachinePolicy method");
}

Here is the MSDN link for method details. Please include below namespace at the top of your source code file:

using System.Management;
RBT
  • 24,161
  • 21
  • 159
  • 240