Skip to content

Commit 67cbd44

Browse files
author
Nick
committed
add powershell modules
adding PS modules
1 parent d8be6f2 commit 67cbd44

File tree

10 files changed

+334
-0
lines changed

10 files changed

+334
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<#
2+
.SYNOPSIS
3+
Get NetBackup Jobs from master server.
4+
.DESCRIPTION
5+
Gets the list of jobs from the master server.
6+
See https://Masterserver:1556/api-docs/index.html?
7+
.PARAMETER assetType
8+
The ability to filter vms or vmgroups
9+
.EXAMPLE
10+
Get-NbAssets -assetType vm or vmGroup
11+
.OUTPUTS
12+
Array
13+
.NOTES
14+
FunctionName : Get-NbAssets
15+
Created by : Nick Britton
16+
#>
17+
18+
function Get-NbAssets()
19+
{
20+
[CmdletBinding()]
21+
param (
22+
[Parameter(Mandatory=$true,
23+
HelpMessage="AssetType needs to specifiy vm or vmGroup")]
24+
[string]$assetType
25+
26+
)
27+
$assetType = "vm"
28+
# FUNCTION START
29+
$results = @()
30+
$uri = $basepath + $assetServiceUri
31+
32+
$default_sort = "commonAssetAttributes.displayName"
33+
34+
if($assetType -eq "vm"){
35+
$assetTypeFilter = "assetType eq 'vm'"
36+
}
37+
elseif($assetType -eq "vmGroup"){
38+
$assetTypeFilter = "assetType eq 'vmGroup'"
39+
}
40+
$offset = 0
41+
$next = $true
42+
43+
while ($next){
44+
45+
$assetServiceUri = "/asset-service/workloads/vmware/assets"
46+
$uri = $basepath + $assetServiceUri
47+
$query_params = @{
48+
"filter" = "$assetTypeFilter"
49+
"sort" = "commonAssetAttributes.displayName"
50+
"page[offset]" = "$offset"
51+
"page[limit]" = "100"
52+
}
53+
54+
$response = Invoke-WebRequest -Uri $uri -Method GET -Body $query_params -ContentType $nbcontent_type -Headers $nbheaders
55+
56+
if ($response.StatusCode -ne 200) { throw "Unable to get VMware assets.`n" }
57+
58+
$api_response = (ConvertFrom-Json -InputObject $response)
59+
60+
$results += $api_response
61+
#write-host "offset is $offset"
62+
$offset = $offset + $api_response.meta.pagination.limit
63+
if($api_response.meta.pagination.hasNext -eq $false){ $next = $false }
64+
65+
}
66+
$assetarray = $results.data
67+
Return $assetarray
68+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<#
2+
.SYNOPSIS
3+
Get client or host details from master server
4+
.DESCRIPTION
5+
Gets the host details for each client on the master and will return the details in an array.
6+
See https://Masterserver:1556/api-docs/index.html?urls.primaryName=config#/Hosts/get_config_hosts for details on the api used.
7+
.EXAMPLE
8+
Get-NbClientInfo
9+
.OUTPUTS
10+
Array
11+
.NOTES
12+
FunctionName : Get-NbClientInfo
13+
Created by : Nick Britton
14+
#>
15+
16+
function Get-NbClientInfo()
17+
{
18+
$uri = $basepath + "/config/hosts"
19+
$response = Invoke-WebRequest -Uri $uri -Method GET -ContentType $nbcontent_type -Headers $nbheaders
20+
if ($response.StatusCode -ne 200){ throw "Unable to get the client details"}
21+
$content = (ConvertFrom-Json -InputObject $response)
22+
$nbClientInfo = $content.hosts
23+
Return $nbClientInfo
24+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<#
2+
.SYNOPSIS
3+
Get client or host details from master server
4+
.DESCRIPTION
5+
Gets the host details for each client on the master and will return the details in an array.
6+
See https://Masterserver:1556/api-docs/index.html?urls.primaryName=config#/Hosts/get_config_hosts for details on the api used.
7+
.EXAMPLE
8+
Get-NbClientInfo
9+
.OUTPUTS
10+
Array
11+
.NOTES
12+
FunctionName : Get-NbClientInfo
13+
Created by : Nick Britton
14+
#>
15+
16+
function Get-NbClientInfo()
17+
{
18+
$uri = $basepath + "/config/hosts"
19+
$response = Invoke-WebRequest -Uri $uri -Method GET -ContentType $nbcontent_type -Headers $nbheaders
20+
if ($response.StatusCode -ne 200){ throw "Unable to get the client details"}
21+
$content = (ConvertFrom-Json -InputObject $response)
22+
$nbClientInfo = $content.hosts
23+
Return $nbClientInfo
24+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<#
2+
.SYNOPSIS
3+
Get NetBackup Job log for specified jobid from master server.
4+
.DESCRIPTION
5+
Gets the job log from the master server.
6+
See https://Masterserver:1556/api-docs/index.html?urls.primaryName=admin#/Jobs/get_admin_jobs__jobId__try_logs for details on the api used.
7+
The JobId can be found in the nbjobs function.
8+
.PARAMETER jobid
9+
JobId is required for the api call.
10+
.EXAMPLE
11+
Get-NbJobLog -jobid ######
12+
.OUTPUTS
13+
Array
14+
.NOTES
15+
FunctionName : Get-NbJobLog
16+
Created by : Nick Britton
17+
#>
18+
19+
function Get-NbJobLog()
20+
{
21+
[CmdletBinding()]
22+
param (
23+
[Parameter(Mandatory=$true,
24+
HelpMessage="Job id is required for api call")]
25+
[string]$jobid
26+
27+
)
28+
29+
$uri = $basepath + "/admin/jobs/$jobid/try-logs"
30+
$response = Invoke-WebRequest -Uri $uri -Method GET -ContentType $nbcontent_type -Headers $nbheaders
31+
32+
if ($response.StatusCode -ne 200){throw "Unable to get the Netbackup jobs!"}
33+
$content = (ConvertFrom-Json -InputObject $response)
34+
Return $content
35+
36+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<#
2+
.SYNOPSIS
3+
Get NetBackup Jobs from master server.
4+
.DESCRIPTION
5+
Gets the list of jobs from the master server.
6+
See https://Masterserver:1556/api-docs/index.html?urls.primaryName=admin#/Jobs/get_admin_jobs for details on the api used.
7+
.PARAMETER filter
8+
The odata filter to apply. See link above for details. Ex. "policyType eq '$policyType' and jobId ne parentJobId"
9+
.PARAMETER pagelimit
10+
Limit the number of records per page
11+
.EXAMPLE
12+
Get-NbJobs -filter policyType eq policyType and jobId ne parentJobId -pagelimit 99
13+
.OUTPUTS
14+
Array
15+
.NOTES
16+
FunctionName : Get-NbJobs
17+
Created by : Nick Britton
18+
#>
19+
20+
function Get-NbJobs()
21+
{
22+
[CmdletBinding()]
23+
param (
24+
[Parameter(Mandatory=$false,
25+
HelpMessage="Filter in odata format. Ex. policyType eq policyType and jobId ne parentJobId")]
26+
[string]$filter,
27+
[Parameter(Mandatory=$false,
28+
HelpMessage="Page limit 0-99")]
29+
[string]$pagelimit
30+
)
31+
$uri = $basepath + "/admin/jobs"
32+
33+
$query_params = @{
34+
"filter" = "$filter" # This adds a filter to only show the restore jobs
35+
"page[limit]" = "$pagelimit"
36+
37+
}
38+
39+
$response = Invoke-WebRequest -Uri $uri -Method GET -Body $query_params -ContentType $nbcontent_type -Headers $nbheaders
40+
41+
if ($response.StatusCode -ne 200) { throw "Unable to get the Netbackup jobs!"}
42+
43+
$content = (ConvertFrom-Json -InputObject $response)
44+
45+
46+
$jobarray = $content.data
47+
Return $jobarray
48+
49+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<#
2+
.SYNOPSIS
3+
Login to NetBackup Api.
4+
.DESCRIPTION
5+
Logs into the netbackup instance, via the api functions.
6+
.EXAMPLE
7+
Get-NbLogin
8+
.OUTPUTS
9+
String
10+
Sets nbheaders var in parent script
11+
.NOTES
12+
FunctionName : Get-Login
13+
Created by : Nick Britton
14+
#>
15+
16+
function Get-NbLogin()
17+
{
18+
$uri = $basepath + "/login"
19+
20+
$body = @{
21+
userName=$username
22+
password=$password
23+
}
24+
$response = Invoke-WebRequest -Uri $uri -Method POST -Body (ConvertTo-Json -InputObject $body) -ContentType $nbcontent_type
25+
26+
if ($response.StatusCode -ne 201) { throw "Unable to connect to the Netbackup master server!" }
27+
28+
$content = (ConvertFrom-Json -InputObject $response)
29+
$logintoken = $content.token
30+
$script:nbheaders = @{ "Authorization" = $logintoken}
31+
return $logintoken
32+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<#
2+
.SYNOPSIS
3+
Get client or host details from master server
4+
.DESCRIPTION
5+
Gets the host details for each client on the master and will return the details in an array.
6+
See https://Masterserver:1556/api-docs/index.html?urls.primaryName=config#/Hosts/get_config_hosts for details on the api used.
7+
.EXAMPLE
8+
Get-NbClientInfo
9+
.OUTPUTS
10+
Array
11+
.NOTES
12+
FunctionName : Get-NbDiscoveryStatus
13+
Created by : Nick Britton
14+
#>
15+
16+
function Get-NbVMDiscoveryStatus()
17+
{
18+
$uri = $basepath + "/admin/discovery/workloads/vmware/status"
19+
$query_params = @{
20+
"page[limit]" = "99"
21+
22+
}
23+
$response = Invoke-WebRequest -Uri $uri -Method GET -Body $query_params -ContentType $nbcontent_type -Headers $nbheaders
24+
if ($response.StatusCode -ne 200){ throw "Unable to get the client details"}
25+
$content = (ConvertFrom-Json -InputObject $response)
26+
$data = $content.data
27+
28+
$properties =
29+
@{Label = "vcenter"; Expression = { $_.attributes.ServerName }},
30+
@{Label = "workload"; Expression = { $_.attributes.WorkloadType }},
31+
@{Label = "Status"; Expression = { $_.attributes.discoveryStatus }},
32+
@{Label = "StartTime"; Expression = { $_.attributes.discoveryStartTime }},
33+
@{Label = "EndTime"; Expression = { $_.attributes.discoveryFinishTime }},
34+
@{Label = "ModifyTime"; Expression = { $_.attributes.lastModifiedDateTime }},
35+
@{Label = "DiscoveryHost"; Expression = { $_.attributes.additionalAttributes.discoveryHost }},
36+
@{Label = "DiscoveryFreq"; Expression = { $_.attributes.additionalAttributes.discoveryFrequencySeconds }},
37+
@{Label = "DiscoveryDisabled"; Expression = { $_.attributes.additionalAttributes.isDiscoveryDisabled }}
38+
39+
$data = $data|Select-Object -Property $properties
40+
Return $data
41+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
To use modules we include this in all of our scripts:
2+
This will search for all the modules and make them available. We update the drive letter depending on use case.
3+
4+
$PS_Modules = Get-ChildItem c:\ -ErrorAction SilentlyContinue -filter "GTSESS*-modules" -Recurse
5+
foreach ($module in $PS_Modules)
6+
{
7+
$path = $module.FullName
8+
get-childitem $path\*.ps1 -Recurse | % {. $_.FullName}
9+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<#
2+
.SYNOPSIS
3+
Setup NetBackup Env.
4+
.DESCRIPTION
5+
Sets up the Netbackup api enviornment for the parent script. Will set nbcontent_type that is used in other functions for other calls
6+
.EXAMPLE
7+
Set-NbInitialSetup
8+
.OUTPUTS
9+
Sets nbcontent_type Variable
10+
.NOTES
11+
FunctionName : Set-NbInitialSetup
12+
Created by : Nick Britton
13+
#>
14+
15+
#####################################################################
16+
# Initial Setup
17+
# Note: This allows self-signed certificates and enables TLS v1.2
18+
#####################################################################
19+
20+
function Set-NbInitialSetup()
21+
{
22+
# Allow self-signed certificates
23+
if ([System.Net.ServicePointManager]::CertificatePolicy -notlike 'TrustAllCertsPolicy')
24+
{
25+
Add-Type -TypeDefinition @"
26+
using System.Net;
27+
using System.Security.Cryptography.X509Certificates;
28+
public class TrustAllCertsPolicy : ICertificatePolicy {
29+
public bool CheckValidationResult(
30+
ServicePoint srvPoint, X509Certificate certificate,
31+
WebRequest request, int certificateProblem) {
32+
return true;
33+
}
34+
}
35+
"@
36+
[System.Net.ServicePointManager]::CertificatePolicy = New-Object -TypeName TrustAllCertsPolicy
37+
38+
# Force TLS v1.2
39+
try {
40+
if ([Net.ServicePointManager]::SecurityProtocol -notcontains 'Tls12') {
41+
[Net.ServicePointManager]::SecurityProtocol += [Net.SecurityProtocolType]::Tls12
42+
}
43+
}
44+
catch {
45+
Write-Host $_.Exception.InnerException.Message
46+
}
47+
}
48+
# Global Variables
49+
$script:nbcontent_type = "application/vnd.netbackup+json;version=4.0"
50+
51+
}

snippets/powershell/modules/gtsess-nbu-modules.psm1

Whitespace-only changes.

0 commit comments

Comments
 (0)