Powershell-脚本按预期在ISE中运行。从命令行调用时出错

时间:2019-12-11 17:10:32

标签: powershell

我有一个调用内部API的脚本。它传递了令牌和json主体。

当我在ISE中对参数(令牌和json_body)进行硬编码时,它运行得很好。一旦我从命令行运行,它就会炸弹。我收到错误消息“无法处理参数,因为参数“ name”无效。

请注意。参数$ json_body可以 非常

这是我的剧本。超级简单,按预期工作

PARAM($json_body, $token)

$json_body = 
'{
     "field1" : "1",
     "field2" : "2",
     "data" : [{<json data here>}]
}'

$header = @{'Token' = '1234567890* '}

Invoke-WebRequest -Uri <uri-here> -Method POST -ContentType "application/json" -Headers $header -Body $json_body 

这是我正在使用的命令

powershell.exe -executionpolicy bypass -inputformat none -file "C:\PowerShell\Call_Rest_API_And_Post.ps1" -token <token_text> -json_body <json_body_text>

我正在从ETL进程传递数据以将数据推入参数,但是如果我直接从PowerShell(不是ISE)运行此命令,则会出现此错误:

Invoke-WebRequest : An unknown error occurred. Please see server logs for details.
At C:\PowerShell\Call_Rest_API_And_Post.ps1:5 char:1
+ Invoke-WebRequest -Uri <uri-here> ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

有指针吗?

0 个答案:

没有答案