2013-10-29 3 views
2

Powershell 버전 3을 사용하고 나머지는 Rest를 통해 개체로 데이터를 가져오고 싶습니다.PowerShell에서 Rest를 통해 사용자 지정 개체 가져 오기

id  : http://services.odata.org/OData/OData.svc/Advertisements(guid'f89dee73-a 
     f9f-4cd4-b330-db93c25ff3c7') 
category : category 
link  : {link, link, link} 
title : 
updated : 2013-10-29T09:06:33Z 
author : author 
content : content 

하지만 ID는, 이름, 방송일이 같은 하나의 오브젝트 (2 객체)가있는 출력을 원하는 :

Invoke-RestMethod services.odata.org/OData/OData.svc/Advertisements?$format=json 

내 출력은 다음과 같습니다

은 내가 CmdLedt 호출-RestMehtod을 사용
Id          Name         Airdate 
--          ----         ------- 
f89dee73-af9f-4cd4-b330-db93c25ff3c7 Old School Lemonade Store, Retro Style 2012-11-07T00:00:00 
db2d2186-1c29-4d1e-88ef-a127f521b9c6 Early morning start, need coffee  2000-02-29T00:00:00 

누군가가 JSON을 반환하는 URI에서 costum 개체를 통해 CmdLedt를 가져 오는 방법을 설명 할 수 있습니까?

나중에이 코드를 C# 코드로 사용하여 URI에서 동적 객체를 가져와야하며 항상 다른 구조로되어 있습니다. 일반적으로 동적 객체를 만들기 때문에이 CmdLet이 필요합니다. 지금까지 C#을 내 코드에서 는 :

var args = new string[] { "http://services.odata.org/OData/OData.svc/?$format=json" }; 
var command = string.Format("Invoke-RestMethod {0}", args[0]); 

var results = InvokeCommand.InvokeScript(command); 

답변

1

나는 PowerShell을의 요청 및 브라우저의 요청을 비교하는 Fiddler을 사용했습니다. 당신이 볼 수 있듯이

// PowerShell 
GET /OData/OData.svc/Advertisements?=json HTTP/1.1 
// Browser 
GET /OData/OData.svc/Advertisements?$format=json HTTP/1.1 

$format는 URL에서 제거됩니다 그리고 당신은 (그래서 모든 URL의 매개 변수를 사용하지 않고, 단지 http://services.odata.org/OData/OData.svc/Advertisements 요청과 같은)을 atom+xml 응답을 얻을 것이다 :

Content-Type: application/atom+xml;type=feed;charset=utf-8 

해결하기 위해 문제가 발생하면 PowerShell을 사용할 때 $을 이스케이프 처리해야합니다.

이 시도 :

$obj = Invoke-RestMethod -uri "http://services.odata.org/OData/OData.svc/Advertisements?`$format=json" 
$obj.value 

출력을 (예상대로) :

ID          Name         AirDate 
--          ----         ------- 
f89dee73-af9f-4cd4-b330-db93c25ff3c7 Old School Lemonade Store, Retro Style 2012-11-07T00:00:00 
db2d2186-1c29-4d1e-88ef-a127f521b9c6 Early morning start, need coffee  2000-02-29T00:00:00 
+1

당신에게 아주 모자의 일종 감사합니다! 완벽하게 작동했습니다 :) –

관련 문제