0

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
3
  • (1) First of all, you should be aware that the password prompt is part of the output from 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 from plink, 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.) Commented Jan 1, 2018 at 22:41
  • I have rewritten my guidance as an answer. Commented Jan 2, 2018 at 18:12
  • Thanks G-Man, nice hint! Indeed, it's not hanging, it's waiting :) 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
    – Free Man
    Commented Jan 2, 2018 at 18:36

1 Answer 1

0

The password prompt is part of the output from plink, so you should expect it to go into $myoutput and not to the console.  I expect that, when you type $myoutput=invoke-expression "$mycmd", it just seems to hang, and doesn’t give you another PS prompt.  It is probably silently reading the password from the keyboard!  If you just go ahead and type the password, it will work.  (Warning: the password may display on the console screen.)

As mentioned above, since the password prompt is part of the output from plink, it goes into $myoutput.  So, you need to be prepared to handle the first line of $myoutput being the password prompt.

1
  • Exact, that's the question now: how to handle this first line of $myoutput to diplay the password prompt ... Thanks very much G-man, you make my question progressing
    – Free Man
    Commented Jan 2, 2018 at 18:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .