【问题标题】:Powershell 2.0 Web-Request Alternative to API GatewayAPI 网关的 Powershell 2.0 Web 请求替代方案
【发布时间】:2018-07-13 11:19:40
【问题描述】:

我想使用 API-Gateway 从亚马逊 AWS 中提取信息。 我创建了一个 Lambda 函数,它生成信息并将该函数转换为 API-GATEWAY。

我想调用invoke-webrequest 来获取信息。 web-request 将包含以下信息以传递以从 api-gateway 获取信息:AWSURL、APIKEY、Data

我已经在Powershell 3.0中创建并测试了invoke-webrequest,下面是代码

Invoke-WebRequest -Headers @{"X-Api-Key" = "<key>"} -Method PUT
              -Body "<data>" -Uri <awsURL>
              | Select-Object -Expand Content

这是真正的问题。

我的大多数机器都在 Windows POS 2009 上我已经创建了在 powershell 2.0 中工作的脚本并在我的 windows 10 机器上进行了测试并且工作正常

  function ConvertFrom-Json20([object] $item){
    add-type -assembly system.web.extensions
    $ps_js=new-object system.web.script.serialization.javascriptSerializer

    #The comma operator is the array construction operator in PowerShell
    return ,$ps_js.DeserializeObject($item)
}

$awsUrl = "<awsURL>"

$urlHeader = [System.Net.WebRequest]::Create($awsURL)
$urlHeader.Headers.Add("X-Api-Key","<APIKEY>")
$urlHeader.Method = "PUT"

$data = "<DATA>"

$requestStream = $urlHeader.GetRequestStream()
$streamWriter = New-Object System.IO.StreamWriter($requestStream)
$streamWriter.Write($data)

if ($null -ne $streamWriter) { $streamWriter.Dispose() }
if ($null -ne $requestStream) { $requestStream.Dispose() }

$res = $urlHeader.GetResponse()

$streamReader = New-Object System.IO.StreamReader $res.GetResponseStream()
$result = $streamReader.ReadToEnd()

$resultFromJson = ConvertFrom-Json20 $result
Write-Host $resultFromJson

但是,当我在 windows XP POS 2009 上运行脚本时,它不起作用。 Windows XP POS 2009 使用的是 powershell 2.0。我在下面得到一个 getResponse() 错误检查

    Exception calling "GetResponse" with "0" argument(s): "The underlying connection was closed: An unexpected error occurred on a send."
At line:24 char:30
+ $res = $urlHeader.GetResponse <<<< ()
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

You cannot call a method on a null-valued expression.
At line:26 char:73
+ $streamReader = New-Object System.IO.StreamReader $res.GetResponseStream <<<< ()
    + CategoryInfo          : InvalidOperation: (GetResponseStream:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At line:27 char:34
+ $result = $streamReader.ReadToEnd <<<< ()
    + CategoryInfo          : InvalidOperation: (ReadToEnd:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

我相信这可能与 windows XP POS Webservices 不喜欢响应或其他东西有关,作为替代我可以使用 cURL,但我真的想使用 windows powershell 2.0 脚本来做到这一点

有人可以提供他们在这方面的知识和经验,因为我已经尝试了很长时间。谢谢。

【问题讨论】:

    标签: powershell curl aws-lambda powershell-2.0 windows-xp


    【解决方案1】:

    尝试使用适当的 PowerShell 目标版本 - powershell -version 2 .\your-script.ps1 进行测试。您可能还想通过深入 $error 对象来查看错误以查找发生了什么。例如,您可能没有 POS 盒上 TLS 连接的有效证书。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-23
      • 1970-01-01
      • 2019-11-01
      • 2017-11-25
      • 2021-11-26
      • 1970-01-01
      • 2012-12-28
      • 2019-10-20
      相关资源
      最近更新 更多