REST APi Authentication with Powershell Basic Starter Script

  • 15 February 2024
  • 0 replies
  • 127 views

Badge +1

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    
$headers["Content-Type"] = $contentType
$Credentials = Get-Credential
$RESTAPIUser = $Credentials.UserName
$RESTAPIPassword1 = $Credentials.GetNetworkCredential().password
$Bytes = [System.Text.Encoding]::UTF8.GetBytes($RESTAPIPassword1)
$RESTAPIPassword =[Convert]::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 = @{}        
$headers2["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


0 replies

Be the first to reply!

Reply