Hi guys,
I have been using the CVPowerShellSDK_v1 and was able to create tools and scripts for our team. Now, I would like to move a step further and make my own API Calls with the Invoke-RestMethod in PowerShell but I am having a hard time to get started (disclaimer: I have a backgrouind as a sysadmin, not a web developper). I played around with Postman, API Explorer, I went through the documentation on the subject and tried different ways to interact with APIs in PowerShell but I still haven’t succeeded in retrieving data with a simple GET method.
I am at the point where I need a little help from friends to move forward. So, if anyone could help me to make a simple GET request through PowerShell with an output in JSON format, I would greatly appreciate and would be able to figure out the rest by myself.
Let’s say, for example, I want to retrieve the information of the EmailServer and create a simple function like Get-CVEmailServer.
In curl, the request would be:
curl -X GET "https://myserver/webconsole/api/EmailServer" -H "accept: application/json"
And the Response body :
{ "enableSSL": false, "smtpServer": "commserve.com", "filterInvalidEmails": false, "startTLS": false, "smtpPort": 25, "useAuthentication": false, "useEncryptedMailServer": false, "maxMailServerSize": 10240, "timeout": 30000, "senderInfo": { "senderName": "LukeBrett", "senderAddress": "lukebrett@mycompany.com" } }
Now, I have been trying different things around the following command, but did not get anything I could work with... :
$headers = @{}
$headers.Add("Accept", "application/json")
$headers.Add("Content-Type", "application/json")
$creds = Get-Credential
Invoke-RestMethod -Uri 'http://myserver/webconsole/api/EmailServer' -Method Get -Credential $creds -SkipCertificateCheck -Headers $headers
I know I might be missing some formatting expressions here and other details, but if someone could provide me with an example, it would really be a game changer for me. Thanks a lot!