none
Powershell Script for software detection method. Improvements / Criticisms?

    General discussion

  • I wrote a powershell script for use as a detection method to solve a few work dilemmas.

    My aim was also to help other technicians in my Enterprise utilize this who also may not know any powershell.

    I'm just 1 year learning PS.

    #Ed Radke - SCCM Reusable Software Detection Method
    
    #See "To use a custom script to determine the presence of a deployment type"
    #https://docs.microsoft.com/en-us/previous-versions/system-center/system-center-2012-R2/gg682159(v=technet.10)#BKMK_Step4
    
    #Outputs a message "Installed" and exit code 0, if all tested installed software versions are greater or equal to the versions being tested.
    #Outputs null and exit code 0, if any version is less than the versions being tested, or does not exist.  This could also mean the software name does not match "EXACTLY".
    
    #Multiple software names can be tested, any one that fails, then the "Detection Method" fails
    
    #Follow the example for specifying multiple softwares, notice the quotes and commas.
    #For a single software name, on a single line just omit the comma.
    
    #Software names and versions can be obtained from Add/Remove Programs but to lessen the risk of a typo occurring browse the registry un-install paths and copy/paste the info from there.
    
    #Software test-list arrays
    [string[]]$SoftwareNameTestList = @(
    "Product 1 Name",
    "Product 2 Name",
    "Product 3 Name",
    "Product 4 Name"
    )
    [string[]]$SoftwareVersionTestList = @(
    "Product 1 Version",
    "Product 2 Version",
    "Product 3 Version",
    "Product 4 Version"
    )
    
    ##### DO NOT EDIT BELOW ####
    $ErrorActionPreference = 'SilentlyContinue'
    $Script:DetectionSuccessful = $Null
    $Script:SoftwareVersionTable = $Null
    
    #All possible registry paths containing un-install info
    [string[]]$RegistryUninstallPaths = @(
         'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall',
         'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall'
    )
    
    #If test-list array element counts don't match, fail
    if (($SoftwareNameTestList.Count) -eq ($SoftwareVersionTestList.Count))
    {
         
         #With each registry path build a table of installed software
         foreach ($RegistryPath in $RegistryUninstallPaths)
         {
              $RegistryFolders = (Get-ChildItem $RegistryPath -Name)
              $SoftwareVersionTable += $(foreach ($Folder in $RegistryFolders){(Get-ItemProperty -Path "$RegistryPath\$Folder") | Select-Object DisplayName,DisplayVersion })
         }
    
         #Step through each test-list element reformat version data
         for ($G = 0; $G -lt ($SoftwareNameTestList.Count); $G++)
         {
              $SoftwareNameTestItem = $SoftwareNameTestList[$G]
              $SoftwareVersionTestItem = $SoftwareVersionTestList[$G]
              $SoftwareVersionTestItem = [System.Version]$SoftwareVersionTestItem
              $SoftwareNameTestItemExists = $False
    
              #Step through each software installed element reformat version data
              foreach ($Record in $SoftwareVersionTable)
              {
                   $SoftwareNameInstalled = ($Record.DisplayName)
                   $SoftwareVersionInstalled = ($Record.DisplayVersion)
                   $SoftwareVersionInstalled = [System.Version]$SoftwareVersionInstalled
    
                   #Compare test-list software name and version number against installed software
                   #Installed software version must be >= the test-list version, this prevents detection method failure if the installed software is newer than that being installed.
                   #At first failure break out of loop, detection has failed
                   if ($SoftwareNameInstalled -eq $SoftwareNameTestItem)
                   {
                        $SoftwareNameTestItemExists = $True
                        if (($SoftwareVersionInstalled -ge $SoftwareVersionTestItem) -and (!($DetectionSuccessful -eq $False)))
                        {
                             $DetectionSuccessful = $True
                        }
                        else
                        {
                             $DetectionSuccessful = $False
                        }
                        break
                   }
              }
    
              if ($SoftwareNameTestItemExists -eq $False)
              {
                   $DetectionSuccessful = $False
                   break
              }
         }
    }
    else
    {
         $DetectionSuccessful = $False
    }
    
    if ($DetectionSuccessful)
    {
         Write-Output "Installed"
    }
    exit 0


    • Edited by Edwierd Wednesday, June 20, 2018 5:41 PM
    • Changed type Edwierd Wednesday, June 20, 2018 5:42 PM
    Tuesday, June 19, 2018 10:09 PM

All replies

  • Hi Edwierd,

    Personally, it's great.

    Thank you for sharing your codes, I think it could also help others.

    Best regards,

    Yuxiang


    Please remember to mark the replies as answers if they help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, June 20, 2018 7:55 AM