|
| 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, then deletes the client, schedule and finally deletes the policy. |
| 7 | +.EXAMPLE |
| 8 | +./New-Policy-StepByStep.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 |
| 105 | +################################################# |
| 106 | +Function CreatePolicyWithDefaults() |
| 107 | +{ |
| 108 | + $uri = $baseUri + $policiesUri |
| 109 | + |
| 110 | + $policy = @{ |
| 111 | + policyName=$testPolicyName |
| 112 | + policyType="VMware" |
| 113 | + policyAttributes=@{} |
| 114 | + clients=@() |
| 115 | + schedules=@() |
| 116 | + backupSelections=@{selections=@()} |
| 117 | + } |
| 118 | + |
| 119 | + $data = @{ |
| 120 | + type="policy" |
| 121 | + id=$testPolicyName |
| 122 | + attributes=@{policy=$policy} |
| 123 | + } |
| 124 | + |
| 125 | + $body = @{data=$data} | ConvertTo-Json -Depth 5 |
| 126 | + |
| 127 | + Write-Host "`nSending a POST request to create $testPolicyName with defaults..." |
| 128 | + $response = Invoke-WebRequest ` |
| 129 | + -Uri $uri ` |
| 130 | + -Method POST ` |
| 131 | + -Body $body ` |
| 132 | + -ContentType $contentType ` |
| 133 | + -Headers $headers |
| 134 | + |
| 135 | + if ($response.StatusCode -ne 204) |
| 136 | + { |
| 137 | + throw "Unable to create policy $testPolicyName." |
| 138 | + } |
| 139 | + |
| 140 | + Write-Host "$testPolicyName created successfully.`n" |
| 141 | + $response = (ConvertFrom-Json -InputObject $response) |
| 142 | +} |
| 143 | + |
| 144 | +##################### |
| 145 | +# List all policies |
| 146 | +##################### |
| 147 | +Function ListPolicies() |
| 148 | +{ |
| 149 | + $uri = $baseUri + $policiesUri |
| 150 | + |
| 151 | + Write-Host "`nSending a GET request to list all policies...`n" |
| 152 | + $response = Invoke-WebRequest ` |
| 153 | + -Uri $uri ` |
| 154 | + -Method GET ` |
| 155 | + -ContentType $contentType ` |
| 156 | + -Headers $headers |
| 157 | + |
| 158 | + if ($response.StatusCode -ne 200) |
| 159 | + { |
| 160 | + throw "Unable to list policies.`n" |
| 161 | + } |
| 162 | + |
| 163 | + Write-Host $response |
| 164 | +} |
| 165 | + |
| 166 | +################# |
| 167 | +# Read a policy |
| 168 | +################# |
| 169 | +Function ReadPolicy() |
| 170 | +{ |
| 171 | + $uri = $baseUri + $policiesUri + $testPolicyName |
| 172 | + |
| 173 | + Write-Host "`nSending a GET request to read policy $testPolicyName...`n" |
| 174 | + $response = Invoke-WebRequest ` |
| 175 | + -Uri $uri ` |
| 176 | + -Method GET ` |
| 177 | + -ContentType $contentType ` |
| 178 | + -Headers $headers |
| 179 | + |
| 180 | + if ($response.StatusCode -ne 200) |
| 181 | + { |
| 182 | + throw "Unable to read policy $testPolicyName.`n" |
| 183 | + } |
| 184 | + |
| 185 | + Write-Host $response |
| 186 | +} |
| 187 | + |
| 188 | +################### |
| 189 | +# Delete a policy |
| 190 | +################### |
| 191 | +Function DeletePolicy() |
| 192 | +{ |
| 193 | + $uri = $baseUri + $policiesUri + $testPolicyName |
| 194 | + |
| 195 | + Write-Host "`nSending a DELETE request to delete policy $testPolicyName..." |
| 196 | + |
| 197 | + $response = Invoke-WebRequest ` |
| 198 | + -Uri $uri ` |
| 199 | + -Method DELETE ` |
| 200 | + -ContentType $contentType ` |
| 201 | + -Headers $headers |
| 202 | + |
| 203 | + if ($response.StatusCode -ne 204) |
| 204 | + { |
| 205 | + throw "Unable to delete policy $testPolicyName.`n" |
| 206 | + } |
| 207 | + |
| 208 | + Write-Host "$testPolicyName deleted successfully.`n" |
| 209 | +} |
| 210 | + |
| 211 | +############################ |
| 212 | +# Add a client to a policy |
| 213 | +############################ |
| 214 | +Function AddClient() |
| 215 | +{ |
| 216 | + $uri = $baseUri + $policiesUri + $testPolicyName + "/clients/" + $testClientName |
| 217 | + |
| 218 | + $data = @{ |
| 219 | + type="client" |
| 220 | + attributes=@{ |
| 221 | + hardware="VMware" |
| 222 | + hostName="MEDIA_SERVER" |
| 223 | + OS="VMware" |
| 224 | + } |
| 225 | + } |
| 226 | + |
| 227 | + $body = @{data=$data} | ConvertTo-Json -Depth 3 |
| 228 | + |
| 229 | + Write-Host "`nSending a PUT request to add client $testClientName to policy $testPolicyName..." |
| 230 | + $response = Invoke-WebRequest ` |
| 231 | + -Uri $uri ` |
| 232 | + -Method PUT ` |
| 233 | + -Body $body ` |
| 234 | + -ContentType $contentType ` |
| 235 | + -Headers $headers |
| 236 | + |
| 237 | + if ($response.StatusCode -ne 201) |
| 238 | + { |
| 239 | + throw "Unable to add client $testClientName to policy $testPolicyName.`n" |
| 240 | + } |
| 241 | + |
| 242 | + Write-Host "$testClientName added to $testPolicyName successfully.`n" |
| 243 | +} |
| 244 | + |
| 245 | +###################################### |
| 246 | +# Add a backup selection to a policy |
| 247 | +###################################### |
| 248 | +Function AddBackupSelection() |
| 249 | +{ |
| 250 | + $uri = $baseUri + $policiesUri + $testPolicyName + "/backupselections" |
| 251 | + |
| 252 | + $data = @{ |
| 253 | + type="backupSelection" |
| 254 | + attributes=@{ |
| 255 | + selections=@("vmware:/?filter=Displayname Contains 'rsv' OR Displayname Contains 'mtv'") |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + $body = @{data=$data} | ConvertTo-Json -Depth 3 |
| 260 | + |
| 261 | + Write-Host "`nSending a PUT request to add backupselection to policy $testPolicyName..." |
| 262 | + $response = Invoke-WebRequest ` |
| 263 | + -Uri $uri ` |
| 264 | + -Method PUT ` |
| 265 | + -Body $body ` |
| 266 | + -ContentType $contentType ` |
| 267 | + -Headers $headers |
| 268 | + |
| 269 | + if ($response.StatusCode -ne 204) |
| 270 | + { |
| 271 | + throw "Unable to add backupselection to policy $testPolicyName.`n" |
| 272 | + } |
| 273 | + |
| 274 | + Write-Host "backupselection added to $testPolicyName successfully.`n" |
| 275 | +} |
| 276 | + |
| 277 | +############################ |
| 278 | +# Add a schedule to policy |
| 279 | +############################ |
| 280 | +Function AddSchedule() |
| 281 | +{ |
| 282 | + $uri = $baseUri + $policiesUri + $testPolicyName + "/schedules/" + $testScheduleName |
| 283 | + |
| 284 | + $data = @{ |
| 285 | + type="schedule" |
| 286 | + id=$testScheduleName |
| 287 | + attributes=@{ |
| 288 | + acceleratorForcedRescan=$false |
| 289 | + backupCopies=@{ |
| 290 | + priority=9999 |
| 291 | + copies=@(@{ |
| 292 | + mediaOwner="owner1" |
| 293 | + storage=$null |
| 294 | + retentionPeriod=@{ |
| 295 | + value=9 |
| 296 | + unit="WEEKS" |
| 297 | + } |
| 298 | + volumePool="NetBackup" |
| 299 | + failStrategy="Continue" |
| 300 | + } |
| 301 | + ) |
| 302 | + } |
| 303 | + backupType="Full Backup" |
| 304 | + excludeDates=@{ |
| 305 | + lastDayOfMonth=$true |
| 306 | + recurringDaysOfWeek=@("4:6", "2:5") |
| 307 | + recurringDaysOfMonth=@(10) |
| 308 | + specificDates=@("2000-1-1", "2016-2-30") |
| 309 | + } |
| 310 | + frequencySeconds=4800 |
| 311 | + includeDates=@{ |
| 312 | + lastDayOfMonth=$true |
| 313 | + recurringDaysOfWeek=@("2:3", "3:4") |
| 314 | + recurringDaysOfMonth=@(10,13) |
| 315 | + specificDates=@("2016-12-31") |
| 316 | + } |
| 317 | + mediaMultiplexing=2 |
| 318 | + retriesAllowedAfterRunDay=$true |
| 319 | + scheduleType="Calendar" |
| 320 | + snapshotOnly=$false |
| 321 | + startWindow=@(@{dayOfWeek=1 |
| 322 | + startSeconds=14600 |
| 323 | + durationSeconds=24600}, |
| 324 | + @{dayOfWeek=2 |
| 325 | + startSeconds=14600 |
| 326 | + durationSeconds=24600}, |
| 327 | + @{dayOfWeek=3 |
| 328 | + startSeconds=14600 |
| 329 | + durationSeconds=24600}, |
| 330 | + @{dayOfWeek=4 |
| 331 | + startSeconds=14600 |
| 332 | + durationSeconds=24600}, |
| 333 | + @{dayOfWeek=5 |
| 334 | + startSeconds=14600 |
| 335 | + durationSeconds=24600}, |
| 336 | + @{dayOfWeek=6 |
| 337 | + startSeconds=14600 |
| 338 | + durationSeconds=24600}, |
| 339 | + @{dayOfWeek=7 |
| 340 | + startSeconds=14600 |
| 341 | + durationSeconds=24600} |
| 342 | + ) |
| 343 | + syntheticBackup=$false |
| 344 | + storageIsSLP=$false |
| 345 | + } |
| 346 | + } |
| 347 | + |
| 348 | + $body = @{data=$data} | ConvertTo-Json -Depth 6 |
| 349 | + |
| 350 | + Write-Host "`nSending a PUT request to add schedule $testScheduleName to policy $testPolicyName..." |
| 351 | + $response = Invoke-WebRequest ` |
| 352 | + -Uri $uri ` |
| 353 | + -Method PUT ` |
| 354 | + -Body $body ` |
| 355 | + -ContentType $contentType ` |
| 356 | + -Headers $headers |
| 357 | + |
| 358 | + if ($response.StatusCode -ne 201) |
| 359 | + { |
| 360 | + throw "Unable to add schedule $testScheduleName to policy $testPolicyName.`n" |
| 361 | + } |
| 362 | + |
| 363 | + Write-Host "schedule $testScheduleName added to $testPolicyName successfully.`n" |
| 364 | +} |
| 365 | + |
| 366 | +Setup |
| 367 | +$loginResponse = Login |
| 368 | +$headers = @{"Authorization" = $loginResponse.token} |
| 369 | +CreatePolicyWithDefaults |
| 370 | +ListPolicies |
| 371 | +ReadPolicy |
| 372 | +DeletePolicy |
| 373 | +ListPolicies |
0 commit comments