Array with mutiple apps reports wrong output

$approvedApplications = @("Notepad++ (64-bit x64)","brave2typo")
$booapprovedApplications = $false

function Get-InstalledApps {
    if (![Environment]::Is64BitProcess) {
        $arrRegistryPaths = @('HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
                              'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*')
    }
    else {
        $arrRegistryPaths = @('HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
                              'HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
                              'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
                              'HKCU:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*')
    }

    $arrUninstallRegistrations = @()
    foreach ($registryPath in $arrRegistryPaths) {
        if (Test-Path $registryPath) {
            $arrUninstallRegistrations += Get-ItemProperty $registryPath
        }
    }

    $arrInstalledApps = @()
    foreach ($uninstallRegistration in $arrUninstallRegistrations) {
        if ($uninstallRegistration.DisplayName -ne $null) {
            $arrInstalledApps += $uninstallRegistration.DisplayName
        }
    }
    return $arrInstalledApps
}

$arrInstalledApplications = Get-InstalledApps
 foreach ($installedApplication in $arrInstalledApplications) {
    foreach ($ApprovedApplication in $approvedApplications) {
        if ($installedApplication -eq $ApprovedApplication) {
            $booapprovedApplications = $true}
            #else {$booapprovedApplications = $False}
        }
    }


if ($booapprovedApplications) {
    $appStatus = @{"Installation status" = "approved app installed"}
} 
else {
    $appStatus = @{"Installation status" = "Not all approved apps installed"}
}

return $appStatus

Hi
I’m trying to check for multiple approved apps and if they are installed it reports “compliant/approved app installed” if not it reports “not all approved apps installed”
when I use the array with one app it works as expected reporting as “compliant” when i enter more than one app into the array with typo(intentionally)like the example it reports compliant
what am I missing here?
Thanks

It depends on where in your list you have your not approved app. :wink: I’d recommend the following modifcation:

$booapprovedApplications = 0

$arrInstalledApplications = Get-InstalledApps
foreach ($installedApplication in $arrInstalledApplications) {
    foreach ($ApprovedApplication in $approvedApplications) {
        if ($installedApplication -eq $ApprovedApplication) {
            $booapprovedApplications ++
        }
    }
}

if (-not $booapprovedApplications) {
    $appStatus = @{"Installation status" = "approved app installed" }
} 
else {
    $appStatus = @{"Installation status" = "Not all approved apps installed" }
}

… the rest stays as it is.

First thanks alot for your help
second i tried it but if i had only one installed it will still report there is value on variable(like 1 out of 2) and report “app installed”
so i changed the check to check for value 2(in this example where there are only two apps) and that seems to work so if the value doesnt add up to the no. of apps then not compliant if it does then compliant

Hmmm … I guess I missunderstood your approach. I thought you wanted to check if there are unapproved apps at all. But it’s great if you found a working solution. :+1:t4: :slightly_smiling_face: