Skip to content

Commit 69cf09c

Browse files
Shreyas AgnihotriShreyas Agnihotri
authored andcommitted
Added powershell code samples
1 parent a59f071 commit 69cf09c

File tree

3 files changed

+269
-2
lines changed

3 files changed

+269
-2
lines changed

README.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,14 @@
1-
# netbackup-api-code-samples
2-
This repository contains code samples that demonstrate the use of NetBackup REST API.
1+
### NetBackup API Code Samples
2+
3+
Contains code samples to invoke NetBackup REST API using different programming languages.
4+
5+
#### Disclaimer
6+
These scripts are only meant to be used as a reference. Please do not use these in production.
7+
8+
#### Executing the scripts in PowerShell
9+
Pre-requisites:
10+
- Powershell 5.0 or higher
11+
12+
Use the following commands to run the sample powershell scripts.
13+
- `.\Get-NB-Images.ps1 -nbmaster <masterServer> -username <username> -password <password>`
14+
- `.\Get-NB-Jobs.ps1 -nbmaster <masterServer> -username <username> -password <password>`

powershell/Get-NB-Images.ps1

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
<#
2+
3+
.SYNOPSIS
4+
This sample script demonstrates the use of NetBackup REST API for listing the backup images.
5+
6+
.DESCRIPTION
7+
This script will get a list of backup images in the last 24 hours and print the details of the last 10 images in a tabular format.
8+
9+
10+
.EXAMPLE
11+
./Get-NB-Images.ps1 -nbmaster "nb-master.example.com" -username "administrator" -password "secret"
12+
13+
#>
14+
15+
param (
16+
[string]$nbmaster = $(throw "Please specify the name of NetBackup master server using -nbmaster parameter."),
17+
[string]$username = $(throw "Please specify the user name using -username parameter."),
18+
[string]$password = $(throw "Please specify the password using -password parameter.")
19+
)
20+
21+
#####################################################################
22+
# Initial Setup
23+
# Note: This allows self-signed certificates and enables TLS v1.2
24+
#####################################################################
25+
26+
function InitialSetup()
27+
{
28+
# Allow self-signed certificates
29+
if ([System.Net.ServicePointManager]::CertificatePolicy -notlike 'TrustAllCertsPolicy')
30+
{
31+
Add-Type -TypeDefinition @"
32+
using System.Net;
33+
using System.Security.Cryptography.X509Certificates;
34+
public class TrustAllCertsPolicy : ICertificatePolicy {
35+
public bool CheckValidationResult(
36+
ServicePoint srvPoint, X509Certificate certificate,
37+
WebRequest request, int certificateProblem) {
38+
return true;
39+
}
40+
}
41+
"@
42+
[System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName TrustAllCertsPolicy
43+
44+
# Force TLS v1.2
45+
try {
46+
if ([Net.ServicePointManager]::SecurityProtocol -notcontains 'Tls12') {
47+
[Net.ServicePointManager]::SecurityProtocol += [Net.SecurityProtocolType]::Tls12
48+
}
49+
}
50+
catch {
51+
Write-Host $_.Exception.InnerException.Message
52+
}
53+
}
54+
}
55+
56+
InitialSetup
57+
58+
#####################################################################
59+
# Global Variables
60+
#####################################################################
61+
62+
$port = 1556
63+
$basepath = "https://" + $nbmaster + ":" + $port + "/netbackup"
64+
$content_type = "application/vnd.netbackup+json;version=1.0"
65+
66+
#####################################################################
67+
# Login
68+
#####################################################################
69+
70+
$uri = $basepath + "/login"
71+
72+
$body = @{
73+
userName=$username
74+
password=$password
75+
}
76+
77+
$response = Invoke-WebRequest `
78+
-Uri $uri `
79+
-Method POST `
80+
-Body (ConvertTo-Json -InputObject $body) `
81+
-ContentType $content_type
82+
83+
if ($response.StatusCode -ne 201)
84+
{
85+
throw "Unable to connect to the Netbackup master server!"
86+
}
87+
88+
$content = (ConvertFrom-Json -InputObject $response)
89+
90+
#####################################################################
91+
# Get NetBackup Images
92+
#####################################################################
93+
94+
$uri = $basepath + "/catalog/images"
95+
96+
$headers = @{
97+
"Authorization" = $content.token
98+
}
99+
100+
$query_params = @{
101+
# "page[limit]" = 100 # This changes the default page size to 100
102+
# "filter" = "policyType eq 'VMware'" # This adds a filter to only show VMware backup images
103+
}
104+
105+
$response = Invoke-WebRequest `
106+
-Uri $uri `
107+
-Method GET `
108+
-Body $query_params `
109+
-ContentType $content_type `
110+
-Headers $headers
111+
112+
if ($response.StatusCode -ne 200)
113+
{
114+
throw "Unable to get the list of Netbackup images!"
115+
}
116+
117+
$content = (ConvertFrom-Json -InputObject $response)
118+
119+
# This prints all the available attributes
120+
#$content.data.attributes | Format-Table -AutoSize
121+
122+
$properties =
123+
@{Label = "Image ID"; Expression = { $_.id }},
124+
@{Label = "Policy"; Expression = { $_.attributes.policyName }},
125+
@{Label = "Client"; Expression = { $_.attributes.clientName }},
126+
@{Label = "Backup Time"; Expression = { $_.attributes.backupTime }}
127+
128+
$content.data | Format-Table -AutoSize -Property $properties

powershell/Get-NB-Jobs.ps1

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<#
2+
3+
.SYNOPSIS
4+
This sample script demonstrates the use of NetBackup REST API for listing the jobs.
5+
6+
.DESCRIPTION
7+
This script will get a list of netbackup jobs and print the details of the last 10 jobs in a tabular format.
8+
9+
10+
.EXAMPLE
11+
./Get-NB-Jobs.ps1 -nbmaster "nb-master.example.com" -username "administrator" -password "secret"
12+
13+
#>
14+
15+
param (
16+
[string]$nbmaster = $(throw "Please specify the name of NetBackup master server using -nbmaster parameter."),
17+
[string]$username = $(throw "Please specify the user name using -username parameter."),
18+
[string]$password = $(throw "Please specify the password using -password parameter.")
19+
)
20+
21+
#####################################################################
22+
# Initial Setup
23+
# Note: This allows self-signed certificates and enables TLS v1.2
24+
#####################################################################
25+
26+
function InitialSetup()
27+
{
28+
# Allow self-signed certificates
29+
if ([System.Net.ServicePointManager]::CertificatePolicy -notlike 'TrustAllCertsPolicy')
30+
{
31+
Add-Type -TypeDefinition @"
32+
using System.Net;
33+
using System.Security.Cryptography.X509Certificates;
34+
public class TrustAllCertsPolicy : ICertificatePolicy {
35+
public bool CheckValidationResult(
36+
ServicePoint srvPoint, X509Certificate certificate,
37+
WebRequest request, int certificateProblem) {
38+
return true;
39+
}
40+
}
41+
"@
42+
[System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName TrustAllCertsPolicy
43+
44+
# Force TLS v1.2
45+
try {
46+
if ([Net.ServicePointManager]::SecurityProtocol -notcontains 'Tls12') {
47+
[Net.ServicePointManager]::SecurityProtocol += [Net.SecurityProtocolType]::Tls12
48+
}
49+
}
50+
catch {
51+
Write-Host $_.Exception.InnerException.Message
52+
}
53+
}
54+
}
55+
56+
InitialSetup
57+
58+
#####################################################################
59+
# Global Variables
60+
#####################################################################
61+
62+
$port = 1556
63+
$basepath = "https://" + $nbmaster + ":" + $port + "/netbackup"
64+
$content_type = "application/vnd.netbackup+json;version=1.0"
65+
66+
#####################################################################
67+
# Login
68+
#####################################################################
69+
70+
$uri = $basepath + "/login"
71+
72+
$body = @{
73+
userName=$username
74+
password=$password
75+
}
76+
$response = Invoke-WebRequest `
77+
-Uri $uri `
78+
-Method POST `
79+
-Body (ConvertTo-Json -InputObject $body) `
80+
-ContentType $content_type
81+
82+
if ($response.StatusCode -ne 201)
83+
{
84+
throw "Unable to connect to the Netbackup master server!"
85+
}
86+
87+
$content = (ConvertFrom-Json -InputObject $response)
88+
89+
#####################################################################
90+
# Get NetBackup Jobs
91+
#####################################################################
92+
93+
$uri = $basepath + "/admin/jobs"
94+
95+
$headers = @{
96+
"Authorization" = $content.token
97+
}
98+
99+
$query_params = @{
100+
# "page[limit]" = 100 # This changes the default page size to 100
101+
# "filter" = "jobType eq 'RESTORE'" # This adds a filter to only show the restore jobs
102+
}
103+
104+
$response = Invoke-WebRequest `
105+
-Uri $uri `
106+
-Method GET `
107+
-Body $query_params `
108+
-ContentType $content_type `
109+
-Headers $headers
110+
111+
if ($response.StatusCode -ne 200)
112+
{
113+
throw "Unable to get the Netbackup jobs!"
114+
}
115+
116+
$content = (ConvertFrom-Json -InputObject $response)
117+
118+
# This prints all the available attributes
119+
#$content.data.attributes | Format-Table -AutoSize
120+
121+
$properties =
122+
@{Label = "Job ID"; Expression = { $_.attributes.jobId }},
123+
@{Label = "Type"; Expression = { $_.attributes.jobType }},
124+
@{Label = "State"; Expression = { $_.attributes.state }},
125+
@{Label = "Status"; Expression = { $_.attributes.status }}
126+
127+
$content.data | Format-Table -AutoSize -Property $properties

0 commit comments

Comments
 (0)