PowerShell -command来自批处理Invoke-RestMethod

时间:2016-06-13 19:00:20

标签: powershell batch-file cmd

我已经尝试了我能想到的每一种组合,但我能想到的最好的是编码问题。我知道如何通过使用-EncodedCommand或创建一个小的.ps1脚本来解决这个问题,但这不是我想要的原因。

所以我希望,有人在这里知道发生了什么。

这适用于PowerShell命令提示符:

Invoke-RestMethod -Uri "http://127.0.0.1:8989/api/series" -Method Get -Header @{"X-Api-Key" = "1234"}

但是当我尝试从命令提示符或批处理中传递它时失败了!

powershell.exe -command "& {Invoke-RestMethod -Uri "http://127.0.0.1:8989/api/series" -Method Get -Header @{ "X-Api-Key" = "1234" } }"

powershell.exe -command "& Invoke-RestMethod -Uri "http://127.0.0.1:8989/api/series" -Method Get -Header @{ "X-Api-Key" = "1234" }"

powershell.exe -command Invoke-RestMethod -Uri http://127.0.0.1:8989/api/series -Method Get -Header @{ "X-Api-Key" = 1234 }

powershell.exe -command @{Invoke-RestMethod -Uri http://127.0.0.1:8989/api/series -Method Get -Header "X-Api-Key" = 1234 }

以及许多,更多,更多的组合;(

更新160614 删除了所有错误示例,不再需要了。

2 个答案:

答案 0 :(得分:0)

你需要引用整个事情并转义引号:

# If running within cmd
powershell.exe -command "& Invoke-RestMethod -Uri \"http://127.0.0.1:8989/api/series\" -Method Get -Header @{ \"X-Api-Key\" = \"1234\" }"

# If running within powershell
powershell.exe -command "& Invoke-RestMethod -Uri \`"http://127.0.0.1:8989/api/series\`" -Method Get -Header @{ \`"X-Api-Key\`" = \`"1234\`" }"

答案 1 :(得分:0)

对我来说,我愿意

runtime: python37
service: dev-pypyserver
instance_class: F4

handlers:
- url: /.*
  secure: always
  script: auto
  
default_expiration: "3s"



entrypoint: pserve server.ini