0

I am running a Perl script that calls a powershell script. Is there a way for me to get a return value from this? I've tried something similar to

my @output = "powershell.exe powershellscript.ps1;
foreach(@output)
{
   print $_;
}

and I get nothing out of it. Do I need to add something to the powershell script to push the return values?

3 Answers 3

3

Try with backsticks,

my @output = `powershell.exe powershellscript.ps1`;
foreach (@output)
{
   print $_;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I went witht his one and it worked great. Thanks everyone for the quick responses.
1

Try using a named pipe:

open(PWRSHELL, "/path/to/powershell.exe powershellscript.ps1 |") or die "can't open powershell: $!";
while (<PWRSHELL>) {
    # do something
}
close(PWRSHELL) or warn "can't close powershell: $!";

Comments

1

Use backticks to run and collect the output:

my @output = ` ...`

If you want the return code (status) too, do (by example):

perl -e '@output=`/bin/date`;print $?>>8,"\n";print "@output"'

See system for more information on interpreting the return code.

ADDENDUM

In lieu of backticks, which can sometimes be annoying to read, you can use qx(STRING) as described in perlop. This is analogous to collecting process output in shell scripts where, in POSIX-compliant shells, capture may be done with the archaic backticks, or more readably, with OUTPUT=$(/bin/date).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.