Powershell piping to variable and write-host at the same time
Asked Answered
A

3

7

Hello all and thanks for your time in advance;

I'm running into a slight issue.

I'm running a command and piping it into a variable so i can manipulate the output.

$variable = some command

this normally works fine but doesn't output what's happening to the screen, which is fine most of the time. However occasionally this command requires some user input (like yes or no or skip for example), and since it's not actually piping anything to the command window, it just sits there hanging instead of prompting the user. If you know what to expect you can hit y or n or s and it'll proceed normally.

Is there anyway to run the command so that the output is piped to a variable AND appears on screen? I've already tried:

($variable = some command) 

I've also tried:

write-host ($variable = some command) 

But neither work. Note that the command running isn't a native windows or shell command and I cannot just run it twice in a row.

To clarify (because i probably wasn't clear)

I've also tried :

$variable = some command : Out-host

and

$variable = some command | out-default

with all their parameters, But the "prompt" from the command (to write y, n, s) doesn't show up.

Being able to pass S automatically would also be acceptable.

Adenoidal answered 27/5, 2015 at 4:40 Comment(0)
I
9

Sounds like you need Tee-Object. Example:

some command | Tee-Object -Variable MyVariable

This should pass everything from the command down the pipe, as well as store all output from the command in the $MyVariable variable for you.

Interaction answered 27/5, 2015 at 5:32 Comment(1)
Thanks for the response, Unfortunately that didn't work either. if i do it exactly like you stated, it reports "Cannot bind to parmeter because it is null. If i () the command, it performs the same behavior as before (no prompt), even though it's actually running the command correctly. Edit: I tried setting up the variable in advance with a false value, and it DOES get overwritten by the command output, but the prompts still don't show up.Adenoidal
O
1

You need to give some specific example that doesn't work. I tried this and it works:

function test { $c = read-host "Say something"; $c }
$x = test

I still see "Say something". read-host does not output to standard output so your problem is surprising. Even this works:

read-host "Say something" *> out

=== EDIT ===

Since this is interaction with cmd.exe you have two options AFAIK. First, test command:

test.cmd

@echo off
set /p something="Say something: "
echo %something%

This doesn't work as you said: $x= ./test.cmd

To make it work:

a) Replace above command with: "Say something:"; $x= ./test.cmd. This is obviously not ideal in general scenario as you might not know in advance what the *.cmd will ask. But when you do know its very easy.

b) Try this:

Start-transcript test_out; 
./test.cmd; 
Stop-transcript;
gc .\test_out | sls 'test.cmd' -Context 0,1 | select -Last 1 | set y
rm test_out 
$z = ($y -split "`n").Trim()

After this $z variable contains: Say something: something. This could be good general solution that you could convert to function:

$z = Get-CmdOutput test.cmd

Details of text parsing might be slightly different in general case - I assumed here that only 1 question is asked and answer is on the same line but in any case with some work you will be able to get everything cmd.exe script outputed in general case:

=== EDIT 2 ===

This might be a better general extraction:

$a = gi test_out; rm test_out
$z = $a | select -Index 14,($a.count-5)
$z
Osier answered 27/5, 2015 at 9:59 Comment(1)
The command is a .cmd command from baseline tools in our company. The issue isn't the regular output, the problem is when there's a prompt that basically says "Yes, No, Edit, Skip" , that prompt doesn't show up if i pipe the command to a variable, it DOES show up if i trigger it from powershell without piping it. So basically i want to mimic the behavior of calling it without piping it, but piping it at the same time. And Tee and | out-host or |out-default haven't worked. I don't care if the prompt is added to the variable, i just need it to show up on the commandline window.Adenoidal
F
0
$variable = ""
some command | % {$variable += $_;"$_"}

This executes the command, and each line of output is both added to $variable and printed to the console.

Fighter answered 16/3, 2021 at 22:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.