Thanks for the update! Sometimes calling the native tools turns out to be the "easiest" way to go, in cases like that I usually make sure the interface is as clean as possible.
I've not actually integrated into my scripts, but as a proof of concept, it works.
A quick test shows me that something like this seems to work:
use IPC::Run3 'run3';
run3 ['powershell', '-Command', 'Invoke-WebRequest', '-Uri', $uri,
'-OutFile', $filename, '-UseDefaultCredentials'];
$? and die "run3 failed with \$?=$?";
(I'm using IPC::Run3 because newer versions use Win32::ShellQuote under the hood, and it allows redirection of STDIN/OUT/ERR if desired. For really complicated cases, I guess PowerShell's -EncodedCommand is probably the way to go.)
Update: Wrapped up in a function:
use File::Temp qw/tempfile/;
use IPC::Run3 'run3';
sub invoke_webrequest {
my $uri = shift;
my ($tfh,$tfn) = tempfile(UNLINK=>1);
close $tfh;
run3 ['powershell', '-Command', 'Invoke-WebRequest', '-Uri', $uri,
'-OutFile', $tfn, '-UseDefaultCredentials'];
$? and die "run3 failed with \$?=$?";
open my $fh, '<', $tfn or die "$tfn: $!";
my $data = do { local $/; <$fh> };
close $fh;
unlink $tfn;
return $data;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.