PowerShell - 来自批处理 Invoke-RestMethod 的命令



我已经尝试了我能想到的所有组合,但我能想到的最好的是编码问题。 我知道如何通过使用 -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删除了不再需要的所有错误示例。

您需要引用整个内容并转义引号:

# 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`" }"
对我来说

我会做的

powershell -Command "Invoke-Webrequest https://file.com -Outfile file.file" & cls
pause >nul 

最新更新