1

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?

3
  • $Example = plink username@hostname command actually works. Plink is just a console application, there's nothing Plink-specific in your question. Commented Jun 5, 2018 at 13:09
  • I tried $Example = plink username@hostname command and does not work Commented Jun 5, 2018 at 13:20
  • & $Plinkpath -P 22 -v "$usr@$Server" -pw $passw $Param[0] = $Param[1] This worked for me! Thx for help! Commented Jun 6, 2018 at 9:18

1 Answer 1

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.
Sign up to request clarification or add additional context in comments.

3 Comments

As a general rule, Invoke-Expression should be a last resort.
@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
Oh, because it opens your code to code injection attacks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.