Powershell – Install SCCM application to client via powershell, comand line or WMI

applicationpowershellsccm-2012wmi

I am trying to create a script in powershell that request and install an available application for a user or computer in the sccm application catalog.

I know that there is a way to force sccm client actions using wmi methods.

Example

WMIC /namespace:\\root\ccm path sms_client CALL TriggerSchedule "{00000000-0000-0000-0000-000000000003}" /NOINTERACTIVE

There is a way that by executing a wmi method or command line, in the users computer, to force the installation of and application.

Best Answer

Here is a solution in vbscript if you're interested. I'm not well versed in powershell yet but I do alot of .net and vbscript automation of SCCM. I'm sure someone here could translate to powershell.

'Declare WMI connection to CCM, temporarily disable errors so the script doesnt stop if the namespace doesn't exist.  We will handle the error next.

On Error Resume Next
      Dim oWMI : Set oWMI = GetObject("winmgmts:\\.\root\ccm\ClientSDK")
On Error Goto 0

' Check the datatype of oWMI to make sure its not null for error handling.

If VarType(oWMI) > 1 Then 

    'Run a query for all applications
    Dim colItems, objItem : Set colItems = oWMI.ExecQuery("Select * from CCM_Application")

    'Same as above... error handling in case nothing was returned
    If VarType(colItems) > 1 Then 

    ' Iterate through all the applications available
        For Each objItem in colItems
            Wscript.Echo "Application: " & objItem.FullName & vbCr & _ 
            "Description: " & objItem.Description & VbCr & _ 
            "Publisher: " & objItem.Publisher & VbCr & _ 
            "Version: " & objItem.SoftwareVersion & VbCr & _ 
            "Install State: " & objItem.InstallState & VbCr & _ 
            "Id: " & objItem.Id & VbCr & _ 
            "Revision: " & objItem.Revision

            'In my example, if the application is Adobe Air then run the install action

            If objItem.FullName = "Adobe Air" and ObjItem.InstallState = "NotInstalled" Then

                'First three parameters identify the application (id, revision, and ismachinetarget).  The Next 3 are EnforcePreference, Priority and IsRebootIfNeeded options.
                '0, "Foreground", "False" sets immediate foreground install with no reboot
                'See the msdn page for info about what other options you can set: https://msdn.microsoft.com/library/jj902780.aspx

                Dim ReturnCode : ReturnCode = objItem.Install(objItem.Id, objItem.Revision, objItem.IsMachineTarget, 0, "Foreground", "False")

                'Returns 0 if it successfully started the install.  This does NOT return the exit code of the installation itself.

                Wscript.Echo "Return Code: " & ReturnCode

            End If
        Next

    End If

End If