0

I'm trying to get SCCM applications to install/uninstall remotely via WMI queries from a .Net app.

Code I have is below:

    Dim wmiLoc As ManagementScope = New ManagementScope("\\" & Target & "\root\ccm\ClientSDK", Options)
    Dim wmiProcPath As ManagementPath = New ManagementPath("CCM_Application")
    Dim mc As ManagementClass = New ManagementClass(wmiLoc, wmiProcPath, Nothing)
    Dim inParams As ManagementBaseObject = mc.GetMethodParameters("Install")

    inParams("EnforcePreference") = "0"
    inParams("Id") = ThisID
    inParams("IsMachineTarget") = ThisIMT
    inParams("IsRebootIfNeeded") = "False"
    inParams("Priority") = "High"
    inParams("Revision") = ThisRev

    Dim outParams As ManagementBaseObject = mc.InvokeMethod("Install", inParams, Nothing)
    Console.WriteLine("Job Id: {0}, Return: {1}", outParams("JobId"), outParams("ReturnValue"))

I suppose you can assume the Rev#, ID, and IsMachineTarget are correct given I pull them from the information I query before this from the machine.

** When I send this particular request to the machine, it logs it into CCM/Logs AppDiscovery only. It shows it in 4 lines: it got the request (not for uninstall specifically but the query), performing detection, discovered app, and detected app deployment.

After it logs this, it does nothing. It's almost as if it didn't get the "Uninstall" or "Install" part of it. **

Anyone have any experience handling this? I know there are other solutions out there but I'm trying to work in my own. I can probably do this via powershell but I'd rather not if possible.

Rathfon
  • 3
  • 1
  • As far as I know the whole point of applications is that the installation is based purely on the evaluation of the detection method. Atm there is no way of forcing an install, if it is detected it will never be installed (there is a request to change this on uservoice, with the status "planned" as of today however) – Syberdoor Mar 22 '18 at 15:59
  • I'm not sure I understand what you mean. If strictly speaking for "available" apps you can do this through powershell via WMI. You mention if it is detected it will never be installed? Detected as in the 'discovered'? – Rathfon Mar 22 '18 at 17:02
  • Ah ok I misunderstood your intention. I was speaking about the use of the "detection method" applications use to determine if the installation was successful or not. If the application is required this is the only check that determines whether it will be installed or not even before the first install but for available ones this is not true. If you got it to work with wmi in ps however I don't see how this would not work in C# as well, there should be no difference in the use of wmi. There are wrapper classes for sccm people sometimes use but the normal wmi implementation should still work – Syberdoor Mar 23 '18 at 09:34

1 Answers1

0

I ended up getting this to work. I have a production app that helps me expedite Windows Updates on my farm of servers. Part of that is retrying Application and Update installs.

using (var searcher = new ManagementObjectSearcher(string.Format(@"\\{0}\root\CCM\ClientSDK", strComputerName, "SELECT * FROM CCM_Application"))
foreach (var obj in searcher.Get())
    if (((string)obj["Name"]).Equals(strApplicationName))
        using (var mInv = new ManagementClass(string.Format(@"\\{0}\root\CCM\ClientSDK", strComputerName), "CCM_Application", null))
        {
            ManagementBaseObject inParams = mInv.GetMethodParameters("Install");
            inParams["EnforcePreference"] = (uint)0;
            inParams["Id"] = obj["Id"];
            inParams["IsMachineTarget"] = obj["IsMachineTarget"];
            inParams["IsRebootIfNeeded"] = false;
            inParams["Revision"] = obj["Revision"];
            mInv.InvokeMethod("Install", inParams, null);
        }
CircaLucid
  • 342
  • 3
  • 8