I've got a function that I wrote that is part of a larger script. The issue I'm having doesn't seem specific to this function, but it's an example of what I'm seeing in general. I'm doing this through the ISE if that matters.
About half the time when I run the script some of the colors get overwritten during the display where sometimes the line being written to the screen will have part of the color from the previous line in it, and not always just at the beginning.
Example output
Program Name | Installed | Latest
---------------------------------------------
Program 1 | 8.4.5.196 | 8.4.5.196
The "Program Name" line is in white which is the way it should be. On he second line the word "Program" is in green like it should be, but the number "1" through "| 8.4.5.196 | 8.4"
is white and ".5.196" is back to green again like it should have been.
I'm assuming it has something to do with multithreading, but I'm not completely sure. I've tried inserting Start-Sleep -m 100 in parts of the script and it does help a bit but does not completely eliminate it and I'd like to not have to slow the
script down to fix it if there's another way around it.
Is there something I'm missing maybe? I'm self learning PS after previous experience with batch files, and C#.
Thanks
function CheckProductVersion ($FilePath, $ProgName, $Current)
{
$ProgName = $ProgName.PadRight(16,' ')
if (Test-Path $FilePath)
{
$result=" "
clv -name result
$result = (Get-Item $FilePath).VersionInfo.ProductVersion
$result = $result -replace "`t|`n|`r",""
$result = $result.PadRight(12,' ')
$Current = $Current.PadRight(12,' ')
if ($result -ne $Current)
{
write-host "$ProgName|`t$result|`t$Current" -foregroundcolor "red"
}
else
{
write-host "$ProgName|`t$result|`t$Current" -foregroundcolor "green"
} # End
}
else
{
write-host "$ProgName|`tNot Found`t|`t$Current" -foregroundcolor "white"
}
}
## Calling the function
write-host "$CompName Report:`n"
write-host "Program Name`t|`tInstalled `t|`tLatest" -foregroundcolor "white"
write-host "---------------------------------------------"
CheckProductVersion $Prog1FilePath "Program 1" $Prog1Current
CheckProductVersion $Prog2FilePath "Program 2" $Prog2Current
CheckProductVersion $Prog3FilePath "Program 3" $Prog3Current