Skip to content

Commit 015bed0

Browse files
committed
create policy in one step in powershell
1 parent bb45bf4 commit 015bed0

File tree

3 files changed

+295
-373
lines changed

3 files changed

+295
-373
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,10 @@ Pre-requisites:
2929
- NetBackup 8.1.1 or higher
3030
- See script README for perl requirements and usage
3131

32+
#### Executing the recipes in PowerShell
33+
Pre-requisites:
34+
- NetBackup 8.1.2 or higher
35+
- PowerShell 4.0 or higher
36+
37+
Use the following commands to run the PowerShell samples.
38+
- `.\create_policy_in_one_step.ps1 -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName>] [-domainType <domainType>]`
Lines changed: 288 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,288 @@
1+
<#
2+
.SYNOPSIS
3+
This sample script demonstrates the use of NetBackup Policy REST APIs.
4+
.DESCRIPTION
5+
This script can be run using NetBackup 8.1.2 and higher.
6+
It creates a policy with the default values for policy type specific attributes, adds a client, schedule, and backup selection to it in one step at the time of creating policy
7+
.EXAMPLE
8+
./create_policy_in_one_step.ps1 -nbmaster <masterServer> -username <username> -password <password> [-domainName <domainName> -domainType <domainType>]
9+
#>
10+
11+
#Requires -Version 4.0
12+
13+
Param (
14+
[string]$nbmaster = $(Throw "Please specify the name of the NetBackup Master Server using the -nbmaster 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://" + $nbmaster + ":" + $port + "/netbackup/"
27+
$policiesUri = "config/policies/";
28+
$contentType = "application/vnd.netbackup+json;version=2.0"
29+
$testPolicyName = "vmware_test_policy"
30+
$testClientName = "MEDIA_SERVER"
31+
$testScheduleName = "vmware_test_schedule"
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+
# Create a policy with default attribute values, but
105+
# custom schedules, backupselections and clients
106+
#################################################
107+
Function CreatePolicy()
108+
{
109+
$uri = $baseUri + $policiesUri
110+
111+
$clients = @{
112+
hardware="VMware"
113+
hostName="MEDIA_SERVER"
114+
OS="VMware"
115+
}
116+
117+
$backupSelections = "vmware:/?filter=Displayname Contains 'rsv' OR Displayname Contains 'mtv'"
118+
119+
$schedules = @{
120+
scheduleName="sched-9-weeks"
121+
acceleratorForcedRescan=$false
122+
backupCopies=@{
123+
priority=9999
124+
copies=@(@{
125+
mediaOwner="owner1"
126+
storage=$null
127+
retentionPeriod=@{
128+
value=9
129+
unit="WEEKS"
130+
}
131+
volumePool="NetBackup"
132+
failStrategy="Continue"
133+
}
134+
)
135+
}
136+
backupType="Full Backup"
137+
excludeDates=@{
138+
lastDayOfMonth=$true
139+
recurringDaysOfWeek=@("4:6", "2:5")
140+
recurringDaysOfMonth=@(10)
141+
specificDates=@("2000-1-1", "2016-2-30")
142+
}
143+
frequencySeconds=4800
144+
includeDates=@{
145+
lastDayOfMonth=$true
146+
recurringDaysOfWeek=@("2:3", "3:4")
147+
recurringDaysOfMonth=@(10,13)
148+
specificDates=@("2016-12-31")
149+
}
150+
mediaMultiplexing=2
151+
retriesAllowedAfterRunDay=$true
152+
scheduleType="Calendar"
153+
snapshotOnly=$false
154+
startWindow=@(@{dayOfWeek=1
155+
startSeconds=14600
156+
durationSeconds=24600},
157+
@{dayOfWeek=2
158+
startSeconds=14600
159+
durationSeconds=24600},
160+
@{dayOfWeek=3
161+
startSeconds=14600
162+
durationSeconds=24600},
163+
@{dayOfWeek=4
164+
startSeconds=14600
165+
durationSeconds=24600},
166+
@{dayOfWeek=5
167+
startSeconds=14600
168+
durationSeconds=24600},
169+
@{dayOfWeek=6
170+
startSeconds=14600
171+
durationSeconds=24600},
172+
@{dayOfWeek=7
173+
startSeconds=14600
174+
durationSeconds=24600}
175+
)
176+
syntheticBackup=$false
177+
storageIsSLP=$false
178+
}
179+
180+
$policy = @{
181+
policyName=$testPolicyName
182+
policyType="VMware"
183+
policyAttributes=@{}
184+
clients=@($clients)
185+
schedules=@($schedules)
186+
backupSelections=@{selections=@($backupSelections)}
187+
}
188+
189+
$data = @{
190+
type="policy"
191+
id=$testPolicyName
192+
attributes=@{policy=$policy}
193+
}
194+
195+
$body = @{data=$data} | ConvertTo-Json -Depth 9
196+
197+
Write-Host "`nSending a POST request to create $testPolicyName..."
198+
$response = Invoke-WebRequest `
199+
-Uri $uri `
200+
-Method POST `
201+
-Body $body `
202+
-ContentType $contentType `
203+
-Headers $headers
204+
205+
if ($response.StatusCode -ne 204)
206+
{
207+
throw "Unable to create policy $testPolicyName."
208+
}
209+
210+
Write-Host "$testPolicyName created successfully.`n"
211+
$response = (ConvertFrom-Json -InputObject $response)
212+
}
213+
214+
#####################
215+
# List all policies
216+
#####################
217+
Function ListPolicies()
218+
{
219+
$uri = $baseUri + $policiesUri
220+
221+
Write-Host "`nSending a GET request to list all policies...`n"
222+
$response = Invoke-WebRequest `
223+
-Uri $uri `
224+
-Method GET `
225+
-ContentType $contentType `
226+
-Headers $headers
227+
228+
if ($response.StatusCode -ne 200)
229+
{
230+
throw "Unable to list policies.`n"
231+
}
232+
233+
Write-Host $response
234+
}
235+
236+
#################
237+
# Read a policy
238+
#################
239+
Function ReadPolicy()
240+
{
241+
$uri = $baseUri + $policiesUri + $testPolicyName
242+
243+
Write-Host "`nSending a GET request to read policy $testPolicyName...`n"
244+
$response = Invoke-WebRequest `
245+
-Uri $uri `
246+
-Method GET `
247+
-ContentType $contentType `
248+
-Headers $headers
249+
250+
if ($response.StatusCode -ne 200)
251+
{
252+
throw "Unable to read policy $testPolicyName.`n"
253+
}
254+
255+
Write-Host $response
256+
}
257+
258+
###################
259+
# Delete a policy
260+
###################
261+
Function DeletePolicy()
262+
{
263+
$uri = $baseUri + $policiesUri + $testPolicyName
264+
265+
Write-Host "`nSending a DELETE request to delete policy $testPolicyName..."
266+
267+
$response = Invoke-WebRequest `
268+
-Uri $uri `
269+
-Method DELETE `
270+
-ContentType $contentType `
271+
-Headers $headers
272+
273+
if ($response.StatusCode -ne 204)
274+
{
275+
throw "Unable to delete policy $testPolicyName.`n"
276+
}
277+
278+
Write-Host "$testPolicyName deleted successfully.`n"
279+
}
280+
281+
Setup
282+
$loginResponse = Login
283+
$headers = @{"Authorization" = $loginResponse.token}
284+
CreatePolicy
285+
ListPolicies
286+
ReadPolicy
287+
DeletePolicy
288+
ListPolicies

0 commit comments

Comments
 (0)