I have a Powershell function that formats out put from another function I have to gather version, path and file information based on some registry queries. It seems all the data is being stored to one variable and only printing one line altogether. Below is my code, the output and an explanation of what I need in the desired output.
Code:
function getOffice()
{
$list = new-object System.Collections.Generic.List[PSCustomObject];
$found_paths = @();
$final_paths = @();
$uninstall_keys = getRegistrySubkeys "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall" '\\Office[\d+.*]';
if ($uninstall_keys -ne $null)
{
foreach ($key in $uninstall_keys)
{
$product_name = getRegistryValue $key "DisplayName";
$version = getRegistryValue $key "DisplayVersion";
$base_install_path = getRegistryValue $key "InstallLocation";
$full_install_path = (Get-ChildItem -Directory -LiteralPath $base_install_path | Where-Object Name -match '^Office\d{1,2}\D?').FullName;
$found_paths += ,$full_install_path;
$exes = @("MSOCF.DLL", "access.exe", "word.exe", "wordCnv.exe", "WordViewer.exe", "Excel.exe", "ExcelCnv.exe", "ExcelViewer.exe", "PowerPoint.exe",
"PowerPointViewer.exe", "PowerPointCnv.exe", "Publisher.exe", "Project.exe", "OneNote.exe", "InfoPath.exe Groove.exe", "FrontPage.exe",
"SharePointDesigner.exe", "Visio.exe", "VisioViewer.exe", "Lync.exeOutlook.exe", "WINPROJ.EXE");
foreach($path in $found_paths)
{
foreach($exe in $exes)
{
$found_files = Get-Item ([System.IO.Path]::Combine($path, $exe)) -EA Ignore;
$found_file = $found_files.Name;
if ($found_file)
{
$office = makeFileResult $found_file, $path, $version, $product_name, $version;
$list.Add($office);
$list
}
}
}
}
}
}
function makeFileResult($name, $path, $file_version, $product_name, $product_version)
{
return [PSCustomObject]@{
path = $path;
file = $name;
product_version = $product_version;
product_name = $product_name;
file_version = $file_version;
};
}
getOffice
Output:
path :
file : {MSOCF.DLL, C:\Program Files\Microsoft Office\Office15, 15.0.4569.1506, Microsoft Project Standard 2013...}
product_version :
product_name :
file_version :
path :
file : {WINPROJ.EXE, C:\Program Files\Microsoft Office\Office15, 15.0.4569.1506, Microsoft Project Standard 2013...}
product_version :
product_name :
file_version :
path :
file : {MSOCF.DLL, C:\Program Files\Microsoft Office\Office15, 15.0.4569.1506, Microsoft Project Standard 2013...}
product_version :
product_name :
file_version :
path :
file : {WINPROJ.EXE, C:\Program Files\Microsoft Office\Office15, 15.0.4569.1506, Microsoft Project Standard 2013...}
product_version :
product_name :
file_version :
I am not sure why all the data is only being stored in the `file` key here.
I am not sure why all the data is only being stored in the file key here.
Desired Output:
path : C:\Program Files\Microsoft Office\Office15
file : MSOCF.DLL
product_version : 15.0.4569.1506
product_name : Microsoft Project Standard 2013
file_version : 15.0.4569.1506
path : C:\Program Files\Microsoft Office\Office15
file : WINPROJ.EXE
product_version : 15.0.4569.1506
product_name : Microsoft Project Standard 2013
file_version : 15.0.4569.1506
foreachloops and do one value at a time and observe the results. Using a debugger helps too.