【问题标题】:powershell raspbian execute Invoke-RestMethod from ps1powershell raspbian 从 ps1 执行 Invoke-RestMethod
【发布时间】: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


    【解决方案1】:

    我认为您偶然发现了 Powershell 的 Raspberry Pi/ARM 版本的一个错误。我也可以用 PowerShell 的 Core 6.2.3 复制这个问题。

    # /usr/bin/powershell -File ./mytestfile.ps1
    

    结果:

    Invoke-Webrequest : Authentication failed, see inner exception.
    At mytestfile.ps1:2 char:18
    +     $WebAction = Invoke-Webrequest -uri $URL -Method Default
    

    但是…… 如果我从 shell 中运行文件,它可以正常工作:

    # /usr/bin/powershell
    PowerShell 6.2.3
    Copyright (c) Microsoft Corporation. All rights reserved.
    
    https://aka.ms/pscore6-docs
    Type 'help' to get help.
    
    PS /home/user> ./mytestfile.ps1
    Downloading https://docs.google.com/spreadsheets/d...blah,blah,blah
    Download:  OK
    PS /home/user>
    

    我认为这需要向 Microsoft 报告以解决:https://github.com/PowerShell/PowerShell/issues

    【讨论】:

      【解决方案2】:

      我想这就是你的答案:https://github.com/dotnet/corefx/issues/33179

      TL;DR:从 bash 启动您的 Powershell 脚本之前,请执行以下操作

      导出 CLR_OPENSSL_VERSION_OVERRIDE=1.1

      加长版: 问题不在于脚本的文件权限,也不是运行它的用户的权限。错误来自 Cmdlet Invoke-WebRequest PowerShell/.NET 依赖于 OpenSSL 库 1.0 版本,Raspbian 默认安装了 1.1 版本。微软网站上的安装说明只是告诉我们安装 libssl1.0,所以我们最终得到了 1.0 和 1.1 的副本。在适当的情况下,这可能会使 PowerShell 感到困惑。您的 Invoke-WebRequest 用于 HTTPS 通信协议,该协议使用传输层安全性 (TLS) 或以前的安全套接字层 (SSL) 进行 HTTP 加密。我的猜测是,当在脚本中调用 Invoke-WebRequest 时,Powershell 会错误地尝试使用 libssl 的 1.1 版,但在运行交互性时,它会正确使用 libssl1.0。我不清楚 CLR_OPENSSL_VERSION_OVERRIDE=1.1 究竟是如何导致正确行为的,但根据我的经验,它确实有效。

      您可以通过运行以下脚本来了解有关该问题的更多信息,该脚本会递归地向您显示初始异常的“内部异常”。这可以更好地了解导致 Invoke-WebRequest 引发异常的基础问题

      Try {
          ((Invoke-WebRequest -Uri "https://httpbin.org/headers").Content | ConvertFrom-Json).headers
      }
      catch {
          $e = $_.Exception
          $msg = $e.Message
          $depth = ">"
          while ($e.InnerException) {
              $e = $e.InnerException
              $msg += "`n$depth $($e.Message)" 
              $depth += ">"
          }
          $msg
      }
      

      以交互方式运行时,它按预期工作:

      User@rpi:/home/User $ /usr/bin/powershell 
      PowerShell 6.2.3
      Copyright (c) Microsoft Corporation. All rights reserved.
      
      https://aka.ms/pscore6-docs
      Type 'help' to get help.
      
      PS /home/user> ./WebRequest-Test.ps1
      
      Host        User-Agent
      ----        ----------
      httpbin.org Mozilla/5.0 (Linux; Linux 4.19.75-v7+ #1270 SMP Tue Sep 24 18:45:1…
      

      但是当我从那个 bash shell 运行它时(没有先运行 export CLR_OPENSSL_VERSION_OVERRIDE=1.1 )我得到:

      User@rpi:/home/User $ /usr/bin/powershell WebRequest-Test.ps1 
      
      The SSL connection could not be established, see inner exception.
      > Authentication failed, see inner exception.
      >> The type initializer for 'SslMethods' threw an exception.
      >>> The type initializer for 'Ssl' threw an exception.
      >>>> The type initializer for 'SslInitializer' threw an exception.
      >>>>> error:0E076071:configuration file routines:MODULE_RUN:unknown module name
      

      【讨论】:

        猜你喜欢
        • 2015-02-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-20
        • 1970-01-01
        • 2017-04-16
        • 1970-01-01
        相关资源
        最近更新 更多