How can I use Powershell to access a restful webservice?

前端 未结 3 1956
无人及你
无人及你 2020-12-30 04:07

I need to integrate an existing powershell script to update it\'s status via a restful web service that returns json. I\'m a bit new to powershell but I was able to find the

相关标签:
3条回答
  • 2020-12-30 04:33

    You could use DataContractJsonSerializer, which is a part of standard .Net library.

    0 讨论(0)
  • 2020-12-30 04:40

    @Jaykul wrote a nice set of RESTful functions that are part of his Mindtouch dreamwiki script over here: http://poshcode.org/691

    0 讨论(0)
  • 2020-12-30 04:42

    What you want is PowerShell 3 and its Invoke-RestMethod, ConvertTo-Json, and ConvertFrom-Json cmdlets. Your code will end up looking like:

    $stuff = invoke-RestMethod -Uri $url -Method Get;

    and there shouldn't even be a need to invoke ConvertFrom-Json on the resulting $stuff => it's already in a usable non-string format.

    As for POSTs|PUTs, simply use PowerShell hashes and arrays to structure your data and then call ConvertTo-Json on it before passing it to invoke-RestMethod or invoke-WebRequest:

    invoke-WebRequest -Uri $url -ContentType application/json -Method Post -Body $objectConvertedToJson

    See http://technet.microsoft.com/en-us/Library/hh849971.aspx for details.

    0 讨论(0)
提交回复
热议问题