如何使用Invoke-Webrequest下载Salesforce报告

时间:2020-05-12 19:11:16

标签: powershell rest salesforce

我需要下载Salesforce报表,以前我一直在通过单击按钮进行一些手动工作, 我最近发现,我可以在url的末尾添加一些参数来让chrome下载它,而无需单击export

# Link to the report
https://mycompanydomain.com/00O3md123456789qf 
# when I add these to the end of the link and paste it to chrome, chrome will download directly
https://mycompanydomain.com/00O3md123456789qf?export=1&enc=UTF-8&xf=csv

是否有一种方法可以使用Powershell自动化? 我尝试使用Invoke Webrequest,但是它无法按照我想要的方式工作,它不会下载实际文件,但会返回响应状态。

# get Salesforce Data
$url = "https://mycompanydomain.com/00O3md123456789qf?export=1&enc=UTF-8&xf=csv"
$output =  ".\salesforce.csv"
$start_time = Get-Date
Invoke-WebRequest -Uri $url -OutFile $output
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"

1 个答案:

答案 0 :(得分:0)

我无法帮助您使用PowerShell,但是Salesforce的REST API包含许多使用cURL库的示例。您也许可以从他们那里改编一些东西,或者也许有不错的博客帖子,例如https://www.jbmurphy.com/2016/07/25/connecting-to-the-salesforce-rest-api-using-powershell/。这是文档中的cURL login call

curl https://login.salesforce.com/services/oauth2/token -d "grant_type=password" -d 
"client_id=myclientid" -d "client_secret=myclientsecret" 
-d "username=mylogin@salesforce.com" -d "password=mypassword123456"

您可以手动发出原始请求,也可以使用现成的SF连接库之一在Java / C#/ Python / PHP中编写一个小程序。

基本上您需要打2个电话

  1. 一个POST登录并获取会话ID
  2. 一个GET来提取报告。它必须包含带有会话ID的标头。当您模仿普通用户访问SF的Web版本时,标头必须假装为Cookie sid=sessionidgoeshere。如果您要使用正确的API调用(是的,有一个报告API可以很好地导出,修改报告和完成许多有趣的工作),则可以使用Authorization Bearer sessionidgoeshere

是的,如果您想要的是解决方案,那应该可以给您足够的信息来开始搜索“ Powershell登录到Salesforce”。如果您想了解更多内容,请继续阅读

我已经回答了几个类似的问题,请检查

如果您以前从未进行过OAuth2流程(起初阅读时有很多选项,并且最简单的“用户名密码”流程已隐藏在giant intimidating list上),那么如果您只需单击一下就可以开始操作通过https://openidconnect.herokuapp.com/

相关问题