- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
How to update records (PUT) in PowerShell using Invoke-WebRequest?
[ Edited ]- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2019 05:26 PM - edited 08-18-2019 05:28 PM
Hi, I'm writing a PowerShell script that adds or updates macfilteraddress objects in a filtermac object. I able to use Invoke-WebRequest to successfully GET and POST (create) these records, but can't figure out how to PUT (update) them.
GET and POST methods seem to just work using URL parameters, however using the same parameters with a PUT method throws a 400 Bad Request error. I'm assuming I have to send a "body" with the PUT request. This might sound obvious to some of you, but I'm not a webdev by profession. I'm familiar with using GET and POST, but not PUT (or DELETE for that matter).
At any rate, I can't seem to find any PowerShell examples of doing this. All the examples I've come across just show how to use the cURL utility. I've tried translating these examples into PowerShell but can't seem to format the request properly, always getting a 400 Bad Request response.
Here is what I'm trying currently:
$body = @{ comment = "Updated comment" } $url = "https://dev.ib.domain.com/wapi/v2.7.3/macfilteraddress/GVtJGVuZ3ItZ2VuZXJhbEAwMDozNDo3Nzo2Njo3Mzo2NA:" $response = Invoke-WebRequest -Uri $url -Method "PUT" -WebSession $cookie -Body $body
Can anyone give me a lead on how to properly send this request in PowerShell?
Thanks,
== Matt
Re: How to update records (PUT) in PowerShell using Invoke-WebRequest?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-18-2019 05:33 PM
Aaand I figured it out. I needed to convert the body hashtable to JSON:
$body = @{ comment = "Updated comment" } $body = $body | ConvertTo-Json $url = "https://dev.ib.domain.com/wapi/v2.7.3/macfilteraddress/GVtJGVuZ3ItZ2VuZXJhbEAwMDozNDo3Nzo2Njo3Mzo2NA:" $response = Invoke-WebRequest -Uri $url -Method "PUT" -WebSession $cookie -Body $body