0

I have the following code:

& $Plinkpath -P "22" -v "User@Server" -pw $passw $commands3 | Out-File $Report -Append -Encoding utf8

This outputs a file with the information needed but I want to store the plink sentence result into a variable.

I tried:

& $Plinkpath -P "22" -v "User@Server" -pw $passw $commands > $Example
$Example = $Plinkpath -P "22" -v "User@Server" -pw $passw $commands

And nothing works :(

How can I get the command output into a variable?

arco444
  • 22,002
  • 12
  • 63
  • 67
Quiron
  • 340
  • 2
  • 12
  • `$Example = plink username@hostname command` actually works. Plink is just a console application, there's nothing Plink-specific in your question. – Martin Prikryl Jun 05 '18 at 13:09
  • I tried `$Example = plink username@hostname command` and does not work – Quiron Jun 05 '18 at 13:20
  • `& $Plinkpath -P 22 -v "$usr@$Server" -pw $passw $Param[0] = $Param[1]` This worked for me! Thx for help! – Quiron Jun 06 '18 at 09:18

1 Answers1

1

Does Invoke-Expression work for you?

$example = Invoke-Expression "$Plinkpath -P '22' -v 'User@Server' -pw $passw $commands"

This should capture the command output into the $example variable.

Below is the cmdlet description:

PS > Get-Help invoke-expression

NAME
    Invoke-Expression

SYNOPSIS
    Runs commands or expressions on the local computer.


SYNTAX
    Invoke-Expression [-Command] <String> [<CommonParameters>]


DESCRIPTION
    The Invoke-Expression cmdlet evaluates or runs a specified string as a command and returns the results of the
    expression or command. Without Invoke-Expression , a string submitted at the command line would be returned
    (echoed) unchanged.
arco444
  • 22,002
  • 12
  • 63
  • 67
  • As a general rule, Invoke-Expression should be a last resort. – EBGreen Jun 05 '18 at 13:22
  • @EBGreen Not that I don't agree, but it would be useful to provide some information to back that statement up so that myself and other users can understand why it should be a last resort – arco444 Jun 05 '18 at 13:41
  • 1
    Oh, because it opens your code to code injection attacks. – EBGreen Jun 05 '18 at 13:42