forked from VeritasOS/netbackup-api-code-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding PS modules
- Loading branch information
Nick
committed
Feb 19, 2021
1 parent
d8be6f2
commit 67cbd44
Showing
10 changed files
with
334 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<# | ||
.SYNOPSIS | ||
Get NetBackup Jobs from master server. | ||
.DESCRIPTION | ||
Gets the list of jobs from the master server. | ||
See https://Masterserver:1556/api-docs/index.html? | ||
.PARAMETER assetType | ||
The ability to filter vms or vmgroups | ||
.EXAMPLE | ||
Get-NbAssets -assetType vm or vmGroup | ||
.OUTPUTS | ||
Array | ||
.NOTES | ||
FunctionName : Get-NbAssets | ||
Created by : Nick Britton | ||
#> | ||
|
||
function Get-NbAssets() | ||
{ | ||
[CmdletBinding()] | ||
param ( | ||
[Parameter(Mandatory=$true, | ||
HelpMessage="AssetType needs to specifiy vm or vmGroup")] | ||
[string]$assetType | ||
|
||
) | ||
$assetType = "vm" | ||
# FUNCTION START | ||
$results = @() | ||
$uri = $basepath + $assetServiceUri | ||
|
||
$default_sort = "commonAssetAttributes.displayName" | ||
|
||
if($assetType -eq "vm"){ | ||
$assetTypeFilter = "assetType eq 'vm'" | ||
} | ||
elseif($assetType -eq "vmGroup"){ | ||
$assetTypeFilter = "assetType eq 'vmGroup'" | ||
} | ||
$offset = 0 | ||
$next = $true | ||
|
||
while ($next){ | ||
|
||
$assetServiceUri = "/asset-service/workloads/vmware/assets" | ||
$uri = $basepath + $assetServiceUri | ||
$query_params = @{ | ||
"filter" = "$assetTypeFilter" | ||
"sort" = "commonAssetAttributes.displayName" | ||
"page[offset]" = "$offset" | ||
"page[limit]" = "100" | ||
} | ||
|
||
$response = Invoke-WebRequest -Uri $uri -Method GET -Body $query_params -ContentType $nbcontent_type -Headers $nbheaders | ||
|
||
if ($response.StatusCode -ne 200) { throw "Unable to get VMware assets.`n" } | ||
|
||
$api_response = (ConvertFrom-Json -InputObject $response) | ||
|
||
$results += $api_response | ||
#write-host "offset is $offset" | ||
$offset = $offset + $api_response.meta.pagination.limit | ||
if($api_response.meta.pagination.hasNext -eq $false){ $next = $false } | ||
|
||
} | ||
$assetarray = $results.data | ||
Return $assetarray | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<# | ||
.SYNOPSIS | ||
Get client or host details from master server | ||
.DESCRIPTION | ||
Gets the host details for each client on the master and will return the details in an array. | ||
See https://Masterserver:1556/api-docs/index.html?urls.primaryName=config#/Hosts/get_config_hosts for details on the api used. | ||
.EXAMPLE | ||
Get-NbClientInfo | ||
.OUTPUTS | ||
Array | ||
.NOTES | ||
FunctionName : Get-NbClientInfo | ||
Created by : Nick Britton | ||
#> | ||
|
||
function Get-NbClientInfo() | ||
{ | ||
$uri = $basepath + "/config/hosts" | ||
$response = Invoke-WebRequest -Uri $uri -Method GET -ContentType $nbcontent_type -Headers $nbheaders | ||
if ($response.StatusCode -ne 200){ throw "Unable to get the client details"} | ||
$content = (ConvertFrom-Json -InputObject $response) | ||
$nbClientInfo = $content.hosts | ||
Return $nbClientInfo | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<# | ||
.SYNOPSIS | ||
Get client or host details from master server | ||
.DESCRIPTION | ||
Gets the host details for each client on the master and will return the details in an array. | ||
See https://Masterserver:1556/api-docs/index.html?urls.primaryName=config#/Hosts/get_config_hosts for details on the api used. | ||
.EXAMPLE | ||
Get-NbClientInfo | ||
.OUTPUTS | ||
Array | ||
.NOTES | ||
FunctionName : Get-NbClientInfo | ||
Created by : Nick Britton | ||
#> | ||
|
||
function Get-NbClientInfo() | ||
{ | ||
$uri = $basepath + "/config/hosts" | ||
$response = Invoke-WebRequest -Uri $uri -Method GET -ContentType $nbcontent_type -Headers $nbheaders | ||
if ($response.StatusCode -ne 200){ throw "Unable to get the client details"} | ||
$content = (ConvertFrom-Json -InputObject $response) | ||
$nbClientInfo = $content.hosts | ||
Return $nbClientInfo | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<# | ||
.SYNOPSIS | ||
Get NetBackup Job log for specified jobid from master server. | ||
.DESCRIPTION | ||
Gets the job log from the master server. | ||
See https://Masterserver:1556/api-docs/index.html?urls.primaryName=admin#/Jobs/get_admin_jobs__jobId__try_logs for details on the api used. | ||
The JobId can be found in the nbjobs function. | ||
.PARAMETER jobid | ||
JobId is required for the api call. | ||
.EXAMPLE | ||
Get-NbJobLog -jobid ###### | ||
.OUTPUTS | ||
Array | ||
.NOTES | ||
FunctionName : Get-NbJobLog | ||
Created by : Nick Britton | ||
#> | ||
|
||
function Get-NbJobLog() | ||
{ | ||
[CmdletBinding()] | ||
param ( | ||
[Parameter(Mandatory=$true, | ||
HelpMessage="Job id is required for api call")] | ||
[string]$jobid | ||
|
||
) | ||
|
||
$uri = $basepath + "/admin/jobs/$jobid/try-logs" | ||
$response = Invoke-WebRequest -Uri $uri -Method GET -ContentType $nbcontent_type -Headers $nbheaders | ||
|
||
if ($response.StatusCode -ne 200){throw "Unable to get the Netbackup jobs!"} | ||
$content = (ConvertFrom-Json -InputObject $response) | ||
Return $content | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<# | ||
.SYNOPSIS | ||
Get NetBackup Jobs from master server. | ||
.DESCRIPTION | ||
Gets the list of jobs from the master server. | ||
See https://Masterserver:1556/api-docs/index.html?urls.primaryName=admin#/Jobs/get_admin_jobs for details on the api used. | ||
.PARAMETER filter | ||
The odata filter to apply. See link above for details. Ex. "policyType eq '$policyType' and jobId ne parentJobId" | ||
.PARAMETER pagelimit | ||
Limit the number of records per page | ||
.EXAMPLE | ||
Get-NbJobs -filter policyType eq policyType and jobId ne parentJobId -pagelimit 99 | ||
.OUTPUTS | ||
Array | ||
.NOTES | ||
FunctionName : Get-NbJobs | ||
Created by : Nick Britton | ||
#> | ||
|
||
function Get-NbJobs() | ||
{ | ||
[CmdletBinding()] | ||
param ( | ||
[Parameter(Mandatory=$false, | ||
HelpMessage="Filter in odata format. Ex. policyType eq policyType and jobId ne parentJobId")] | ||
[string]$filter, | ||
[Parameter(Mandatory=$false, | ||
HelpMessage="Page limit 0-99")] | ||
[string]$pagelimit | ||
) | ||
$uri = $basepath + "/admin/jobs" | ||
|
||
$query_params = @{ | ||
"filter" = "$filter" # This adds a filter to only show the restore jobs | ||
"page[limit]" = "$pagelimit" | ||
|
||
} | ||
|
||
$response = Invoke-WebRequest -Uri $uri -Method GET -Body $query_params -ContentType $nbcontent_type -Headers $nbheaders | ||
|
||
if ($response.StatusCode -ne 200) { throw "Unable to get the Netbackup jobs!"} | ||
|
||
$content = (ConvertFrom-Json -InputObject $response) | ||
|
||
|
||
$jobarray = $content.data | ||
Return $jobarray | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<# | ||
.SYNOPSIS | ||
Login to NetBackup Api. | ||
.DESCRIPTION | ||
Logs into the netbackup instance, via the api functions. | ||
.EXAMPLE | ||
Get-NbLogin | ||
.OUTPUTS | ||
String | ||
Sets nbheaders var in parent script | ||
.NOTES | ||
FunctionName : Get-Login | ||
Created by : Nick Britton | ||
#> | ||
|
||
function Get-NbLogin() | ||
{ | ||
$uri = $basepath + "/login" | ||
|
||
$body = @{ | ||
userName=$username | ||
password=$password | ||
} | ||
$response = Invoke-WebRequest -Uri $uri -Method POST -Body (ConvertTo-Json -InputObject $body) -ContentType $nbcontent_type | ||
|
||
if ($response.StatusCode -ne 201) { throw "Unable to connect to the Netbackup master server!" } | ||
|
||
$content = (ConvertFrom-Json -InputObject $response) | ||
$logintoken = $content.token | ||
$script:nbheaders = @{ "Authorization" = $logintoken} | ||
return $logintoken | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<# | ||
.SYNOPSIS | ||
Get client or host details from master server | ||
.DESCRIPTION | ||
Gets the host details for each client on the master and will return the details in an array. | ||
See https://Masterserver:1556/api-docs/index.html?urls.primaryName=config#/Hosts/get_config_hosts for details on the api used. | ||
.EXAMPLE | ||
Get-NbClientInfo | ||
.OUTPUTS | ||
Array | ||
.NOTES | ||
FunctionName : Get-NbDiscoveryStatus | ||
Created by : Nick Britton | ||
#> | ||
|
||
function Get-NbVMDiscoveryStatus() | ||
{ | ||
$uri = $basepath + "/admin/discovery/workloads/vmware/status" | ||
$query_params = @{ | ||
"page[limit]" = "99" | ||
|
||
} | ||
$response = Invoke-WebRequest -Uri $uri -Method GET -Body $query_params -ContentType $nbcontent_type -Headers $nbheaders | ||
if ($response.StatusCode -ne 200){ throw "Unable to get the client details"} | ||
$content = (ConvertFrom-Json -InputObject $response) | ||
$data = $content.data | ||
|
||
$properties = | ||
@{Label = "vcenter"; Expression = { $_.attributes.ServerName }}, | ||
@{Label = "workload"; Expression = { $_.attributes.WorkloadType }}, | ||
@{Label = "Status"; Expression = { $_.attributes.discoveryStatus }}, | ||
@{Label = "StartTime"; Expression = { $_.attributes.discoveryStartTime }}, | ||
@{Label = "EndTime"; Expression = { $_.attributes.discoveryFinishTime }}, | ||
@{Label = "ModifyTime"; Expression = { $_.attributes.lastModifiedDateTime }}, | ||
@{Label = "DiscoveryHost"; Expression = { $_.attributes.additionalAttributes.discoveryHost }}, | ||
@{Label = "DiscoveryFreq"; Expression = { $_.attributes.additionalAttributes.discoveryFrequencySeconds }}, | ||
@{Label = "DiscoveryDisabled"; Expression = { $_.attributes.additionalAttributes.isDiscoveryDisabled }} | ||
|
||
$data = $data|Select-Object -Property $properties | ||
Return $data | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
To use modules we include this in all of our scripts: | ||
This will search for all the modules and make them available. We update the drive letter depending on use case. | ||
|
||
$PS_Modules = Get-ChildItem c:\ -ErrorAction SilentlyContinue -filter "GTSESS*-modules" -Recurse | ||
foreach ($module in $PS_Modules) | ||
{ | ||
$path = $module.FullName | ||
get-childitem $path\*.ps1 -Recurse | % {. $_.FullName} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<# | ||
.SYNOPSIS | ||
Setup NetBackup Env. | ||
.DESCRIPTION | ||
Sets up the Netbackup api enviornment for the parent script. Will set nbcontent_type that is used in other functions for other calls | ||
.EXAMPLE | ||
Set-NbInitialSetup | ||
.OUTPUTS | ||
Sets nbcontent_type Variable | ||
.NOTES | ||
FunctionName : Set-NbInitialSetup | ||
Created by : Nick Britton | ||
#> | ||
|
||
##################################################################### | ||
# Initial Setup | ||
# Note: This allows self-signed certificates and enables TLS v1.2 | ||
##################################################################### | ||
|
||
function Set-NbInitialSetup() | ||
{ | ||
# Allow self-signed certificates | ||
if ([System.Net.ServicePointManager]::CertificatePolicy -notlike 'TrustAllCertsPolicy') | ||
{ | ||
Add-Type -TypeDefinition @" | ||
using System.Net; | ||
using System.Security.Cryptography.X509Certificates; | ||
public class TrustAllCertsPolicy : ICertificatePolicy { | ||
public bool CheckValidationResult( | ||
ServicePoint srvPoint, X509Certificate certificate, | ||
WebRequest request, int certificateProblem) { | ||
return true; | ||
} | ||
} | ||
"@ | ||
[System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName TrustAllCertsPolicy | ||
|
||
# Force TLS v1.2 | ||
try { | ||
if ([Net.ServicePointManager]::SecurityProtocol -notcontains 'Tls12') { | ||
[Net.ServicePointManager]::SecurityProtocol += [Net.SecurityProtocolType]::Tls12 | ||
} | ||
} | ||
catch { | ||
Write-Host $_.Exception.InnerException.Message | ||
} | ||
} | ||
# Global Variables | ||
$script:nbcontent_type = "application/vnd.netbackup+json;version=4.0" | ||
|
||
} |
Empty file.