SCCM and Powershell – Force install/uninstall of available software in software center through CIM/WMI on a remote client

What do we want to achieve ?

You have application X deployed (or like in this case Google chrome) as available to your client and you wan’t to trigger the installation remotely without needing to actually go in to software center on that remote computer like this

Install

Perfect if you want to save time and last but not least its cool to run scripts 😀 !

This could be a perfect help for either service desk or if your a sysadmin and have a lot of servers that you maintain.

Script also available over at Technet 

Powershell Script


Function Trigger-AppInstallation
{

Param
(
 [String][Parameter(Mandatory=$True, Position=1)] $Computername,
 [String][Parameter(Mandatory=$True, Position=2)] $AppName,
 [ValidateSet("Install","Uninstall")]
 [String][Parameter(Mandatory=$True, Position=3)] $Method
)

Begin {
$Application = (Get-CimInstance -ClassName CCM_Application -Namespace "root\ccm\clientSDK" -ComputerName $Computername | Where-Object {$_.Name -like $AppName})

$Args = @{EnforcePreference = [UINT32] 0
Id = "$($Application.id)"
IsMachineTarget = $Application.IsMachineTarget
IsRebootIfNeeded = $False
Priority = 'High'
Revision = "$($Application.Revision)" }

}

Process

{

Invoke-CimMethod -Namespace "root\ccm\clientSDK" -ClassName CCM_Application -ComputerName $Computername -MethodName $Method -Arguments $Args

}

End {}

}

Dissecting the script

We start out with naming the function Trigger-AppInstallation (tho it also will uninstall) I’m just bad at making up names lol.

there’s 3 parameters that needs to be specified when running the function and those are

Computername (states the computer name for example SD010)

Appname (The applicaiton you would like to install or uninstall)

Method (Is defined with either Install or Uninstall depending on what you want and it can the parameter will only accept those 2 different strings and if you would write anything else it will just prompt an error msg)


Function Trigger-AppInstallation
{

Param
(
[String][Parameter(Mandatory=$True, Position=1)] $Computername,
[String][Parameter(Mandatory=$True, Position=2)] $AppName,
[ValidateSet("Install","Uninstall")]
[String][Parameter(Mandatory=$True, Position=3)] $Method
)

Next up is the Begin block where we are just getting the CIM instance for that specific application from the remote computer after that we gather the necessary arguments that we need later when we are going to invoke the CIM method in the process block. The only 2 arguments i kept static was IsRebootIfNeeded = $False meaning that it will not reboot when installing or uninstalling the application, if you set this one to $True it will reboot. and Priority i kept as “High” because why not right ?


Begin {
$Application = (Get-CimInstance -ClassName CCM_Application -Namespace "root\ccm\clientSDK" -ComputerName $Computername | Where-Object {$_.Name -like $AppName})

$Args = @{EnforcePreference = [UINT32] 0
Id = "$($Application.id)"
IsMachineTarget = $Application.IsMachineTarget
IsRebootIfNeeded = $False
Priority = 'High'
Revision = "$($Application.Revision)" }

}

The process block, here we are calling the method that’s either Install or Uninstall on the specific CIM instance. Passing through computername, method and arguments from earlier. Information about the method it self you can find over at Microsoft https://msdn.microsoft.com/en-us/library/jj902785.aspx but sadly sometimes the documentation there doesn’t seem to be 100% correct but nether the less its a great starting point.


Process

{

Invoke-CimMethod -Namespace "root\ccm\clientSDK" -ClassName CCM_Application -ComputerName $Computername -MethodName $Method -Arguments $Params

}

Examples

If you trigger the function below you will install the the app Google chrome on computer SD010


Trigger-AppInstallation -Computername SD010 -AppName "Google Chrome" -Method Install

If the script was invoked succesfully you will get back the following return value 0

TriggerappinstallPS

and the installation is triggered on the remote client

Triggerappinstall

If you want to uninstall the same application, just change the -Method parameter to Uninstall instead of install.


Trigger-AppInstallation -Computername SD010 -AppName "Google Chrome" -Method Uninstall

And the following will happen on the client

uninstallapp

Thats all for now, until next time !

Cheers Timmy

32 comments

  1. Since the wmi info will only hold applications that are targeted to devices, is there any way to force an application that is targeted to a user or user group?
    With the new software center, not sure this is possible but thought i would ask if anyone tried.
    Thanks for the tip!

    1. Good question and in theory i think it would work but i havent tried it out yet but i will certainly do. Will get back to you with the result.

  2. For some reason it is not working for me. When I run the script after modifying the computer name – I get a message do you want to run the script – I select Run Once – After that nothing happens on the remote machine and there is no message in PS window.
    I am currently on SCCM 2012 R2 SP1. I can install Google Chrome from SC.

    1. You also have to modify the name of the application, it has to be exatcly the same as you have named it in SCCM. Let me know if that helped ?

  3. Yes I checked the application name – it is Google Chrome – Yet nothing happens. I will try a different application and see the result. Will keep you posted.

  4. I tried with 7Zip, Winrar, VLC Player. I am not able to get the PS script to work. The applications name are exactly as they are listed in SCCM Application.

  5. Will this work to install Operating System as well? I have the operating system image available in the software center and It works when I manually select and install. But not with this script. Any idea?

    1. Hey, No it won’t work because Configuration Manager uses different built in functions to trigger OSD then applications. I don’t know which WMI class it is right now but i will take a look and see if i can figure it out.

  6. timmyit, I am trying to use your code to help me with a task I need to accomplish that this script would be perfect! Unfortunately when I try running it I’m getting the following error and I’m not sure where to go from here? Can you help me?

    PS I:> Trigger-AppInstallation -Computername NR8B5YFTHOU -AppName “AutoCAD Architectural 2017” -Method UnInstall
    Invoke-CimMethod : The WinRM client cannot process the request. Property
    “IsMachineTarget” with MI type MI_STRING does not match the expected type from the
    schema: MI_BOOLEAN. Specify the correct type and retry the operation.
    At line:28 char:1
    + Invoke-CimMethod -Namespace “rootccmClientSDK” -ClassName CCM_Appli …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : MetadataError: (rootccmClientSDK:CCM_Application:Str
    ing) [Invoke-CimMethod], CimException
    + FullyQualifiedErrorId : HRESULT 0x803381e1,Microsoft.Management.Infrastructure
    .CimCmdlets.InvokeCimMethodCommand
    + PSComputerName : NR8B5YFTHOU

    1. Mike-

      I received the same errors myself. What version of powershell is on the machine? I had to tweak the script quite a bit to get it to run on Powershell 2.0 but I think Powershell 4.0 also needs some tweaks.

      1. what tweaks are needed to get it to run under 3.0 or 4.0? I keep getting the same error and cant get past it. it runs fine if I put the computername of my local computer but errors on a remote computer.

      1. Yes. For that error I had to cast the string to bool.
        The line to change within $Args should be:
        IsMachineTarget = [System.Convert]::ToBoolean($Application.IsMachineTarget)

        Another gotcha I found was that I had to be logged in to the remote machine with my admin account just because I don’t have an account with permissions to do it without remote session established.
        Kind of a pain for my case, but I threw in a quick call to MSTSC and a Start-Sleep to work around it.

  7. Hi Mike,
    What tweek that you have made on this script? can you please share your script so that i will make changes. Please note the script is running on PS version 4.0

    Thanks,
    VJ

  8. I often visit your page and have noticed that you don’t update it
    often. More frequent updates will give your
    page higher rank & authority in google. I know that writing content takes a lot
    of time, but you can always help yourself with miftolo’s tools which will
    shorten the time of creating an article to a couple of seconds.

  9. I run this script and no application is return. But I have one application available in software center

  10. I have optional software in the software center, but it is not installing by this script. Do I need to change anything to get optional software too.

  11. Hello,
    I am getting the below error message when i used it in my server, but…. when I use it from my regular computer (workstation), it works.. but it does not install anything….
    Suggestions?
    Thank you

    Get-CimInstance : The client cannot connect to the destination specified in the request. Verify that
    the service on the destination is running and is accepting requests. Consult the logs and
    documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If
    the destination is the WinRM service, run the following command on the destination to analyze and
    configure the WinRM service: “winrm quickconfig”.
    At line:14 char:17
    + $Application = (Get-CimInstance -ClassName CCM_Application -Namespace “rootccm …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ConnectionError: (rootccmclientSDK:CCM_Application:String) [Get-CimIns
    tance], CimException
    + FullyQualifiedErrorId : HRESULT 0x80338012,Microsoft.Management.Infrastructure.CimCmdlets.GetCim
    InstanceCommand
    + PSComputerName :

    Invoke-CimMethod : The client cannot connect to the destination specified in the request. Verify that
    the service on the destination is running and is accepting requests. Consult the logs and
    documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If
    the destination is the WinRM service, run the following command on the destination to analyze and
    configure the WinRM service: “winrm quickconfig”.
    At line:29 char:1
    + Invoke-CimMethod -Namespace “rootccmclientSDK” -ClassName CCM_Application -Com …
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ConnectionError: (rootccmclientSDK:CCM_Application:String) [Invoke-Cim
    Method], CimException
    + FullyQualifiedErrorId : HRESULT 0x80338012,Microsoft.Management.Infrastructure.CimCmdlets.Invoke
    CimMethodCommand
    + PSComputerName :

    1. Hi Miguel
      Add a Start-Service -inputObject Winrm in the begging of your script. Service is not running. Also add Stop-Service at the end to put the client back into the state it was

      I’m getting an error at the Invoke-CimMethod line.

      Invoke-CimMethod : The WS-Management service cannot process the request. The WMI provider returned an ‘invalid parameter’ error.
      At line:30 char:1
      + Invoke-CimMethod -Namespace “rootccmclientSDK” -ClassName CCM_Appli …
      + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      + CategoryInfo : InvalidArgument: (rootccmclientSDK:CCM_Application:String) [Invoke-CimMethod], CimException
      + FullyQualifiedErrorId : HRESULT 0x8033811e,Microsoft.Management.Infrastructure.CimCmdlets.InvokeCimMethodCommand

      1. Invoke-CimMethod : The WinRM client cannot process the request. Property
        “IsMachineTarget” with MI type MI_STRING does not match the expected type from the
        schema: MI_BOOLEAN. Specify the correct type and retry the operation.
        At line:28 char:1
        + Invoke-CimMethod -Namespace “rootccmClientSDK” -ClassName CCM_Appli …
        + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo : MetadataError: (rootccmClientSDK:CCM_Application:Str
        ing) [Invoke-CimMethod], CimException
        + FullyQualifiedErrorId : HRESULT 0x803381e1,Microsoft.Management.Infrastructure
        .CimCmdlets.InvokeCimMethodCommand
        + PSComputerName : NR8B5YFTHOU

        In my case the issue was, that the package had multiple revisions. tried to provide with where object to take the latest revision of the package and it was installed successfull.

  12. Hi,
    i am looking for powershell script which can install the TS under Operating Systems in Software center.
    Above code works for Applications, but not for Programs under Operating Systems.
    Looking help on this.

    thanks
    Thanga

  13. Tried above to trigger repair remotely and it works well if cache has the latest content. Is there any way to download latest content in cache prior to triggering repair method?

    Downloadcontents method of ccm_application does not seem to work

    Any pointers would be helpful

  14. Everything looks like it’s working – the $Application and $args populated correctly, even returns a JobID and Return value of 0, but doesn’t actually invoke anything…anybody have any ideas? Tried running as standard user and admin user, no difference

    Win10 1809
    SCCM 1810

    1. No sorry can’t recall seeing this, I just tested on Win10 1809 and ConfigMgr 1902 and it was working fine for me. On the client side do you see any indication that the application started to Install in the AppEnforce.log located in C:\windows\ccm\logs ?

  15. Hey Timmy

    Does this only work on software that is locally cached on a remote computer, or does it prompt the remote computer to download the software from the server?

    I can download chrome no problem since it’s locally cached but no other software. I am using the exact name as it is in SCCM.

    I keep receiving this error:

    Invoke-CimMethod : The WinRM client cannot process the request. Property “IsMachineTarget” with MI type MI_STRING does not match the expected type from the schema: MI_BOOLEAN.

    Any help would be much appreciated.

    1. Hey,

      If the application is available for the remote computer it downloads the content just as it would do if you clicked on “Install” from software center. As for the “IsMachineTarget” property, are you deploying the application to a device group or user group? Its a couple of years ago I created this script but if I remember correctly it only works if you target devices, but I could be wrong. Under Deployment types -> properties -> User Experience are you installing the software for SYSTEM or User?

  16. Hey,

    I spoke with the application team and they said whether it’s deployed to a machine or user is based on the application.

    However, when I just run $Application = (Get-CimInstance -ClassName CCM_Application -Namespace “root\ccm\clientSDK” -ComputerName $Computername | Where-Object {$_.Name -like $AppName}) to see if it filled the $application variable is filled, it’s empty.

    If I run that line with an application that’s already installed, the variable is filled. I checked the properties of the installed applications and they all say IsMachineTarget = $false.

    I’m assuming that you can’t push out the install if it’s targeted to a user or user group, correct?

  17. I’m trying this script in Powershell 5

    I keep getting this error regardless of what I pass for AppName

    Invoke-InstallSCCMApplication -AppName “Quip” -Method Install
    Invoke-InstallSCCMApplication : Unable to invoke CIM Method for localhost:’Quip’: No instance found with given property values.
    At line:1 char:1
    + Invoke-InstallSCCMApplication -AppName “Quip” -Method Install
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,Invoke-InstallSCCMApplication

    if I run Get-CimInstance -ClassName CCM_Application -Namespace “root\ccm\clientSDK” -ComputerName “localhost” | Where-Object {$_.Name -like “Quip”} for instance I get

    ContentSize :
    Deadline :
    Description :
    ErrorCode : 0
    EstimatedInstallTime :
    EvaluationState : 3
    FullName : Quip
    Name : Quip
    NextUserScheduledTime :
    PercentComplete : 0
    Publisher : Quip
    Type : 1
    AllowedActions : {Install}
    AppDTs :
    ApplicabilityState : Applicable
    ConfigureState : NotNeeded
    DeploymentReport :
    EnforcePreference : 2
    FileTypes :
    HighImpactDeployment :
    Icon : iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAALESURBVFhHvZfLThRBFIZZQhT37hiQBBMYL1FXOu5QHBlFm
    uhCtwoYkKA8ghB9AUffwPfAwMJLYEiIs5TEu4AZHJkh6fL/y66hevqgzUwXnXyLOnXq1J/u06dOtezMjMRiPtNxGHjgGVgAH0EV/AZfAlse3ALtUgwJ0WiDYCnwApSAikkZPAddUkwb0UiwuBXMggrQgZev9qkPty
    +o73f71dbkNQU/TXnqulofHVBrdzLwSatXF1NGCNc+Aa12bBvRiAWd4B3QgVaHTqvSRK624f/YmsyponfOFlIAKcxF9ooY4NgLPnHhm0s9amNsQNwkDj/vZxHjuBHBPDkFe2i/0AAOR8EaF6zkTqjt6WEx8H5gjJX
    cSSPiK+iEPSoAE21gkY7cvPrIEwM2AmNZIvhp22CPCJijw+v+Hqi+IQZqBr4J63M8hm1XAAz81apMmo2xK2KAJNgcz5rEZP3QSWkE8D9X74fPiAuTpOidNW8hj3ELNz8CdJEpTQyKi/zColK+r2I/8OUaKRZ/0UAA
    9zxEASM0LGV7xQXEX327fwFYI8UihcG0EeFRAOs3Ktx50dkFrJiBgDwF6F/vx73LorMLWNwCAQsU8JmDXw92a7trylNDRsA3CtjhoPIwXHj84lLwQZt/GMuOzcIUCKhQwPZfAeGym6yA5VBsEghQFMD6rI/UeieX2
    AJ0Eq6PHlwSElsAOxfdaEiOrrAFsIdDIeoTHUmSldBgC2gH7OH27HqSroSkJgADijiww8hQL6Ab6ON4c9zdcWwTEkAweEoD+0AXDUk9kgC24execVol25JJRAQQGI4BXZgowuWbEAUQGNNAH1D8HK5yYk8BBBNdQF
    9MmJhso9jJYC4x/imAYJJtOq9mbCC1M69drJi8hvHsaCZPTExxcxs4NXI5jYsvbioBZ17PbwKeHTzAmKy1t9MA/nym4+Ufh75CUjFYoSgAAAAASUVORK5CYII=
    Id : ScopeId_6C900AD6-A53B-4C44-B96C-1002E20C5DF9/Application_fb7b7b4f-943e-4bf7-983b-15b78b4a03e9
    InformativeUrl :
    InProgressActions : {}
    InstallState : NotInstalled
    IsMachineTarget : False
    IsPreflightOnly : False
    LastEvalTime : 11/23/2019 10:34:47 AM
    LastInstallTime : 6/22/2019 12:35:45 PM
    NotifyUser : False
    OverrideServiceWindow : False
    RebootOutsideServiceWindow : False
    ReleaseDate :
    ResolvedState : Available
    Revision : 38
    SoftwareVersion : 5.4.32
    StartTime : 6/14/2019 12:45:00 PM
    SupersessionState : None
    UserUIExperience : True
    PSComputerName : localhost

    So it’s there as Quip but gives the previous error. Am I doing something wrong? Or is there additional changes that need to be made to the script to get it working with Powershell 5?

Leave a Reply