Site icon TimmyIT.com

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

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

and the installation is triggered on the remote client

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

Thats all for now, until next time !

Cheers Timmy

Exit mobile version