Reputation: 3
I have a piece of code in powershell which I use as an "installation recipe". I use this script to check that the preparation of the PCs is good and that the various software are installed correctly. Set-ExecutionPolicy -ExecutionPolicy Unrestricted -Force
If ((Test-Path "C:\Program Files\7-Zip") -eq $True)
{Write-Host " ~ 7-ZIP : Installation => OK! ~" -ForegroundColor Green}
else{Write-Host " ~ 7-ZIP : Installation => NOK! ~" -ForegroundColor Red}
Sleep 3
If ((Test-Path "C:\Program Files (x86)\Adobe\Acrobat Reader DC") -eq $True)
{Write-Host " ~ Adobe Reader DC : Install => OK! ~" -ForegroundColor Green}
else{Write-Host " ~ Adobe Reader DC : Install => NOK! ~" -ForegroundColor Red}
exit
If the installation is OK (OK), then it generates a value that we store and then export to a .CSV or .XLSX file. Ditto if the installation is not OK (NOK).
How do you do that?
Thanks for your help
Upvotes: 0
Views: 132
Reputation: 61208
A Hashtable as shown in VonPryz answer to collect the names and paths of the software to test is indeed the easiest way of dealing with this.
From the questions title,you would like a CSV file with the results of the tests; not only colored console output. For that, you need to loop through the list of software and output objects you collect in a variable like below:
# add as many items here as you would like to test
$software = @{
"7-Zip" = "C:\Program Files\7-Zip"
"Adobe Reader DC" = "C:\Program Files (x86)\Adobe\Acrobat Reader DC"
}
# a template line for output to console
$message = ' ~ {0} : Install => {1}! ~'
# loop through the hashtable Keys
$result = $software.Keys | ForEach-Object {
$installed = Test-Path -Path $software[$_]
if ($installed) { $color = 'Green'; $success = 'OK' }
else { $color = 'Red'; $success = 'NOK' }
# output to console
Write-Host ($message -f $_, $success) -ForegroundColor $color
# output an object to save as CSV
[PsCustomObject]@{
'Software' = $_
'Path' = $software[$_]
'IsInstalled' = $installed
}
}
# output result to console as table
$result | Format-Table -AutoSize
# output result as CSV file
$result | Export-Csv -Path 'D:\Test\InstalledSoftware.csv' -NoTypeInformation
Output of $result
on screen as Table:
Software Path IsInstalled -------- ---- ----------- 7-Zip C:\Program Files\7-Zip False Adobe Reader DC C:\Program Files (x86)\Adobe\Acrobat Reader DC True
Upvotes: 0
Reputation: 24081
One approach is to save each non-installed software's name into an array for later processing.
That being said, testing paths the way in question could be improved. Instead of typing paths here and there, store them in a collection for easy processing. A hashtable works fine. Like so,
$ht = @{
"7-Zip" = "C:\Program Files\7-Zip"
"Adobe Reader DC" = "C:\Program Files (x86)\Adobe\Acrobat Reader DC"
"FooApp" = "C:\Program Files\FooApp"
}
$failedInstalls = @()
foreach($key in $ht.keys){
if(test-path $ht[$key] ) {
Write-Host " ~ $key : Installation => OK! ~" -ForegroundColor Green
} else {
Write-Host " ~ $key : Installation => NOK! ~" -ForegroundColor Red
$failedInstalls += $key
}
}
$failedInstalls
What's done here is that software names and paths are stored in a hash table. So, one central location for all the paths. Then the collection is iterated and each missing software is added to $failedInstalls
array. Changing number of software is trivial, it only requires change in the hash table - no need for if(test-path...
statement for each piece of software.
As how to export array as an XSLX or CSV is left as an exercise to the reader.
Upvotes: 1