|
| 1 | +<# |
| 2 | +.SYNOPSIS |
| 3 | +This sample script demonstrates the use of NetBackup APIs for retieving VMware plugin information. |
| 4 | +.DESCRIPTION |
| 5 | +This script can be run using NetBackup 8.3 and higher. |
| 6 | +It retrieves VMware asset data |
| 7 | +.EXAMPLE |
| 8 | +./get_VMware_Asset_Data.ps1 -MasterServer <masterServer> -username <username> -password <password> [-domainName <domainName> -domainType <domainType>][-assetType <vm|vmGroup>][-assetsFilter <filter>] |
| 9 | +#> |
| 10 | + |
| 11 | +#Requires -Version 4.0 |
| 12 | + |
| 13 | +Param ( |
| 14 | + [string]$MasterServer = $(Throw "Please specify the name of the NetBackup Master Server using the -MasterServer 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 | + [string]$assetType, |
| 20 | + [string]$assetsFilter |
| 21 | +) |
| 22 | + |
| 23 | +#################### |
| 24 | +# Global Variables |
| 25 | +#################### |
| 26 | + |
| 27 | +$port = 1556 |
| 28 | +$baseUri = "https://" + $MasterServer + ":" + $port + "/netbackup/" |
| 29 | +$assetServiceUri = "asset-service/workloads/vmware/assets?"; |
| 30 | +$contentType = "application/vnd.netbackup+json;version=4.0" |
| 31 | + |
| 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 | +# GET Assets |
| 105 | +################################################# |
| 106 | +Function getAssets() |
| 107 | +{ |
| 108 | + $uri = $baseUri + $assetServiceUri |
| 109 | + |
| 110 | + $default_sort = "sort=commonAssetAttributes.displayName" |
| 111 | + |
| 112 | + if($assetType -eq "vm"){ |
| 113 | + $assetTypeFilter = "filter=assetType eq 'vm'" |
| 114 | + } |
| 115 | + elseif($assetType -eq "vmGroup"){ |
| 116 | + $assetTypeFilter = "filter=assetType eq 'vmGroup'" |
| 117 | + } |
| 118 | + |
| 119 | + if(![string]::IsNullOrEmpty($assetsFilter)){ |
| 120 | + if([string]::IsNullOrEmpty($assetTypeFilter)){ |
| 121 | + $assetTypeFilter = "filter=" + $assetsFilter |
| 122 | + } |
| 123 | + else{ |
| 124 | + $assetTypeFilter = $assetTypeFilter + " and " + $assetsFilter |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + $offset = 0 |
| 129 | + $next = $true |
| 130 | + |
| 131 | + while ($next){ |
| 132 | + $uri = $baseUri + $assetServiceUri + $assetTypeFilter + "&" + $default_sort + "&page[offset]=$offset" |
| 133 | + |
| 134 | + Write-Host "`nSending a GET request to list all Assets...`n" |
| 135 | + |
| 136 | + $response = Invoke-WebRequest ` |
| 137 | + -Uri $uri ` |
| 138 | + -Method GET ` |
| 139 | + -ContentType $contentType ` |
| 140 | + -Headers $headers |
| 141 | + |
| 142 | + if ($response.StatusCode -ne 200) |
| 143 | + { |
| 144 | + throw "Unable to get VMware assets.`n" |
| 145 | + } |
| 146 | + |
| 147 | + $api_response = (ConvertFrom-Json -InputObject $response) |
| 148 | + |
| 149 | + if($assetType -eq "vmGroup"){ |
| 150 | + $vmGroup_data = |
| 151 | + @{Label = "DisplayName"; Expression = { $_.attributes.commonAssetAttributes.displayName}}, |
| 152 | + @{Label = "filterConstraint"; Expression = { $_.attributes.filterConstraint }}, |
| 153 | + @{Label = "oDataQueryFilter"; Expression = { $_.attributes.oDataQueryFilter }}, |
| 154 | + @{Label = "Asset Protection Plans"; Expression = { $_.attributes.commonAssetAttributes.activeProtection.protectionDetailsList }} |
| 155 | + |
| 156 | + $api_response.data | Format-Table -AutoSize -Property $vmGroup_data |
| 157 | + } |
| 158 | + else{ |
| 159 | + $vm_data = |
| 160 | + @{Label = "DisplayName"; Expression = { $_.attributes.commonAssetAttributes.displayName}}, |
| 161 | + @{Label = "InstanceUUID"; Expression = { $_.attributes.instanceUuid }}, |
| 162 | + @{Label = "vCenter"; Expression = { $_.attributes.vCenter }}, |
| 163 | + @{Label = "Asset Protection Plans"; Expression = { $_.attributes.commonAssetAttributes.activeProtection.protectionDetailsList }} |
| 164 | + |
| 165 | + $api_response.data | Format-Table -AutoSize -Property $vm_data |
| 166 | + |
| 167 | + } |
| 168 | + |
| 169 | + $offset = $offset + $api_response.meta.pagination.limit |
| 170 | + |
| 171 | + if($api_response.meta.pagination.hasNext -eq $false){ |
| 172 | + $next = $false |
| 173 | + } |
| 174 | + |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +Setup |
| 179 | +$loginResponse = Login |
| 180 | +$headers = @{"Authorization" = $loginResponse.token} |
| 181 | +getAssets |
0 commit comments