Calling URL Exports Programmatically - Returns (401) Unauthorized response

557 views Asked by At

I am trying to call service now URL programmatically but i receive 401 unauthorized error. Interestingly when i call servicenow webservice url in chrome browser with one login session open , i can successfully download the file.

Here is my powershell script.


 $WebClient=New-Object System.Net.WebClient 
 $credCatch=New-Object System.Net.CredentialCache
 $urlServiceNow = "http://instancename.service-now.com/incident.do?csv"

 try {
     $creds=New-Object System.Net.NetworkCredential($username,$password)
     $credCatch.Add($urlServiceNow,"Basic",$creds)
     $WebClient.Credentials = $credCache
     $WebClient.DownloadFile($urlServiceNow,$FilePath)

 }

 catch [System.Net.WebException] 
  {   # $_ is set to the ErrorRecord of the exception 
    if ($_.Exception.InnerException) 
   {
         $_.Exception.InnerException.Message   } 
     else {
         $_.Exception.Message

    } 
  }
1

There are 1 answers

0
reaching for libra On

Looks like you there is a typo in the variable naming. The cred information is being set in a $credCatch variable, but the web client is referencing a $credCache variable.