12/25/2022 0 Comments Powershell json query![]() ![]() The code above takes the ID of a single VM, stores it in a hashtable, and then submits that in JSON format to the collection URI stored in $uri. $r = Invoke-WebRequest -Uri $uri -Headers $global:RubrikHead -Method Post -Body (ConvertTo-Json -InputObject $body) For brevity’s sake, I’ve pulled the VM information and have included it below. All I need is the VM ID, which is a combination of the vCenter UUID and VM’s MOID, and the URI of the collection (/job/type/backup in this example). I’m going to perform a simple POST request that asks a Rubrik cluster to take an on-demand backup of a virtual machine. A complete list of status codes can be found on Wikipedia here. ![]() You can, however, use the status code to determine if the call was even submitted to the server. Because of this, you should not base the success or failure of an operational solely on the API status code. The details on the issue would reside in the response content. You could, for example, successfully contact the RESTful endpoint and receive a status code of 200 which means “OK.” However, the call itself may fail because the server has a blocking task or is otherwise unable to fulfill your request. ![]() It’s important to differentiate between the call status versus the response content. POST is not idempotent nor is it safe, meaning multiple requests yield varying results and data is modified. This method tells an endpoint that something new is to be created and the response from the server will indicate both the status of the API call and a response with details on the activity. The first method we’ll cover is POST, which I previewed back in the basic authentication article. It’s just the body content format and supported methods that should vary from solution to solution. I’ll be using a Rubrik cluster as my endpoint, but again – any RESTful endpoint should be similar in nature. The idea is to give you a head start on how to structure the API calls so that you can write your own. Now, I’ll dive into some of the methods available for sending data to a RESTful API endpoint by way of PowerShell code. In the past few posts in this series, I’ve tackled how to perform basic authentication and use the GET method to pull down data.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |