|
| 1 | +<# |
| 2 | +
|
| 3 | +.SYNOPSIS |
| 4 | +This sample script demonstrates the use of NetBackup REST API for listing the alerts. |
| 5 | +
|
| 6 | +.DESCRIPTION |
| 7 | +This script will get a list of netbackup alerts and print the details of the latest 10 alerts in a tabular format. |
| 8 | +NetBackup supports alerts for VMWare job failures. For more information check help document. |
| 9 | +
|
| 10 | +.EXAMPLE |
| 11 | +./Get-NB-Alerts.ps1 -nbmaster "nb-master.example.com" -username "administrator" -password "secret" |
| 12 | +
|
| 13 | +#> |
| 14 | + |
| 15 | +param ( |
| 16 | + [string]$nbmaster = $(throw "Please specify the name of NetBackup master server using -nbmaster parameter."), |
| 17 | + [string]$username = $(throw "Please specify the user name using -username parameter."), |
| 18 | + [string]$password = $(throw "Please specify the password using -password parameter.") |
| 19 | +) |
| 20 | + |
| 21 | +##################################################################### |
| 22 | +# Initial Setup |
| 23 | +# Note: This allows self-signed certificates and enables TLS v1.2 |
| 24 | +##################################################################### |
| 25 | + |
| 26 | +function InitialSetup() |
| 27 | +{ |
| 28 | + # Allow self-signed certificates |
| 29 | + if ([System.Net.ServicePointManager]::CertificatePolicy -notlike 'TrustAllCertsPolicy') |
| 30 | + { |
| 31 | + Add-Type -TypeDefinition @" |
| 32 | + using System.Net; |
| 33 | + using System.Security.Cryptography.X509Certificates; |
| 34 | + public class TrustAllCertsPolicy : ICertificatePolicy { |
| 35 | + public bool CheckValidationResult( |
| 36 | + ServicePoint srvPoint, X509Certificate certificate, |
| 37 | + WebRequest request, int certificateProblem) { |
| 38 | + return true; |
| 39 | + } |
| 40 | + } |
| 41 | +"@ |
| 42 | + [System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName TrustAllCertsPolicy |
| 43 | + |
| 44 | + # Force TLS v1.2 |
| 45 | + try { |
| 46 | + if ([Net.ServicePointManager]::SecurityProtocol -notcontains 'Tls12') { |
| 47 | + [Net.ServicePointManager]::SecurityProtocol += [Net.SecurityProtocolType]::Tls12 |
| 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 | + $response |
| 85 | + throw "Unable to connect to the Netbackup master server!" |
| 86 | +} |
| 87 | + |
| 88 | +$content = (ConvertFrom-Json -InputObject $response) |
| 89 | + |
| 90 | +##################################################################### |
| 91 | +# Get NetBackup Alerts |
| 92 | +##################################################################### |
| 93 | + |
| 94 | +$uri = $basepath + "/manage/alerts" |
| 95 | + |
| 96 | +$headers = @{ |
| 97 | + "Authorization" = $content.token |
| 98 | +} |
| 99 | + |
| 100 | +$query_params = @{ |
| 101 | +# "page[limit]" = 100 # This changes the default page size to 100 |
| 102 | +# "filter" = "subCategory eq 'VMWARE'" # This adds a filter to only show the alerts for job failures of VMWARE policy type |
| 103 | +} |
| 104 | + |
| 105 | +$response = Invoke-WebRequest ` |
| 106 | + -Uri $uri ` |
| 107 | + -Method GET ` |
| 108 | + -Body $query_params ` |
| 109 | + -ContentType $content_type ` |
| 110 | + -Headers $headers |
| 111 | + |
| 112 | +if ($response.StatusCode -ne 200) |
| 113 | +{ |
| 114 | + throw "Unable to get the Netbackup alerts!" |
| 115 | +} |
| 116 | + |
| 117 | +$content = (ConvertFrom-Json -InputObject $response) |
| 118 | + |
| 119 | +# This prints all the available attributes |
| 120 | +# $content.data.attributes | Format-Table -AutoSize |
| 121 | + |
| 122 | +$properties = |
| 123 | + @{Label = "Title"; Expression = { $_.attributes.title }}, |
| 124 | + @{Label = "Generated At"; Expression = { $_.attributes.createdDateTime }}, |
| 125 | + @{Label = "Job ID"; Expression = { $_.attributes.params.jobId }}, |
| 126 | + @{Label = "Status Code"; Expression = { $_.attributes.params.status }}, |
| 127 | + @{Label = "Error Message"; Expression = { $_.attributes.params.errorMsg }}, |
| 128 | + @{Label = "Policy Name"; Expression = { $_.attributes.params.policyName }}, |
| 129 | + @{Label = "Policy Type"; Expression = { $_.attributes.params.policyType }} |
| 130 | + |
| 131 | +$content.data | Format-Table -AutoSize -Property $properties |
0 commit comments