0

The first time my PSMenu function is called, the formatting appears correct. After the first loop iteration, the formatting somewhat randomly changes. It seems local to the first few "columns" of text. If you type enough $input sometimes the formatting will return to normal.

Things I have tried:

  • multiple clear-hosts at beginning and end of loop
  • adding a start-sleep .2 right before the loop repeats

I have figured out that the problem is with clear-host. I have tried using "Console.clear() and [System.Console]::Clear()" but neither work. I receive the error :

At line:3 char:20
+      console.clear()
+                    ~
An expression was expected after '('.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ExpectedExpression

Here is a snippet example of the the format part of my code:

Function PSMenu{
    Clear-Host
    $title= 'Example REMOVAL TOOL'
    $line = '_________________________________________'
    $t='    '
   
 
    write-host "        $t$line$t         " -BackgroundColor gray -ForegroundColor black;
    Write-Host "        $t$t $title $t $t                   " -BackgroundColor yellow -ForegroundColor black

    write-host "        $t$line$t         " -BackgroundColor gray -ForegroundColor black; 
    Out-String;Out-String;
    
    Write-Host "Give me some text and I will repeat this loop .('q' to quit.)" -ForegroundColor yellow
    Write-Host "Computers cleansed: $successList `n`n"


}

Do{
    PSMenu;
    $input = Read-host "Enter some text"
    write-host "Now I am doing something"
    pause

}Until($input -eq 'q')
Zero
  • 1
  • 1

1 Answers1

0

It appears to be fixed by just adding a sleep at the beginning of the function. Sometimes switching colors takes longer than expected. Although, I am not really sure why this worked. I just know it did.

Function PSMenu{
    Clear-Host
    $title= 'Example REMOVAL TOOL'
    $line = '_________________________________________'
    $t='    '
   sleep -Milliseconds 100
 
    write-host "        $t$line$t         " -BackgroundColor gray -ForegroundColor black;
    Write-Host "        $t$t $title $t $t                   " -BackgroundColor yellow -ForegroundColor black

    write-host "        $t$line$t         " -BackgroundColor gray -ForegroundColor black; 
    Out-String;Out-String;
    
    Write-Host "Give me some text and I will repeat this loop .('q' to quit.)" -ForegroundColor yellow
    Write-Host "Computers cleansed: $successList `n`n"


}

Do{
    PSMenu;
    $input = Read-host "Enter some text"
    write-host "Now I am doing something"
    pause

}Until($input -eq 'q')
Patrick Mcvay
  • 2,221
  • 1
  • 11
  • 22