Skip to content

Commit 2dd9a05

Browse files
authored
Vmware assets ps scripts
Adding powershell scripts for GET VMware assets
1 parent b75ce85 commit 2dd9a05

3 files changed

Lines changed: 378 additions & 0 deletions

File tree

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# NetBackup VMware Asset API Code Samples for PowerShell
2+
3+
This directory contains PowerShell scripts demonstrating the use of NetBackup Asset Service APIs for creating and retrieving VMware assets.
4+
5+
#### Disclaimer
6+
7+
The samples are provided only for reference and not meant for production use.
8+
9+
#### Executing the recipes in PowerShell
10+
11+
##### Prerequisites:
12+
13+
- NetBackup 8.3 or higher
14+
- PowerShell 4.0 or higher
15+
16+
##### Script Details
17+
18+
- get_vmware_assets script uses the NetBackup Asset Service API to get the VMware workload assets (filtered by the given filter if specified). It prints the details such as asset display name, instance Id, vCenter and the plan names that the asset is protected by.
19+
Note: assetsFilter (should be in OData format; refer to the NetBackup API documentation) can be used to filter the assets returned. It is optional; if not specified the script will print all the VM assets. Redirect the script output to a file to avoid printing the details on terminal.
20+
21+
- create_vmware_asset_group script creates a VMware asset group using sample data provided inside the script. Redirect the script output to a file to
22+
avoid printing the details on terminal.
23+
24+
##### Running the Scripts
25+
26+
Use the following commands to run the PowerShell sample:
27+
- .\get_vmware_assets.ps1 -MasterServer <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>] [-assetType <vm|vmGroup>] [-assetsFilter <filter>]
28+
- .\create_vmware_asset_group.ps1 -MasterServer <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
<#
2+
.SYNOPSIS
3+
This sample script demonstrates the use of NetBackup APIs for creating VMware asset group
4+
.DESCRIPTION
5+
This script can be run using NetBackup 8.3 and higher.
6+
It creates a VMware asset group
7+
.EXAMPLE
8+
./create_vmware_asset_group.ps1 -MasterServer <masterServer> -username <username> -password <password> [-domainName <domainName> -domainType <domainType>]
9+
#>
10+
11+
#Requires -Version 4.0
12+
13+
Param (
14+
[string]$MasterServer = $(Throw "Please specify the name of the NetBackup Master Server using the -MasterServer parameter."),
15+
[string]$username = $(Throw "Please specify the user name using the -username parameter."),
16+
[string]$password = $(Throw "Please specify the password using the -password parameter."),
17+
[string]$domainName,
18+
[string]$domainType
19+
)
20+
21+
####################
22+
# Global Variables
23+
####################
24+
25+
$port = 1556
26+
$baseUri = "https://" + $MasterServer + ":" + $port + "/netbackup/"
27+
$assetServiceUri = "asset-service/queries";
28+
$contentType = "application/vnd.netbackup+json;version=4.0"
29+
30+
31+
###############################################################
32+
# Setup to allow self-signed certificates and enable TLS v1.2
33+
###############################################################
34+
Function Setup()
35+
{
36+
# Allow self-signed certificates
37+
if ([System.Net.ServicePointManager]::CertificatePolicy -notlike 'TrustAllCertsPolicy')
38+
{
39+
Add-Type -TypeDefinition @"
40+
using System.Net;
41+
using System.Security.Cryptography.X509Certificates;
42+
public class TrustAllCertsPolicy : ICertificatePolicy {
43+
public bool CheckValidationResult(
44+
ServicePoint srvPoint, X509Certificate certificate,
45+
WebRequest request, int certificateProblem) {
46+
return true;
47+
}
48+
}
49+
"@
50+
[System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName TrustAllCertsPolicy
51+
}
52+
53+
# Force TLS v1.2
54+
try {
55+
if ([Net.ServicePointManager]::SecurityProtocol -notcontains 'Tls12') {
56+
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
57+
}
58+
}
59+
catch {
60+
Write-Host "`n"$_.Exception.InnerException.Message
61+
}
62+
}
63+
64+
######################################
65+
# Login to the NetBackup webservices
66+
######################################
67+
68+
Function Login()
69+
{
70+
$uri = $baseUri + "login"
71+
72+
$body = @{
73+
userName=$username
74+
password=$password
75+
}
76+
if ($domainName -ne "") {
77+
$body.add("domainName", $domainName)
78+
}
79+
if ($domainType -ne "") {
80+
$body.add("domainType", $domainType)
81+
}
82+
Write-Host "`nSending a POST request to login to the NetBackup webservices..."
83+
84+
$response = Invoke-WebRequest `
85+
-Uri $uri `
86+
-Method POST `
87+
-Body (ConvertTo-Json -InputObject $body) `
88+
-ContentType $contentType
89+
90+
if ($response.StatusCode -ne 201)
91+
{
92+
throw "Unable to connect to the NetBackup Master Server"
93+
}
94+
95+
Write-Host "Login successful.`n"
96+
$response = (ConvertFrom-Json -InputObject $response)
97+
return $response
98+
}
99+
100+
101+
#################################################
102+
# Create Asset Group
103+
#################################################
104+
Function createAssetGroup()
105+
{
106+
$uri = $baseUri + $assetServiceUri
107+
108+
$assetGroupDataJson = '{
109+
"data": {
110+
"type": "query",
111+
"attributes": {
112+
"queryName": "create-or-update-assets",
113+
"workloads": ["vmware"],
114+
"parameters": {
115+
"objectList": [
116+
{
117+
"correlationId": "corr-groupvcfdf",
118+
"type": "vmwareGroupAsset",
119+
"assetGroup": {
120+
"description": "sampleDescription",
121+
"assetType": "vmGroup",
122+
"filterConstraint": "sampleMachine",
123+
"oDataQueryFilter": "true",
124+
"commonAssetAttributes": {
125+
"displayName": "sampleGroup249",
126+
"workloadType": "vmware",
127+
"protectionCapabilities": {
128+
"isProtectable": "YES",
129+
"isProtectableReason": "sampleReason",
130+
"isRecoverable": "NO",
131+
"isRecoverableReason": "sampleReason"
132+
},
133+
"detection": {
134+
"detectionMethod": "MANUAL"
135+
}
136+
}
137+
}
138+
}
139+
]
140+
}
141+
}
142+
}
143+
}'
144+
145+
Write-Host "Creating Asset Group.`n"
146+
147+
$response_create_asset_group = Invoke-WebRequest `
148+
-Uri $uri `
149+
-Method POST `
150+
-Body ($assetGroupDataJson) `
151+
-ContentType $contentType `
152+
-Headers $headers
153+
154+
if ($response_create_asset_group.StatusCode -ne 201)
155+
{
156+
throw "Unable to create asset group."
157+
}
158+
159+
Write-Host "asset group created successfully.`n"
160+
echo $response_create_asset_group
161+
Write-Host $response_create_asset_group
162+
163+
$response_create_stu = (ConvertFrom-Json -InputObject $response_create_asset_group)
164+
}
165+
166+
Setup
167+
$loginResponse = Login
168+
$headers = @{"Authorization" = $loginResponse.token}
169+
createAssetGroup
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
<#
2+
.SYNOPSIS
3+
This sample script demonstrates the use of NetBackup APIs for retieving VMware plugin information.
4+
.DESCRIPTION
5+
This script can be run using NetBackup 8.3 and higher.
6+
It retrieves VMware asset data
7+
.EXAMPLE
8+
./get_VMware_Asset_Data.ps1 -MasterServer <masterServer> -username <username> -password <password> [-domainName <domainName> -domainType <domainType>][-assetType <vm|vmGroup>][-assetsFilter <filter>]
9+
#>
10+
11+
#Requires -Version 4.0
12+
13+
Param (
14+
[string]$MasterServer = $(Throw "Please specify the name of the NetBackup Master Server using the -MasterServer parameter."),
15+
[string]$username = $(Throw "Please specify the user name using the -username parameter."),
16+
[string]$password = $(Throw "Please specify the password using the -password parameter."),
17+
[string]$domainName,
18+
[string]$domainType,
19+
[string]$assetType,
20+
[string]$assetsFilter
21+
)
22+
23+
####################
24+
# Global Variables
25+
####################
26+
27+
$port = 1556
28+
$baseUri = "https://" + $MasterServer + ":" + $port + "/netbackup/"
29+
$assetServiceUri = "asset-service/workloads/vmware/assets?";
30+
$contentType = "application/vnd.netbackup+json;version=4.0"
31+
32+
33+
###############################################################
34+
# Setup to allow self-signed certificates and enable TLS v1.2
35+
###############################################################
36+
Function Setup()
37+
{
38+
# Allow self-signed certificates
39+
if ([System.Net.ServicePointManager]::CertificatePolicy -notlike 'TrustAllCertsPolicy')
40+
{
41+
Add-Type -TypeDefinition @"
42+
using System.Net;
43+
using System.Security.Cryptography.X509Certificates;
44+
public class TrustAllCertsPolicy : ICertificatePolicy {
45+
public bool CheckValidationResult(
46+
ServicePoint srvPoint, X509Certificate certificate,
47+
WebRequest request, int certificateProblem) {
48+
return true;
49+
}
50+
}
51+
"@
52+
[System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName TrustAllCertsPolicy
53+
}
54+
55+
# Force TLS v1.2
56+
try {
57+
if ([Net.ServicePointManager]::SecurityProtocol -notcontains 'Tls12') {
58+
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
59+
}
60+
}
61+
catch {
62+
Write-Host "`n"$_.Exception.InnerException.Message
63+
}
64+
}
65+
66+
######################################
67+
# Login to the NetBackup webservices
68+
######################################
69+
70+
Function Login()
71+
{
72+
$uri = $baseUri + "login"
73+
74+
$body = @{
75+
userName=$username
76+
password=$password
77+
}
78+
if ($domainName -ne "") {
79+
$body.add("domainName", $domainName)
80+
}
81+
if ($domainType -ne "") {
82+
$body.add("domainType", $domainType)
83+
}
84+
Write-Host "`nSending a POST request to login to the NetBackup webservices..."
85+
86+
$response = Invoke-WebRequest `
87+
-Uri $uri `
88+
-Method POST `
89+
-Body (ConvertTo-Json -InputObject $body) `
90+
-ContentType $contentType
91+
92+
if ($response.StatusCode -ne 201)
93+
{
94+
throw "Unable to connect to the NetBackup Master Server"
95+
}
96+
97+
Write-Host "Login successful.`n"
98+
$response = (ConvertFrom-Json -InputObject $response)
99+
return $response
100+
}
101+
102+
103+
#################################################
104+
# GET Assets
105+
#################################################
106+
Function getAssets()
107+
{
108+
$uri = $baseUri + $assetServiceUri
109+
110+
$default_sort = "sort=commonAssetAttributes.displayName"
111+
112+
if($assetType -eq "vm"){
113+
$assetTypeFilter = "filter=assetType eq 'vm'"
114+
}
115+
elseif($assetType -eq "vmGroup"){
116+
$assetTypeFilter = "filter=assetType eq 'vmGroup'"
117+
}
118+
119+
if(![string]::IsNullOrEmpty($assetsFilter)){
120+
if([string]::IsNullOrEmpty($assetTypeFilter)){
121+
$assetTypeFilter = "filter=" + $assetsFilter
122+
}
123+
else{
124+
$assetTypeFilter = $assetTypeFilter + " and " + $assetsFilter
125+
}
126+
}
127+
128+
$offset = 0
129+
$next = $true
130+
131+
while ($next){
132+
$uri = $baseUri + $assetServiceUri + $assetTypeFilter + "&" + $default_sort + "&page[offset]=$offset"
133+
134+
Write-Host "`nSending a GET request to list all Assets...`n"
135+
136+
$response = Invoke-WebRequest `
137+
-Uri $uri `
138+
-Method GET `
139+
-ContentType $contentType `
140+
-Headers $headers
141+
142+
if ($response.StatusCode -ne 200)
143+
{
144+
throw "Unable to get VMware assets.`n"
145+
}
146+
147+
$api_response = (ConvertFrom-Json -InputObject $response)
148+
149+
if($assetType -eq "vmGroup"){
150+
$vmGroup_data =
151+
@{Label = "DisplayName"; Expression = { $_.attributes.commonAssetAttributes.displayName}},
152+
@{Label = "filterConstraint"; Expression = { $_.attributes.filterConstraint }},
153+
@{Label = "oDataQueryFilter"; Expression = { $_.attributes.oDataQueryFilter }},
154+
@{Label = "Asset Protection Plans"; Expression = { $_.attributes.commonAssetAttributes.activeProtection.protectionDetailsList }}
155+
156+
$api_response.data | Format-Table -AutoSize -Property $vmGroup_data
157+
}
158+
else{
159+
$vm_data =
160+
@{Label = "DisplayName"; Expression = { $_.attributes.commonAssetAttributes.displayName}},
161+
@{Label = "InstanceUUID"; Expression = { $_.attributes.instanceUuid }},
162+
@{Label = "vCenter"; Expression = { $_.attributes.vCenter }},
163+
@{Label = "Asset Protection Plans"; Expression = { $_.attributes.commonAssetAttributes.activeProtection.protectionDetailsList }}
164+
165+
$api_response.data | Format-Table -AutoSize -Property $vm_data
166+
167+
}
168+
169+
$offset = $offset + $api_response.meta.pagination.limit
170+
171+
if($api_response.meta.pagination.hasNext -eq $false){
172+
$next = $false
173+
}
174+
175+
}
176+
}
177+
178+
Setup
179+
$loginResponse = Login
180+
$headers = @{"Authorization" = $loginResponse.token}
181+
getAssets

0 commit comments

Comments
 (0)