I need to output the property of the Variable DisplayVersion which contains the version of the application in the result how do I do that in foreach loop?
$programList = @(
'SAP'
'Tanium'
'Sentinel'
'DisplayLink'
'Cisco AnyConnect'
'Adobe Acrobat Reader'
'Google'
'Lotus Notes'
'Java Runtime'
'My IT Windows'
'Qualys'
'Snow'
'LogRhythm'
)
$Regpath = @(
'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
)
$installedPrograms = (Get-ItemProperty $Regpath).where({$_.DisplayName})
$result = foreach($program in $programList)
{
$check = $installedPrograms.DisplayName -match $program
if($check)
{
foreach($match in $check)
{
[pscustomobject]@{
Program = $program
Status = 'Found'
Match = $match
}
}
continue
}
[pscustomobject]@{
Program = $program
Status = 'Not Found'
Match = $null
version = $null
}
}
$result
The output should look something like
Program Status Match Version
------- ------ ----- -------
SAP Found SAP GUI for Windows 7.60 (Patch 4) 7.60.4
CodePudding user response:
Here is how you can approach the logic for your code, making use of Group-Object -AsHashtable
and a maybe a PowerShell class
(personal preference here).
$programList = @(
'SAP'
'Tanium'
'Sentinel'
'DisplayLink'
'Cisco AnyConnect'
'Adobe Acrobat Reader'
'Google'
'Lotus Notes'
'Java Runtime'
'My IT Windows'
'Qualys'
'Snow'
'LogRhythm'
)
$Regpath = @(
'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*'
'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'
)
$installedPrograms = (Get-ItemProperty $Regpath).where({ $_.DisplayName }) |
Group-Object DisplayName -AsHashTable -AsString
class Thing {
[string] $Program
[string] $Status = 'Not Found'
[string] $Match
[string] $Version
Thing() { }
Thing([string] $Program, [string] $DisplayName, [string] $Version) {
$this.Program = $Program
$this.Status = 'Found'
$this.Match = $DisplayName
$this.Version = $Version
}
}
$result = foreach($program in $programList) {
if($keys = $installedPrograms.Keys -match [regex]::Escape($program)) {
foreach($key in $keys) {
$match = $installedPrograms[$key]
[Thing]::new($program, $match.DisplayName, $match.DisplayVersion)
}
continue
}
[Thing]@{ Program = $program }
}
CodePudding user response:
Getting the version just match the displayname back to its corresponding object $installedPrograms.Where({$PSItem.DisplayName -eq $match}).DisplayVersion
[pscustomobject]@{
Program = $program
Status = 'Found'
Match = $match
Version = $installedPrograms.Where({$PSItem.DisplayName -eq $match}).DisplayVersion
}