Introducing SOC Insights for BloxOne Threat Defense: Boost your SOC efficiency with AI-driven insights to eliminate manual work and accelerate investigation and response times. Read the blog announcement here.

API & Integration, DevOps,NetOps,SecOps

Reply

How to update records (PUT) in PowerShell using Invoke-WebRequest?

[ Edited ]
New Member
Posts: 2
9293     0

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?

New Member
Posts: 2
9294     0

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
Showing results for 
Search instead for 
Did you mean: 

Recommended for You