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

Strange behaviour of read-host

$
0
0

Hello, after some hours fighting against PowerShell (v3.0 in Win2012 and v4.0 in Win8.1) I have found an strange behavior in my script.

I ask for a text to the user (using "$text = Read-Host") and I append it to an array one by one. In the middle of this process I have to show some additional information to the user, so I print it in screen and use an additional "Read-Host 'Press ENTER'" before clean the screen.

When I show the content of the array it have more items than I expect. Why?

Next script is a very simplify version with minimal lines:

#__________________________________________________________________________________________________
#__________________________________________________________________________________________________
function capturarNivel1(){
    Write-Host " ------ Strange error ------ "
    $tmptxt = Read-Host "`nIntro text to be captured";
    Read-Host "Press ENTER  or Intro text. IT IS CAPTURED BY ERROR"
    Write-Host "`nreturned value tmptxt=[$tmptxt]`n" # Here the captured value is correct
    return $tmptxt
    }
#__________________________________________________________________________________________________
#__________________________________________________________________________________________________
function main(){
    $lFiltros = @()
    Clear-Host

    $filtro = capturarNivel1
    Write-Host "`nreceived value filtro=[$filtro]`n"
    $lFiltros += $filtro
    foreach($s in $lFiltros){
        Write-Host "[$s]"
        }
    }
#__________________________________________________________________________________________________
#__________________________________________________________________________________________________

main

The output is this:

 ------ Strange error ------

Intro text to be captured: AAAAAAAA
Press ENTER  or Intro text. IT IS CAPTURED BY ERROR: 11111111

returned value tmptxt=[AAAAAAAA]


received value filtro=[11111111 AAAAAAAA]

[11111111]
[AAAAAAAA]
PS C:\Users\Pablo\Desktop\ErrorPowerShell>


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>