0

I run this curl command on PowerShell.

curl.exe -0 -v --cert $cert --key $key --cacert $cacert GET $URIString

I am able to get the output when everything goes right but I want to get its "log" e.g.

% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:--  0:00:01 --:--:--     0* Could not resolve host: GET
* Closing connection 0
curl: (6) Could not resolve host: GET
*   Trying XXX.XXX.XXX.XXX...
* TCP_NODELAY set
* Connected to xxxxxxxxxx (XXX.XXX.XXX.XXX) port 443 (#1)
* ALPN, offering http/1.1
* unable to set private key file: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX' type PEM
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
* Closing connection 1
curl: (58) unable to set private key file: 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXX' type PEM 

This is for error handling.

GitGawd
  • 53
  • 9

2 Answers2

1

When calling external programs from PowerShell, you might want to use the Start-Process cmdlet to gain some control over it. You could evaluate the exit code for example. And further more, you gain access to its standard output stream and - I think that's more important to you - its standard error stream. If you call curl like this:

Start-Process curl -ArgumentList "-0 -v --cert $cert --key $key --cacert $cacert GET $URIString" -Wait -RedirectStandardOutput $env:USERPROFILE\Desktop\stdout.txt -RedirectStandardError $env:USERPROFILE\Desktop\stderr.txt

you can afterwards evaluate both streams, stdout and stderr.

If you also want to check the exit code, you can call curl like this:

$process = Start-Process curl -ArgumentList "-0 -v --cert $cert --key $key --cacert $cacert GET $URIString" -Wait -RedirectStandardOutput $env:USERPROFILE\Desktop\stdout.txt -RedirectStandardError $env:USERPROFILE\Desktop\stderr.txt -PassThru
Write-Host "Exit code: $($process.ExitCode)"
stackprotector
  • 10,498
  • 4
  • 35
  • 64
0

Unfortunately I couldn't find a good solution either. The only thing I could capture is the HTML output and I guess you're getting the same.

Maybe you could try PowerShell's native Invoke-Webrequest command. However it doesn't support as many options as curl, so maybe you'll have to to some tinkering. This question for example shows how to replace the --cacert flag. The --key flag and --cert can be replaced by a PFX file, according to this answer. Everything else should have native support.

With this you could get the output from the powershell command by just saving it to a variable or output it to a file.

sIPeye
  • 28
  • 5