Search code examples
powershellodatapowershell-4.0

How can I update an entity via OData service using PowerShell?


I'm trying to work out how to do OData updates using PowerShell as the client. I found the site services.odata.org to use for testing: http://services.odata.org/OData/OData.svc/$metadata.

I've tried this:

Invoke-RestMethod -Method Put -ContentType 'application/json' `
    -Uri 'http://services.odata.org/V3/(S(k22mmq0ajlv45epd2psyysnd))/OData/OData.svc/Products(0)' `
    -Body ( @{ Description = 'CheesyPeas' } | ConvertTo-Json )

but I get back

Invoke-RestMethod : <?xml version="1.0" encoding="utf-8"?>
  <m:error xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
    <m:code />
    <m:message xml:lang="en-US">
      Error processing request stream. Type information must be specified for types that take part in inheritance.
    </m:message>
  </m:error>
At line:1 char:1
+ Invoke-RestMethod -Uri 'http://services.odata.org/V3/(S(k22mmq0ajlv45epd2psyysnd ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

I think this has something to do with the Navigation Properties. Ideally, I'd be testing against a simple entity with no Navigation Properties until I've got a basic PUT working but I can't find one. Can anyone help me get this working?


Solution

  • As the error said, you need to specify the type you send in the body.

    Write your code like below:

    Invoke-RestMethod -Method Put -ContentType 'application/json' `
        -Uri 'http://services.odata.org/V3/(S(k22mmq0ajlv45epd2psyysnd))/OData/OData.svc/Products(0)' `
        -Body ( @{ "odata.type" = 'ODataDemo.Product'; Description = 'CheesyPeas' } | ConvertTo-Json )