Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

Incorrect colours displayed by Write-Host

$
0
0

Much of my PowerShell is reporting and I like to use coloured output to make the results easier to interpret.  I have on numerous occasions run into a problem with the wrong colours being displayed by Write-Host (no lectures please about not using Write-Host).  I am working in PowerShell ISE.

For instance:

$fgcolor = 'Green'
Write-Host "Some text in green?" -ForegroundColor $fgcolor

However it will actually display the text in some other colour.

When I check the value of the variables they are correct.

I have searched online on many occasions but I don't find any reports of anyone else being troubled.  My normal work around is to output in HTML and view using a web browser, but that's really not a solution. 

Can anyone give me any ideas how I might troubleshoot this further?

Below is my actual code and a picture of some example output:

Function ReportStatusOfAccounts {
    Write-Host "Pre-Report of all user accounts in the OU $OU"
    Foreach ($record in $global:allusers) {
        $Rname = $Rlastlogondate = $Rpasswordlastset = $Rhomedirectory = $Rprofilepath = '' # Set them all to blank so we don't get variable value 'bleed'
        $Rname = $record.name
        $Rlastlogondate = $record.lastlogondate ; 
        $Rpasswordlastset = $record.passwordlastset
        $Rhomedirectory = $record.homedirectory
        $Rprofilepath = $record.profilepath
        $global:lldColor = $global:plsColor = 'Black'
        if ($($record.lastlogondate) -lt $90Days ) { $global:lldColor = 'Green' }
        if ($($record.lastlogondate) -gt $90Days ) {  $global:lldColor = 'Red' }
        if (-not $Rlastlogondate) { $Rlastlogondate = "No Value              " ; $global:lldColor = 'DarkYellow' }
        if ($($record.passwordlastset) -lt $90Days ) {  $global:plsColor = 'Green' }
        if ($($record.passwordlastset) -gt $90Days ) {  $global:plsColor = 'Red' }
        Write-Host "User " -nonewline -ForegroundColor $global:normalTextColor
        Write-Host "$($record.name)`t" -nonewline -ForegroundColor $global:variableTextColor
        Write-Host "`tLast Logon Date: " -nonewline -ForegroundColor $global:normalTextColor
        Write-Host "$Rlastlogondate $global:lldColor" -nonewline -ForegroundColor $global:lldColor
        Write-Host "`tPassword Last Set: " -nonewline -ForegroundColor $global:variableTextColor
        Write-Host "$Rpasswordlastset $global:plsColor" -ForegroundColor $global:plsColor
    }
}

You can see values on the last line should be showing in DarkYellow and Red, but are both in Green

Other note: Use of some colours seems to 'trigger' this behaviour.  Up until I tried using 'DarkYellow' this was reporting in Red and Green without problem.  However removing the DarkYellow reference (and restarting my 'ISE session) do not cause the behaviour to revert to correct.



Viewing all articles
Browse latest Browse all 21975

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>