2016-08-02 3 views
1

개체 생성 코드가 한 줄이면 동적 json 개체를 전달할 수있었습니다. 현실 세계에서 동적 JSON 객체가 더 이상 할 필요가 있기 때문에Dynamic Json 변수를 Invoke-Restmethod로 넘김

Invoke-RestMethod -ContentType "application/json" -Method Post -Body '{ "name" : "azurefunctionapp2email", "appname": "Applicationnamehere", "requestedBy" : "requestedby", "reqdate" : "requestdate", "status" : "Successfully Deployed", "AppsCount" : "2" }' ` 
    -Uri “https://implementurihere" 

는, I는 JSON이 중단됩니다 and referenced in the above as below. But new line shift 새로운 라인을 만들어 구분. 내가 ConvertTo-Json 기능 파이프에 노력하고 유지하기 위해 출력을 발견 ''\ 연구 \ n '을 도입하기 :

$body = '{ "name" : "azurefunctionapp2email", ` 
     "appname": "Applicationnamehere", ` 
     "requestedBy" : "requestedby", ` 
     "reqdate" : "requestdate", 
     "status" : "Successfully Deployed", 
     "AppsCount" : "2" }' ` 

Invoke-RestMethod -ContentType "application/json" -Method Post -Body $body ` 
    -Uri “https://implementurihere" 

참고 : $body이 한 줄은 위의 작품 인 경우.

우리는 동적 json, 긴 파일 및 피드를 만드는 시나리오에서 어떻게 접근합니까?

답변

2

마지막 줄에 생략해야하는 백틱이 들어 있기 때문에이 예가 작동하지 않습니다.

$body = 
@' 
    { "name" : "azurefunctionapp2email", 
     "appname": "Applicationnamehere", 
     "requestedBy" : "requestedby", 
     "reqdate" : "requestdate", 
     "status" : "Successfully Deployed", 
     "AppsCount" : "2" } 
'@ 

또한 의지 할 수 있습니다 개체를 정의하는 PowerShell에서의 해시 테이블을 사용하는 고려할 수 있습니다 : 당신은 역 따옴표로 각 라인을 분리해서 할 필요가 없습니다

당신은 당신의 JSON을 정의하는 here string을 사용할 수 있습니다 형식 문자열을 사용하지 않고 변수를 사용할 수 있습니다.

$bodyObject = @{ 
    name = 'azurefunctionapp2email' 
    appname = 'Applicationnamehere' 
    requestedBy = 'requestedby' 
    reqdate = 'requestdate' 
    status = 'Successfully Deployed' 
    AppsCount = '2' 
} 

$bodyObject | ConvertTo-Json 
+0

예, 감사합니다. –