AnsweredAssumed Answered

Powershell and  APIv2

Question asked by David Blackburn on Sep 25, 2015
Latest reply on Nov 12, 2015 by josiah371

My question is: Did I miss something?

I've been trying to get something that is so simple to do with CURL into Powershell.


The API documents for CURL are pretty clear. This works great.

curl -s -k -H "X-Requested-With: curl app" "" > export.xml


I've read through the API documents and the Community posts, and many,many things brought up by Google on how to do this with Powershell, with much less success.

This Powershell code:


add-type @"
  using System.Net;
  using System.Security.Cryptography.X509Certificates;

  public class IDontCarePolicy : ICertificatePolicy {
  public IDontCarePolicy() {}
      public bool CheckValidationResult(
          ServicePoint sPoint, X509Certificate cert,
          WebRequest wRequest, int certProb) {
          return true;
[System.Net.ServicePointManager]::CertificatePolicy = new-object IDontCarePolicy

$username               = "q_userid"
$password               = "q_password"
$password_base64 = ConvertTo-SecureString $password -AsPlainText -Force
$cred                       = New-Object System.Management.Automation.PSCredential ($username, $password_base64)
$uri                          = ""
$headers                 = @{"X-Requested-With"="powershell"}

$response               = Invoke-RestMethod -uri $uri -cred $cred -headers $headers -method get


Returns this error:

Invoke-RestMethod : The remote server returned an error: (401) Unauthorized.

At D:\QualysGuard\curl.ps1:25 char:20

+ $response        = Invoke-RestMethod -uri $uri -cred $cred -headers $headers -me ...

+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException

    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand