|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +This sample script demonstrates how to use NetBackup API to generate a new reissue token. |
| 4 | +.DESCRIPTION |
| 5 | +This script will generate a new reissue token for the host specified in -nbclient. |
| 6 | +.EXAMPLE |
| 7 | +./Get-NB-ReissueToken.ps1 -nbmaster "nb-master.example.com" -username "administrator" -password "secret" -nbclient "nb-client.example.com" |
| 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]$nbclient = $(throw "Please specify the client using -nbclient 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 | + # Add 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 host UUID for a hostname... |
| 91 | +##################################################################### |
| 92 | + |
| 93 | +$uri = $basepath + "/config/hosts" |
| 94 | + |
| 95 | +$headers = @{ |
| 96 | + "Authorization" = $content.token |
| 97 | +} |
| 98 | + |
| 99 | +$query_params = @{"name"=$nbclient} |
| 100 | + |
| 101 | +$response = Invoke-WebRequest ` |
| 102 | + -Uri $uri ` |
| 103 | + -Method GET ` |
| 104 | + -Body $query_params ` |
| 105 | + -ContentType $content_type ` |
| 106 | + -Headers $headers |
| 107 | + |
| 108 | +if ($response.StatusCode -ne 200) |
| 109 | +{ |
| 110 | + throw "Unable to get the UUID for client $nbclient!" |
| 111 | +} |
| 112 | + |
| 113 | +$myToken = $content.token |
| 114 | + |
| 115 | +$content = (ConvertFrom-Json -InputObject $response) |
| 116 | + |
| 117 | +# capture the uuid... |
| 118 | +$uuid = $content.uuid |
| 119 | + |
| 120 | +##################################################################### |
| 121 | +# Get Reissue token |
| 122 | +##################################################################### |
| 123 | + |
| 124 | +$uri = $basepath + "/security/securitytokens" |
| 125 | + |
| 126 | +$headers = @{ |
| 127 | + "Authorization" = $myToken |
| 128 | +} |
| 129 | + |
| 130 | +$body = @{ |
| 131 | + allowedCount=1 |
| 132 | + hostId=$uuid |
| 133 | + tokenName=$nbclient + "_reissue" |
| 134 | + type=1 |
| 135 | + validFor=86400 |
| 136 | +} |
| 137 | +$response = Invoke-WebRequest ` |
| 138 | + -Uri $uri ` |
| 139 | + -Method POST ` |
| 140 | + -Body (ConvertTo-Json -InputObject $body) ` |
| 141 | + -ContentType $content_type ` |
| 142 | + -Headers $headers |
| 143 | + |
| 144 | +if ($response.StatusCode -ne 201) |
| 145 | +{ |
| 146 | + throw "Unable to connect to the Netbackup master server!" |
| 147 | +} |
| 148 | + |
| 149 | +$content = (ConvertFrom-Json -InputObject $response) |
| 150 | + |
| 151 | +$content |
0 commit comments