Sorry if my question is trivial; I'm a beginner with PowerShell.
My program (plink) is expecting for an answer from the keyboard and will return some output. Is it possible to redirect the output to a variable/file?
Details:
(I am assigning the command, itself, to a variable, for convenience.)
Without output redirection — Console output is OK:
PS C:\Users\user> $mycmd="plink -v -ssh -l $user $myserver vncserver -list"
PS C:\Users\user> invoke-expression $mycmd
user@server's password:
TigerVNC server sessions:
X DISPLAY # PROCESS ID
:1 26788
PS C:\Users\user>
However with output redirection in to a variable:
PS C:\Users\user> $myoutput=invoke-expression "$mycmd"
...still waiting in this state ==> I type my passwd blindly (passwd prompt is captured in $myoutput)
PS C:\Users\user> $myoutput
user@server's password:
TigerVNC server sessions:
X DISPLAY # PROCESS ID
PS C:\Users\server>
$myoutput variable contents is OK But the password prompt is redirected in to the variable too !
My pbm now is how to make usage comprehensive :user can't guess when typing his passwd :( (i.e. how to handle the password prompt?)
Remarqs: verbose mode of plink (plink -v ...) still outputting to the console when redirecting to $myoutput
$myoutput=invoke-expression "$mycmd"
Looking up host "server"
Connecting to xxx.XXX.XXX.XXX port 22
We claim version: SSH-2.0-PuTTY_Release_0.66
Server version: SSH-2.0-OpenSSH_7.4
Using SSH protocol version 2
Doing Diffie-Hellman group exchange
Doing Diffie-Hellman key exchange with hash SHA-256
Host key fingerprint is:
ssh-rsa 2048 blabklablablablabla
Initialised AES-256 SDCTR client->server encryption
Initialised HMAC-SHA-256 client->server MAC algorithm
Initialised AES-256 SDCTR server->client encryption
Initialised HMAC-SHA-256 server->client MAC algorithm
Using username "user".
Using SSPI from SECUR32.DLL
Attempting GSSAPI authentication
GSSAPI authentication initialisation failed
The target was not recognized.
I enter my passwd here (in the dark)
Sent password
Access granted
Opening session as main channel
Opened main channel
Started a shell/command
Server sent command exit status 1
Disconnected: All channels closed
plink
. Are you prepared to handle the first line of$myoutput
being the password prompt? (2) Since the password prompt is part of the output fromplink
, you should expect it to go into$myoutput
and not to the console. When you type$myoutput=invoke-expression "$mycmd"
, do you get another PS prompt, or does it just seem to hang? If it just seems to hang, it may be silently reading the password from the keyboard! Try typing the password. (Warning: it may display on the console screen.)PS C:\Users\user> $myoutput=invoke-expression "$mycmd"
.....still waiting in this state ==> I type passwd blindly (prompt passwd captured into $myoutput)PS C:\Users\user> $myoutput _user@server's password:_ TigerVNC server sessions: X DISPLAY # PROCESS ID PS C:\Users\server
$myoutput contents is OK But my pbm now is how to make usage more freindly: user cant't guess when typing his passwd :( Remarqs: verbose mode of plink (plink -v) still outputting to the console when redirecting to $myoutput