|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +This sample script demonstrates the use of NetBackup processes REST API. |
| 4 | +.DESCRIPTION |
| 5 | +This script can be run using NetBackup 8.2 or higher version. |
| 6 | +.EXAMPLE |
| 7 | +./get_processes.ps1 -MasterServer <masterServer> -UserName <username> -Password <password> -Client <client> [-DomainName <domainName> -DomainType <domainType>] |
| 8 | +#> |
| 9 | + |
| 10 | +#Requires -Version 4.0 |
| 11 | + |
| 12 | +Param ( |
| 13 | + [string]$MasterServer = $(Throw "Please specify the name of the NetBackup Master Server using the -MasterServer parameter."), |
| 14 | + [string]$UserName = $(Throw "Please specify the user name using the -UserName parameter."), |
| 15 | + [string]$Password = $(Throw "Please specify the password using the -Password parameter."), |
| 16 | + [string]$Client = $(Throw "Please specify the client name using the -Client parameter."), |
| 17 | + [string]$DomainName, |
| 18 | + [string]$DomainType |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +############################################################### |
| 23 | +# Setup to allow self-signed certificates and enable TLS v1.2 |
| 24 | +############################################################### |
| 25 | +Function Setup() |
| 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 | + |
| 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 "`n"$_.Exception.InnerException.Message |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +#################### |
| 56 | +# Global Variables |
| 57 | +#################### |
| 58 | + |
| 59 | +$port = 1556 |
| 60 | +$baseUri = "https://" + $MasterServer + ":" + $port + "/netbackup/" |
| 61 | +$adminHostsUri = "admin/hosts/"; |
| 62 | +$contentType = "application/vnd.netbackup+json;version=3.0" |
| 63 | +$hostName = $Client |
| 64 | +$testServiceName = "bpcd" |
| 65 | + |
| 66 | +###################################### |
| 67 | +# Login to the NetBackup webservices |
| 68 | +###################################### |
| 69 | +Function Login() |
| 70 | +{ |
| 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...`n" |
| 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 | + $content = (ConvertFrom-Json -InputObject $response) |
| 99 | + return $content |
| 100 | +} |
| 101 | + |
| 102 | +######################### |
| 103 | +# Get a Host uuid |
| 104 | +######################### |
| 105 | +Function getUUID() |
| 106 | +{ |
| 107 | + $uri = $baseUri + "config/hosts?filter=" + $hostName |
| 108 | + |
| 109 | + |
| 110 | + Write-Host "`nSending a GET request to get host UUID ..`n" |
| 111 | + $response = Invoke-WebRequest ` |
| 112 | + -Uri $uri ` |
| 113 | + -Method GET ` |
| 114 | + -ContentType $contentType ` |
| 115 | + -Headers $headers |
| 116 | + |
| 117 | + if ($response.StatusCode -ne 200) |
| 118 | + { |
| 119 | + throw "Unable to get host UUID.`n" |
| 120 | + } |
| 121 | + |
| 122 | + Write-Host "fetched UUID successfully.`n" |
| 123 | + $content = (ConvertFrom-Json -InputObject $response) |
| 124 | + return $content |
| 125 | +} |
| 126 | + |
| 127 | +######################### |
| 128 | +# Get a specific Processes |
| 129 | +######################### |
| 130 | +Function GetProcess() |
| 131 | +{ |
| 132 | + $uri = $baseUri + $adminHostsUri + $uuid + "/processes?filter=processName eq 'bpcd'" |
| 133 | + |
| 134 | + Write-Host "`nSending a GET request to read a specific processes on client $client...`n" |
| 135 | + $response = Invoke-WebRequest ` |
| 136 | + -Uri $uri ` |
| 137 | + -Method GET ` |
| 138 | + -ContentType $contentType ` |
| 139 | + -Headers $headers |
| 140 | + |
| 141 | + if ($response.StatusCode -ne 200) |
| 142 | + { |
| 143 | + throw "Unable to read processes on client $client.`n" |
| 144 | + } |
| 145 | + $response = (ConvertFrom-Json -InputObject $response) |
| 146 | + $response.data.attributes | Format-Table -autosize |
| 147 | +} |
| 148 | + |
| 149 | +################# |
| 150 | +# Get Processes |
| 151 | +################# |
| 152 | +Function GetProcesses() |
| 153 | +{ |
| 154 | + $uri = $baseUri + $adminHostsUri + $uuid + "/processes" |
| 155 | + |
| 156 | + Write-Host "`nSending a GET request to read processes on client $client...`n" |
| 157 | + $response = Invoke-WebRequest ` |
| 158 | + -Uri $uri ` |
| 159 | + -Method GET ` |
| 160 | + -ContentType $contentType ` |
| 161 | + -Headers $headers |
| 162 | + |
| 163 | + if ($response.StatusCode -ne 200) |
| 164 | + { |
| 165 | + throw "Unable to read processes on client $client.`n" |
| 166 | + } |
| 167 | + $response = (ConvertFrom-Json -InputObject $response) |
| 168 | + $response.data.attributes | Format-Table -autosize |
| 169 | +} |
| 170 | + |
| 171 | + |
| 172 | +Setup |
| 173 | +$loginResponse = Login |
| 174 | +$headers = @{"Authorization" = $loginResponse.token} |
| 175 | +$uuidResponse = getUUID |
| 176 | +$uuid = $uuidResponse.hosts[0].uuid |
| 177 | +GetProcess |
| 178 | +GetProcesses |
0 commit comments