我寫了一些代碼,試圖從一個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