Powershell 2.0 Web-Request替代API网关

问题描述 投票:0回答:1

我想使用API​​-Gateway从amazon 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 Web服务不喜欢回复或其他东西有关,作为ALTERNATIVE我可以使用cURL但我真的想用windows powershell 2.0脚本来做这个

有人可以提供他们的知识和经验,因为我已经尝试这么久了。谢谢。

powershell curl aws-lambda powershell-v2.0 windows-xp
1个回答
0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.