SCCM Detection Script (PowerShell) for the Visual C++ Redistributable(s)

As of writing this, I am working as an apprentice in IT and one of my duties is administering our SCCM server, taking care mostly of application and operating system deployments. One of the things we've previously had a lot of trouble with, is deploying the Visual C++ Redistributables.

SCCM Detection Script (PowerShell) for the Visual C++ Redistributable(s)

As of writing this, I am working as an apprentice in IT and one of my duties is administering our System Center Configuration Manager (SCCM) server, taking care mostly of application and operating system deployments.

SCCM is a device management software by Microsoft, used to automatically install operating systems, provision applications and keeping computers (and the installed software) up to date on a large scale.

One of the things we've previously had a lot of trouble with, is deploying the Redistributable(s) of the Microsoft Visual C++ runtime. As many will know, there are quite a lot of these things, and different applications will use different versions of this runtime, so it's rarely sufficient to install just one of them. So to solve this issue, we've opted to install all currently available packages on our computers.

To deploy an application, SCCM needs a way to detect if that application is already installed, or whether a deployment initiated through SCCM succeeded. There's several different ways to that, three of them being rather simple, the last one not so much:

  1. Windows Installer: For applications that utilize MSI packages to install, the SCCM client can use the integrated Windows Installer database to detect if an application deployment succeeded. For that you will need to provide the MSI's so called product code.
  2. Windows Registry: Most if not all applications leave some entries in the Windows registry, therefore the SCCM client can check for a specific key and value, and optionally even check if that entry conforms to certain requirements the SCCM administrator specified.
  3. Files: Likewise to the Windows Registry method, the SCCM client can check for the existance of a specific file in the local file system (and again optionally for certain requirements on that file).

The last method and the one I've employed for detecting this specific application, is using a custom script. This can be a PowerShell script, a VBScript or a JScript script. I'm using a PowerShell script both because it's the most modern one, and because it's the language I know best of the three:

# Collection of all versions of the runtime the script should check for,
# adjust to fit your needs. In its current state it will detect all
# Visual C++ Redistributables from 2005 to 2019 in both x86 and x64 flavors.

$PackagesFound = @{
    "2005" = $False;
    "2008x86" = $False;
    "2010x86" = $False;
    "2012x86" = $False;
    "2013x86" = $False;
    "2015-2019x86" = $False;
    
    "2005x64" = $False;
    "2008x64" = $False;
    "2010x64" = $False;
    "2012x64" = $False;
    "2013x64" = $False;
    "2015-2019x64" = $False;
}

# Variable initialization
$Year = ""
$Architecture = ""

# Iterate through below mentioned registry keys
& {
    Get-ChildItem HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*
    Get-ChildItem HKLM:\SOFTWARE\WoW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*
} | ForEach-Object { 
    $CurDisplayName = $_.GetValue("DisplayName") # Get DisplayName
    # Check if Display Name begins with "Microsoft Visual C++" and if so extract the year.
    if( $CurDisplayName -match "^Microsoft Visual C\+\+\D*(?<Year>(\d|-){4,9}).*Redistributable.*" ) {
        $Year = $Matches.Year
        [Void] ($CurDisplayName -match "(?<Arch>(x86|x64))") # Extract Architecture
        $Architecture = $Matches.Arch
        $PackagesFound[ ''+$Year+$Architecture ] = $True
    }
}


# Report result back to SCCM
If ( $PackagesFound.Values -notcontains $False) {
    "All required versions of Microsoft Visual C++ were found"
    $PackagesFound
} Else {
    ### DO NOT OUTPUT ANYTHING HERE ###
}

If you look at the last few lines of the code, you will see that this script doesn't use exit codes to convey its result. This is because SCCM in the end doesn't care about those. What it does care about is if there is any output from the detection script. If there is, SCCM considers the application detected. I've combined this script with a script (utilizing the excellent PowerShell App Deployment Toolkit) that executes each of the 12 Visual C++ Redistributable installers, one after another. With this I have finally managed to properly deploy this runtime for good. If in the future a new version of the runtime is released, it will be easy to adjust the script to accompany it.

This script is free to use and adapt to your own needs, though providing a link to this page would be appreciated. Likewise I'm grateful for any sort of feedback on this script, as this is my first dabble into writing application detection scripts for SCCM. Just reach out to me on Twitter @niemalsnever. Huge thanks go out to my buddy M., who helped me a great deal to come up with this script!

Mastodon