2016-06-13 2 views
0

내가 생각할 수있는 모든 조합을 시도했지만 가장 좋은 것은 인코딩 문제입니다. 나는 -EncodedCommand를 사용하거나 작은 .ps1 스크립트를 작성하여이 문제를 해결하는 방법을 알고 있지만 몇 가지 이유를 찾고있는 것은 아닙니다.PowerShell -command from batch Invoke-RestMethod

여기 누군가가 여기에서 무슨 일이 일어나고 있는지 알고 있기를 바랍니다.

이 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 제거 모든 오류 예, 더 이상 필요하지

+0

[this] (http://stackoverflow.com/a/34282945)를 참조하십시오. – PetSerAl

답변

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\`" }" 
+0

PowerShell.exe '명령 줄 파서는 실제로 백틱이 아닌 이스케이프를 위해 백 슬래시를 사용합니다. – PetSerAl

+0

@PetSerAl : 실제로 쉘이 따옴표를 이스케이프 처리합니다. 업데이트가 필요합니다. cmd.exe 내에서 실행하는 경우 \ – Guvante

+1

을 사용해야합니다. PowerShell에서 실행하려면''\' "''이어야합니다.''PowerShell.exe' 명령 줄 구문 분석기는 명령 줄에서''\''을 볼 필요가 있습니다. – PetSerAl