|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +This sample script demonstrates the use of NetBackup REST API for listing the jobs. |
| 4 | +.DESCRIPTION |
| 5 | +This script will get a list of netbackup jobs and print the details of the last 10 jobs in a tabular format. |
| 6 | +.EXAMPLE |
| 7 | +./Get-NB-PolicyDetails.ps1 -nbmaster "nb-master.example.com" -username "administrator" -password "secret" -nbpolicy "policy_name" |
| 8 | +#> |
| 9 | + |
| 10 | +param ( |
| 11 | + |
| 12 | + [string]$nbmaster = $(throw "Please specify the name of NetBackup master server using -nbmaster parameter."), |
| 13 | + [string]$username = $(throw "Please specify the user name using -username parameter."), |
| 14 | + [string]$password = $(throw "Please specify the password using -password parameter."), |
| 15 | + [string]$nbpolicy = $(throw "Please specify the policy using -nbpolicy parameter.") |
| 16 | + |
| 17 | + ) |
| 18 | + |
| 19 | + |
| 20 | +##################################################################### |
| 21 | +# Initial Setup |
| 22 | +# Note: This allows self-signed certificates and enables TLS v1.2 |
| 23 | +##################################################################### |
| 24 | + |
| 25 | +function InitialSetup() |
| 26 | +{ |
| 27 | + # Allow self-signed certificates |
| 28 | + if ([System.Net.ServicePointManager]::CertificatePolicy -notlike 'TrustAllCertsPolicy') |
| 29 | + { |
| 30 | + Add-Type -TypeDefinition @" |
| 31 | + using System.Net; |
| 32 | + using System.Security.Cryptography.X509Certificates; |
| 33 | + public class TrustAllCertsPolicy : ICertificatePolicy { |
| 34 | + public bool CheckValidationResult( |
| 35 | + ServicePoint srvPoint, X509Certificate certificate, |
| 36 | + WebRequest request, int certificateProblem) { |
| 37 | + return true; |
| 38 | + } |
| 39 | + } |
| 40 | +"@ |
| 41 | + [System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName TrustAllCertsPolicy |
| 42 | + |
| 43 | + # Force TLS v1.2 |
| 44 | + try { |
| 45 | + if ([Net.ServicePointManager]::SecurityProtocol -notmatch 'Tls12') { |
| 46 | + [Net.ServicePointManager]::SecurityProtocol = ([Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12) |
| 47 | + |
| 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=2.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 the detais for a named policy... |
| 91 | +##################################################################### |
| 92 | + |
| 93 | +$uri = $basepath + "/config/policies/" + $nbpolicy |
| 94 | + |
| 95 | +$headers = @{ |
| 96 | + "Authorization" = $content.token |
| 97 | + "X-NetBackup-Policy-Use-Generic-Schema" = $true |
| 98 | +} |
| 99 | + |
| 100 | +$query_params = @{} |
| 101 | + |
| 102 | +$response = Invoke-WebRequest ` |
| 103 | + -Uri $uri ` |
| 104 | + -Method GET ` |
| 105 | + -Body $query_params ` |
| 106 | + -ContentType $content_type ` |
| 107 | + -Headers $headers |
| 108 | + |
| 109 | +if ($response.StatusCode -ne 200) |
| 110 | +{ |
| 111 | + throw "Unable to get the policy details for $nbpolicy!" |
| 112 | +} |
| 113 | + |
| 114 | +$content = (ConvertFrom-Json -InputObject $response) |
| 115 | + |
| 116 | + |
| 117 | +# This prints the majority of policies attributes... |
| 118 | +$content.data.attributes.policy.policyAttributes |
| 119 | + |
| 120 | +# This prints the policy client list... |
| 121 | +#$content.data.attributes.policy.clients.hostname |
| 122 | + |
| 123 | +# This prints the backup selections... |
| 124 | +#$content.data.attributes.policy.backupSelections.selections |
0 commit comments