我写了一些代码,试图从一个Rest API获取值并将其发布到另一个。 我将代码保存在.ps1文件中。如果我编辑它并运行它(或者将它复制并粘贴到一个空的PowerShell终端中),它可以达到我的预期。但是,当我尝试直接运行相同的.ps1文件时,第二个Invoke-RestMethod出现错误。PowerShell运行PowerShell代码工作,执行为.ps1不
不明白为什么我会得到不同的结果,并且错误消息没有给我多少线索。
我在做什么错?
我使用的代码是(与修改API密钥):
$encoded = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($APIkey+":"))
$headers = @{"Content-Type" = "application/json"; "AUTHORIZATION" = "Basic $encoded"}
$APIkey = "123456789"
$metricId = "123"
$r = Invoke-RestMethod -Uri https://coinbase.com/api/v1/currencies/exchange_rates
$metric = [PSCustomObject]@{
value = [Math]::Round($r.btc_to_eur, 2)
}
$baseuri = "https://api.numerousapp.com/v1/metrics/$metricId/events"
$data = ConvertTo-Json $metric
Invoke-RestMethod -Uri $baseuri -Body $data -Headers $headers -Method Post
和错误消息,在运行的.ps1文件时,我得到的是:
Invoke-RestMethod : : At C:\NumerousBitcoinEur.ps1:13 char:1 + Invoke-RestMethod -Uri $baseuri -Body $data -Headers $headers -Method Post + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
我使用PowerShell 4.0
当我在粘贴控制台或从ISE运行时遇到同样的错误。所以我不认为ps1文件与它有任何关系。 – bouvierr