Strange behaviour of read-host
hello, after hours fighting against powershell (v3.0 in win2012 , v4.0 in win8.1) have found strange behavior in script.
ask text user (using "$text = read-host") , append array 1 one. in middle of process have show additional information user, print in screen , use additional "read-host 'press enter'" before clean screen.
when show content of array have more items expect. why?
next script simplify version minimal lines:
#__________________________________________________________________________________________________ #__________________________________________________________________________________________________ function capturarnivel1(){ write-host " ------ strange error ------ " $tmptxt = read-host "`nintro text captured"; read-host "press enter or intro text. captured error" write-host "`nreturned value tmptxt=[$tmptxt]`n" # here captured value 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 this:
------ strange error ------ intro text captured: aaaaaaaa press enter or intro text. captured error: 11111111 returned value tmptxt=[aaaaaaaa] received value filtro=[11111111 aaaaaaaa] [11111111] [aaaaaaaa] ps c:\users\pablo\desktop\errorpowershell>
hi pablo,
if wondering "11111111" item in $ifiltros, that's due way function calls read-host. not storing output of second read-host variable or passing function pipeline, it's written output of function (a function returns output, not written behind return statement).
if want prevent that, can either store user input or null it. examples:
# 1) pass out-null read-host "press enter or intro text. captured error" | out-null # 2) store in $null $null = read-host "press enter or intro text. captured error" # 3) store in variable later use within function $optionalinput = read-host "press enter or intro text. captured error"
cheers,
fred
there's no place 127.0.0.1
Windows Server > Windows PowerShell
Comments
Post a Comment