Solved

PowerShell Invoke-RestMethod for API Call

  • 19 August 2022
  • 4 replies
  • 4905 views

Userlevel 1
Badge +5

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!

icon

Best answer by Jos Meijer 19 August 2022, 21:44

View original

4 replies

Userlevel 7
Badge +17

I am not experienced with powershell, but if you go to this link:

https://api.commvault.com/#a2b7504f-69d1-4965-87c9-630bc0dbc54e

And then at the top at the section Language select PowerShell - RestMethod

It will translate from Curl to what you need, at least an example is given.

In this case:

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept", "application/json")
$headers.Add("Authtoken", "QSDK token would be auto set after POST Login is called.")

$response = Invoke-RestMethod 'http://WebConsoleHostName/webconsole/api/EmailServer' -Method 'GET' -Headers $headers
$response | ConvertTo-Json

the response would be something like:

{
"enableSSL": false,
"smtpServer": "smtp.commvault.com",
"smtpPort": 25,
"useAuthentication": false,
"maxMailServerSize": 0,
"userInfo": {
"userName": ""
},
"senderInfo": {
"senderName": "Yash",
"senderAddress": "Commvault@GreenValley.test.com"
}
}

Hope this helps 🙂

 

Userlevel 1
Badge +5

Thanks @Jos Meijer! :) I wasn’t aware that I could change the language to PowerShell on the API page. I was able to do the test I suggested. The token part needed to be adapted a little so I’ll post it here : 

# How to connect and get a token

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept", "application/json")
$headers.Add("Content-Type", "application/json")

$body = "{`n `"password`": `"Password_in_Base64`",`n `"username`": `"admin`",`n `"timeout`" : 30`n}"

$response = Invoke-RestMethod 'http://WebServerName/webconsole/api/Login' -Method 'POST' -Headers $headers -Body $body
$token = $response.token

# Getting the EmailServer

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Accept", "application/json")
$headers.Add("Authtoken", $token)

$response = Invoke-RestMethod 'http://WebServerName/webconsole/api/EmailServer' -Method 'GET' -Headers $headers
$response | ConvertTo-Json


{
"enableSSL": false,
"smtpServer": "MySmtpServer",
"filterInvalidEmails": false,
"startTLS": false,
"smtpPort": 25,
"useAuthentication": false,
"useEncryptedMailServer": false,
"maxMailServerSize": 10240,
"timeout": 30000,
"senderInfo": {
"senderName": "LukeBrett",
"senderAddress": "lbrett@test.com"
}
}

 

Userlevel 7
Badge +17

Your welcome @LukeBrett 

Good to hear it's working now 🙂

Userlevel 7
Badge +19

@LukeBrett it might be worth looking into the V2 version of the Powershell SDK. It delivers more backend functionality and includes by default a cmdlet that allows you to set the SMTP server. I have raised some questions if the missing functionality from V1 is ported to V2 so we can all focus on one generic version.

See: https://www.powershellgallery.com/packages/CommvaultPowerShell/0.2.6

Reply