11-08-2021 04:13 AM
Hi all,
on the official Veritas page on the GitHub there is a section for NetBackup PowerShell Cmdlets (https://github.com/VeritasOS/nb-powershell). Since I am not so strong in Powershell scripting I would like to ask you how to compile these scripts.
I was able to list Cmdlets - see below. However, I do not know how to make these cmdlets working (e.g. Get-NBAlert). Should I download e.g. GetAlert.cs and then somehow compile it?
Any help much appreciated!
PS > Get-Command -Module NBCmdlets
CommandType Name ModuleName
----------- ---- ----------
Cmdlet Add-NBRecoverVMWareInstantAccessMount NBCmdlets
Cmdlet Get-AppDetails NBCmdlets
Cmdlet Get-NBAccessPermissions NBCmdlets
Cmdlet Get-NBAccessRoles NBCmdlets
Cmdlet Get-NBAccessRules NBCmdlets
Cmdlet Get-NBAlert NBCmdlet
.....
11-08-2021 11:08 AM - edited 11-08-2021 11:09 AM
The code for those look to be 3 years old, really out dated. If you're interested in trying to take advantage of the API by using powershell and have some scripting experience, I would look at the code and just write your own small little PowerShell scripts for things you need /want to automate.
I would also use this one instead as its updated fairly often.
https://github.com/VeritasOS/netbackup-api-code-samples/tree/master/recipes/powershell
11-09-2021 01:51 AM
Hi @Krutons !
I will try to dig into the powershell code....however, is it possible to leverage API calls when I have Netbackup Appliance (Netbackup 8.2). I am on the Windows machine, so I do not know how to make curl command from Windows to use API calls.
11-09-2021 06:22 AM
Yes JimmyB2, you can make API calls from almost anywhere.
Let's start from the beginning. Have you already created an API Key?
11-09-2021 07:14 AM
Hi @Krutons , yes, I have created API key!
11-09-2021 07:49 AM
Have you went to https://<master server>/api-docs/index.html and tested the API key on a basic API call like under admin. GET /admin/jobs
If you have verified the API key works we can move on.
11-09-2021 08:03 AM
Hi @Krutons , yes, I can access this page. Moreover, I have to specify port number within the address - https://<master server>:1556/api-docs/index.html
I would like to carry tomorrow. So far, big thanks for your guidance!
11-09-2021 08:16 AM - edited 11-09-2021 09:22 AM
JimmyB2, no problem.
Here is a powershell script that you could run to get jobs. You could replace any info that would be specific to your environment. Once you've looked over that, let me know if you have any questions and I'll be happy to help/explain. I personally don't use username/password, I strictly auth with API Keys.
<#
.SYNOPSIS
This sample script demonstrates the use of NetBackup REST API for listing the jobs.
.DESCRIPTION
This script will get a list of netbackup jobs and print the details of the last 10 jobs in a tabular format.
.EXAMPLE
./Get-NB-Jobs.ps1 -nbmaster "nb-master.example.com" -apikey "key"
#>
param (
[string]$nbmaster = $(throw "Please specify the name of NetBackup master server using -nbmaster parameter."),
[string]$apikey = $(throw "Please specify the API Key using the -apikey parameter.")
)
#####################################################################
# Initial Setup
# Note: This allows self-signed certificates and enables TLS v1.2
#####################################################################
function InitialSetup()
{
# Allow self-signed certificates
if ([System.Net.ServicePointManager]::CertificatePolicy -notlike 'TrustAllCertsPolicy')
{
Add-Type -TypeDefinition @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName TrustAllCertsPolicy
# Force TLS v1.2
try {
if ([Net.ServicePointManager]::SecurityProtocol -notmatch 'Tls12') {
[Net.ServicePointManager]::SecurityProtocol = ([Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12)
}
}
catch {
Write-Host $_.Exception.InnerException.Message
}
}
}
InitialSetup
#####################################################################
# Global Variables
#####################################################################
$port = 1556
$basepath = "https://" + $nbmaster + ":" + $port + "/netbackup"
$content_type = "application/vnd.netbackup+json;version=1.0"
#####################################################################
# Get NetBackup Jobs
#####################################################################
$uri = $basepath + "/admin/jobs"
$headers = @{
"Authorization" = $apikey
}
$query_params = @{
# "page[limit]" = 100 # This changes the default page size to 100
# "filter" = "jobType eq 'RESTORE'" # This adds a filter to only show the restore jobs
}
$response = Invoke-WebRequest `
-Uri $uri `
-Method GET `
-Body $query_params `
-ContentType $content_type `
-Headers $headers
if ($response.StatusCode -ne 200)
{
throw "Unable to get the Netbackup jobs!"
}
$content = (ConvertFrom-Json -InputObject $response)
# This prints all the available attributes
#$content.data.attributes | Format-Table -AutoSize
$properties =
@{Label = "Job ID"; Expression = { $_.attributes.jobId }},
@{Label = "Type"; Expression = { $_.attributes.jobType }},
@{Label = "State"; Expression = { $_.attributes.state }},
@{Label = "Status"; Expression = { $_.attributes.status }}
$content.data | Format-Table -AutoSize -Property $properties
11-10-2021 06:24 AM
Hi @Krutons ,
thanks for the scripts, I will try to run them once I rearrange the formatting.
Additionally, I have a question, on the page ''https://<master server>:1556/api-docs/index.html" I am able to run the API call and I can get the answer.
However, if I run the API call in the different web brower tab (https://server_name:1556/netbackup/admin/jobs), I see response "Access denied". So, is it possible to add the API key within the API call url?
11-10-2021 06:41 AM - edited 11-10-2021 06:45 AM
You can't make an API call from the browser like that, that I am aware of. You could download Postman and use that to run test curls.
When you invoke the web request like the following, this is your 'GET'.
$response = Invoke-WebRequest `
-Uri $uri `
-Method GET `
-Body $query_params `
-ContentType $content_type `
-Headers $headers
Your authorization is in the header as stated in a variable above.
$headers = @{ "Authorization" = $apikey }
The uri is composed of several components.
$port = 1556
$basepath = "https://" + $nbmaster + ":" + $port + "/netbackup"
$uri = $basepath + "/admin/jobs"
You shouldn't have to do any formatting for the script that I had previously posted. Simply copy the code, paste it into a .ps1 file. Then open powershell and run the script, for example jobs.ps1 -nbmaster <server name> -apikey <key you generated> and it should return the default 10 most recent jobs.
If you understand these concepts, we can get into it more. If you need some more explanations, let me know.
11-10-2021 07:24 AM
Hi @Krutons ,
hm, this is the thing, why I need to run the API call within the Powershell script in order to remotely run the API call...
Unfortunately, the jobs.ps1 script returns no value/no output :(
11-10-2021 08:21 AM - edited 11-10-2021 08:25 AM
That's strange, I can copy that code and it works fine. Does it throw an error? If you want you can download the VOX_Test.txt and save it as a .ps1 file and try that.
11-15-2021 02:47 AM
Hi @Krutons
I can confirm that it works. After downloading the VOX_Test.txt file and renaming it, the script works well!