【发布时间】:2019-09-21 23:19:05
【问题描述】:
我正在编写一个通过 Invoke-RestMethod 连接到 api 的 powershell 脚本。 对 api 的调用是公开的,无需进行身份验证。 它在 powershell 中工作,但无法在 ps1 脚本中执行。 我的操作系统是带有 powershell core 6.1.3 的 raspian
在 powershell 中的测试按预期工作:
pi@raspberry:~/Documents $ pwsh
PowerShell 6.1.3
Copyright (c) Microsoft Corporation. All rights reserved.
https://aka.ms/pscore6-docs
Type 'help' to get help.
PS /home/pi/Documents> $salida = Invoke-RestMethod -Uri 'https://api.binance.com/api/v1/time' -Method Get
PS /home/pi/Documents> Write-Host "Time: " $salida
Time: @{serverTime=1569106660940}
PS /home/pi/Documents>
但是有了这个ps1脚本(脚本有执行权限):
#!/usr/bin/pwsh
$salida = Invoke-RestMethod -Uri 'https://api.binance.com/api/v1/time' -Method Get
Write-Host "Time: " $salida
我收到此错误:
pi@raspberry:~/Documents $ sudo ./a.ps1
Invoke-RestMethod : Authentication failed, see inner exception.
At /home/pi/Documents/a.ps1:2 char:11
+ $salida = Invoke-RestMethod -Uri 'https://api.binance.com/api/v1/time ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (Method: GET, Re...rShell/6.1.3
}:HttpRequestMessage) [Invoke-RestMethod], HttpRequestException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Time:
pi@raspberry:~/Documents $
【问题讨论】:
标签: powershell raspberry-pi raspbian powershell-core