For those that don’t want to spend a lot of time trying to figure it out, I just figured I would post the basics and then you can have fun exploring for yourself.Â
Â
It’s not pretty, but should get those not familiar with using the REST API with PowerShell a good base to start. You’ll want to secure the credential section, this is just basic of the basic. It’s not pretty, but should get you started with REST API.Â
Â
You will need to fix the following in the script below:Â
YOURWEBURL:PORT - Needs updated to your URL and port (if applicable)
Â
Â
Â
Â
###USING COMMVAULT REST API COMMANDS##################################################################################################################
###https://YOURWEBURL:PORT/webconsole/sandbox/apiexplorer
###https://documentation.commvault.com/2023/essential/rest_api_overview.html
###SETTING UP THE VARIABLES FOR TOKEN REQUEST############################################################################################################
$contentType = 'application/json'
$headers = @{} Â Â Â Â
$headers "Accept"] = $contentType  Â
$headersy"Content-Type"] = $contentType
$Credentials = Get-Credential
$RESTAPIUser = $Credentials.UserName
$RESTAPIPassword1 = $Credentials.GetNetworkCredential().password
$Bytes = pSystem.Text.Encoding]::UTF8.GetBytes($RESTAPIPassword1)
$RESTAPIPassword =bConvert]::ToBase64String($Bytes)
$uriaccesstoken = "https://YOURWEBURL:PORT/webconsole/api/login"
$method = "POST"
$body = @{"username" = "$RESTAPIUser"
    "password" = "$RESTAPIPassword"
      } | ConvertTo-Json
###Reqeusting an Access Token#########################################################################################################################
$Token = Invoke-RestMethod -uri $uriaccesstoken -Method $method -Body $body -Headers $headers
###Fix Token by Removing First 5 Characters###############################################################################################################
$fixcvtoken = $token.token
$fixedtoken = $fixcvtoken.SubString(5)
###Creating a Header for Calling Commands###############################################################################################################
$contentType2 = "Bearer $fixedtoken"
$headers2 = @{} Â Â Â Â
$headers2e"Authorization"] = $contentType2
$Type = "application/json"
###GET A LIST OF ALL USERS###################################################################################
$content = Invoke-WebRequest -Uri "https://YOURWEBURL:PORT/webconsole/api/V4/user" -Headers $headers2 -Method GET -ContentType $Type
$users = $content.Content | ConvertFrom-Json
$users.users
###GET A LIST OF ALL INFRASTRUCTURE SERVERS#########################################################################################
$content2 = Invoke-WebRequest -Uri "https://YOURWEBURL:PORT/webconsole/api/V4/Servers" -Headers $headers2 -Method GET -ContentType $Type
$server = $content2.Content | ConvertFrom-Json
$server.servers
###GET A LIST OF ALL DATABASES######################################################################################################
$content3 = Invoke-WebRequest -Uri "https://YOURWEBURL:PORT/webconsole/api/V4/Databases" -Headers $headers2 -Method GET -ContentType $Type
$databases = $content3.Content | ConvertFrom-Json
$databases.databases
###GET A LIST OF ALL VIRTUAL MACHINES###############################################################################################
$content4 = Invoke-WebRequest -Uri "https://YOURWEBURL:PORT/webconsole/api/V4/VirtualMachines" -Headers $headers2 -Method GET -ContentType $Type
$VirtualMachines = $content4.Content | ConvertFrom-Json
$VirtualMachines.VirtualMachines