以编程方式调用URL导出 - 返回(401)未经授权的响应

时间:2016-12-21 22:15:14

标签: powershell-v2.0 servicenow

我正在尝试以编程方式调用服务现在URL但我收到401未经授权的错误。有趣的是,当我在Chrome浏览器中调用servicenow webservice url打开一个登录会话时,我可以成功下载该文件。

这是我的powershell脚本。

 $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 个答案:

答案 0 :(得分:0)

看起来变量命名中存在拼写错误。信用信息在$ credCatch变量中设置,但Web客户端引用$ credCache变量。