Skip to content

Commit ec101cf

Browse files
author
Pedro da Silveira
committed
Powershell code to get asset by filter, parse the AssetIds, and delete all those assets using the given cleanup time
1 parent 723c5a9 commit ec101cf

File tree

2 files changed

+173
-0
lines changed

2 files changed

+173
-0
lines changed
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
<#
2+
3+
.SYNOPSIS
4+
This sample script demonstrates the use of NetBackup REST API for searching assets
5+
based on a search criteria and delete those assets returned from the search.
6+
7+
.DESCRIPTION
8+
This script will get a list of asset in the AssetDB that matches the data filter and
9+
delete those assets. However, the asset only gets deleted if there is not subscription
10+
associated to the asset, and the last discovered time of the asset is older than the given
11+
cleanupTime.
12+
13+
.EXAMPLE
14+
./Post-NB-Cleanup-Assets.ps1 -nbmaster "nb-master.example.com" -username "administrator" -password "secret" -filter "workloadType eq 'VMware'" -cleanuptime "2018-06-29T15:58:45.678Z"
15+
16+
#>
17+
18+
param (
19+
[string]$nbmaster = $(throw "Please specify the name of NetBackup master server using -nbmaster parameter."),
20+
[string]$username = $(throw "Please specify the user name using -username parameter."),
21+
[string]$password = $(throw "Please specify the password using -password parameter."),
22+
[string]$filter = $(throw "Please specify the filter using -filter paramter"),
23+
[string]$cleanuptime = $(throw "Please specify the cleanuptime using -cleanuptime paramter")
24+
)
25+
26+
#####################################################################
27+
# Initial Setup
28+
# Note: This allows self-signed certificates and enables TLS v1.2
29+
#####################################################################
30+
31+
function InitialSetup()
32+
{
33+
34+
[Net.ServicePointManager]::SecurityProtocol = "Tls12"
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+
[Net.ServicePointManager]::SecurityProtocol = "Tls12"
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 $_.Exception.InnerException.Message
61+
}
62+
}
63+
}
64+
65+
InitialSetup
66+
67+
#####################################################################
68+
# Global Variables
69+
#####################################################################
70+
71+
$port = 1556
72+
$basepath = "https://" + $nbmaster + ":" + $port + "/netbackup"
73+
$content_type1 = "application/vnd.netbackup+json;version=1.0"
74+
$content_type2 = "application/vnd.netbackup+json;version=2.0"
75+
76+
#####################################################################
77+
# Login
78+
#####################################################################
79+
80+
$uri = $basepath + "/login"
81+
82+
$body = @{
83+
userName=$username
84+
password=$password
85+
}
86+
87+
$response = Invoke-WebRequest `
88+
-Uri $uri `
89+
-Method POST `
90+
-Body (ConvertTo-Json -InputObject $body) `
91+
-ContentType $content_type1
92+
93+
if ($response.StatusCode -ne 201)
94+
{
95+
throw "Unable to connect to the Netbackup master server!"
96+
}
97+
98+
$content = (ConvertFrom-Json -InputObject $response)
99+
100+
#####################################################################
101+
# Get NetBackup Assets
102+
#####################################################################
103+
104+
$uri = $basepath + "/assets?filter=" + $filter
105+
106+
$headers = @{
107+
"Authorization" = $content.token
108+
}
109+
110+
$response = Invoke-WebRequest `
111+
-Uri $uri `
112+
-Method GET `
113+
-ContentType $content_type2 `
114+
-Headers $headers
115+
116+
if ($response.StatusCode -ne 200)
117+
{
118+
throw "Unable to get the list of Netbackup Assets!"
119+
}
120+
121+
$content_get = (ConvertFrom-Json -InputObject $response)
122+
123+
if (!$content_get.data){
124+
echo ""
125+
echo "Your filter: $filter did not return any asset."
126+
echo ""
127+
} else {
128+
echo ""
129+
echo ""
130+
echo "These are the assets returned by your filter:"
131+
echo ""
132+
133+
# This prints all the available attributes
134+
#$content_get.data.attributes | Format-Table -AutoSize
135+
136+
$properties =
137+
@{Label = "Asset ID"; Expression = { $_.id }},
138+
@{Label = "ProviderGeneratedId"; Expression = { $_.attributes.providerGeneratedId }},
139+
@{Label = "DisplayName"; Expression = { $_.attributes.displayName }},
140+
@{Label = "AssetType"; Expression = { $_.attributes.assetType }},
141+
@{Label = "WorkloadType"; Expression = { $_.attributes.workloadType }}
142+
143+
$content_get.data | Format-Table -AutoSize -Property $properties
144+
145+
# Prepara payload for the Asset Cleanup API call
146+
$assetIds = $content_get.data.id -join '","'
147+
$assetIds = '"' + $assetIds + '"'
148+
149+
$body_cleanupRequest = '{"data":{
150+
"type":"assetCleanup", "id":"id",
151+
"attributes":{
152+
"cleanupTime":"' + $cleanupTime + '",
153+
"assetIds":[' + $assetIds + ']}}}'
154+
155+
#####################################################################
156+
# Delete NetBackup Assets
157+
#####################################################################
158+
159+
$cleanup_uri = $basepath + "/assets/asset-cleanup"
160+
161+
$response_delete = Invoke-WebRequest `
162+
-Uri $cleanup_uri `
163+
-Method POST `
164+
-Body ($body_cleanupRequest) `
165+
-ContentType $content_type2 `
166+
-Headers $headers
167+
168+
if ($response_delete.StatusCode -ne 204)
169+
{
170+
throw "Unable to delete Assets " + $assetIds
171+
}
172+
}

snippets/powershell/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ These scripts are only meant to be used as a reference. If you intend to use the
1616
Use the following commands to run the PowerShell samples.
1717
- `.\Get-NB-Images.ps1 -nbmaster <masterServer> -username <username> -password <password>`
1818
- `.\Get-NB-Jobs.ps1 -nbmaster <masterServer> -username <username> -password <password>`
19+
- `.\Post-NB-Cleanup-Assets.ps1 -nbmaster <masterServer> -username <username> -password <password> -filter <workloadType eq 'VMware'> -cleanupTime 2018-06-29T15:18:45.678Z`

0 commit comments

Comments
 (0)