Forum Discussion

JimmyB2's avatar
JimmyB2
Level 4
4 years ago

NetBackup PowerShell Cmdlets how to use it

Hi all,

on the official Veritas page on the GitHub there is a section for NetBackup PowerShell Cmdlets (https://github.com/VeritasOS/nb-powershell). Since I am not so strong in Powershell scripting I would like to ask you how to compile these scripts.

I was able to list Cmdlets - see below. However, I do not know how to make these cmdlets working (e.g. Get-NBAlert). Should I download e.g. GetAlert.cs and then somehow compile it?

Any help much appreciated!

PS > Get-Command -Module NBCmdlets

CommandType Name ModuleName
----------- ---- ----------
Cmdlet Add-NBRecoverVMWareInstantAccessMount NBCmdlets
Cmdlet Get-AppDetails NBCmdlets
Cmdlet Get-NBAccessPermissions NBCmdlets
Cmdlet Get-NBAccessRoles NBCmdlets
Cmdlet Get-NBAccessRules NBCmdlets
Cmdlet Get-NBAlert NBCmdlet
.....

 

12 Replies

    • JimmyB2's avatar
      JimmyB2
      Level 4

      Hi Krutons !

      I will try to dig into the powershell code....however, is it possible to leverage API calls when I have Netbackup Appliance (Netbackup 8.2). I am on the Windows machine, so I do not know how to make curl command from Windows to use API calls.

      • Krutons's avatar
        Krutons
        Moderator

        Yes JimmyB2, you can make API calls from almost anywhere.

        Let's start from the beginning. Have you already created an API Key?

  • Hi Krutons , yes, I can access this page. Moreover, I have to specify port number within the address -  https://<master server>:1556/api-docs/index.html

    I would like to carry tomorrow. So far, big thanks for your guidance!

    • Krutons's avatar
      Krutons
      Moderator

      JimmyB2, no problem.

      Here is a powershell script that you could run to get jobs. You could replace any info that would be specific to your environment. Once you've looked over that, let me know if you have any questions and I'll be happy to help/explain. I personally don't use username/password, I strictly auth with API Keys.

       

      <#
      .SYNOPSIS
      This sample script demonstrates the use of NetBackup REST API for listing the jobs.
      .DESCRIPTION
      This script will get a list of netbackup jobs and print the details of the last 10 jobs in a tabular format.
      .EXAMPLE
      ./Get-NB-Jobs.ps1 -nbmaster "nb-master.example.com" -apikey "key"
      #>
      
      param (
          [string]$nbmaster = $(throw "Please specify the name of NetBackup master server using -nbmaster parameter."),    
          [string]$apikey = $(throw "Please specify the API Key using the -apikey parameter.")
      )
      
      #####################################################################
      # Initial Setup
      # Note: This allows self-signed certificates and enables TLS v1.2
      #####################################################################
      
      function InitialSetup()
      {
        # 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 -notmatch 'Tls12') {
                  [Net.ServicePointManager]::SecurityProtocol = ([Net.ServicePointManager]::SecurityProtocol -bor [Net.SecurityProtocolType]::Tls12)
              }
          }
          catch {
              Write-Host $_.Exception.InnerException.Message
          }
        }
      }
      
      InitialSetup
      
      #####################################################################
      # Global Variables
      #####################################################################
      
      $port = 1556
      $basepath = "https://" + $nbmaster + ":" + $port + "/netbackup"
      $content_type = "application/vnd.netbackup+json;version=1.0"
      
      #####################################################################
      # Get NetBackup Jobs
      #####################################################################
      
      $uri = $basepath + "/admin/jobs"
      
      $headers = @{
        "Authorization" = $apikey
      }
      
      $query_params = @{
      #  "page[limit]" = 100                   # This changes the default page size to 100
      #  "filter" = "jobType eq 'RESTORE'"     # This adds a filter to only show the restore jobs
      }
      
      $response = Invoke-WebRequest                 `
                      -Uri $uri                     `
                      -Method GET                   `
                      -Body $query_params           `
                      -ContentType $content_type    `
                      -Headers $headers
      
      if ($response.StatusCode -ne 200)
      {
          throw "Unable to get the Netbackup jobs!"
      }
      
      $content = (ConvertFrom-Json -InputObject $response)
      
      # This prints all the available attributes
      #$content.data.attributes | Format-Table -AutoSize
      
      $properties = 
          @{Label = "Job ID"; Expression = { $_.attributes.jobId }},
          @{Label = "Type";   Expression = { $_.attributes.jobType }},
          @{Label = "State";  Expression = { $_.attributes.state }},
          @{Label = "Status"; Expression = { $_.attributes.status }}
      
      $content.data | Format-Table -AutoSize -Property $properties

       

       

       

      • JimmyB2's avatar
        JimmyB2
        Level 4

        Hi Krutons ,

        thanks for the scripts, I will try to run them once I rearrange the formatting.

        Additionally, I have a question, on the page ''https://<master server>:1556/api-docs/index.html" I am able to run the API call and I can get the answer.

        However, if I run the API call in the different web brower tab (https://server_name:1556/netbackup/admin/jobs), I see response "Access denied". So, is it possible to add the API key within the API call url?

  • Hi Krutons ,

    hm, this is the thing, why I need to run the API call within the Powershell script in order to remotely run the API call...

    Unfortunately, the jobs.ps1 script returns no value/no output :(

    • Krutons's avatar
      Krutons
      Moderator

      That's strange, I can copy that code and it works fine. Does it throw an error? If you want you can download the VOX_Test.txt and save it as a .ps1 file and try that.

  • Hi Krutons 

    I can confirm that it works. After downloading the VOX_Test.txt file and renaming it, the script works well!