2

I wanted to make a nice GUI for my little script. I've this post describing a function how to do it.

function Write-Color([String[]]$Text, [ConsoleColor[]]$Color) {
    for ($i = 0; $i -lt $Text.Length; $i++) {
        Write-Host $Text[$i] -Foreground $Color[$i] -NoNewLine
    }
    Write-Host
}

Write-Color -Text Red,White,Blue -Color Red,White,Blue

It generally works fine but it doesn't like when there are spaces. I wanted to make sure the output is good looking so I've added some spaces..

enter image description here

Write-Color -Text "`t`t`SQL Connectivity:   ", "Verified ($gSerlServer\$gServerSqlDB)" -Color White, Green

Is there a way so that it would include the spaces and not act weirdly (at least from my point of view?). The funny thing is it sometimes display it correctly, and sometimes it doesn't. I don't have a clue why?

Community
  • 1
  • 1
MadBoy
  • 10,824
  • 24
  • 95
  • 156
  • What is your PowerShell version and where are you running this script. I am having a hard time reproducing your output. – Matt Oct 08 '15 at 01:43
  • Windows 2012 R2, PowerShell ISE,PSVersion 4.0. I've seen this on multiple occasions, not only with spaces. When I think about it with Start-Sleep working it must be something with the output delays. – MadBoy Oct 08 '15 at 06:46

1 Answers1

1

I've no idea why this works but adding Start-Sleep before executing colors solves the problem

Start-Sleep -Milliseconds 30
# Write-Color goes here... 

So for every new display after clearing screen (going thru menus) I actually set the Start-Sleep. It's like execution is too fast with display before colors catch up the the screen.

MadBoy
  • 10,824
  • 24
  • 95
  • 156