Management Center and Reporting

 View Only
  • 1.  REST API with PowerShell

    Posted Mar 04, 2020 08:51 AM
    ​Hi all
    I'm Just sharing a couple of REST API tasters to get you started with REST APIs using Powershell, hopefully this will see more proxy/management centre REST API sharing ;-)
    they both connect to Management Centre using the same powershell commands, they do one or more GETs with the output displaying after being converted to JSON

    run them in the powershell ISE so you can see the output,

    the first just gets the details of all the policies in management Centre, including the UUIDs for each policies which we will use in the other powershell script
     first script

    #powershell SMC Rest API toe dipping Paul Gobey

    #run this from the Pwershell ISE at first to see the output

    #credentials bit
    $mycreds = Get-Credential yourSMCUserName

    #this section is required to get past the you're using a duff SSL Cert for your SMC
    add-type @"
        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 TrustAllCertsPolicy

    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Ssl3, [Net.SecurityProtocolType]::Tls, [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls12

    # the rest api URL
    $URL = "https://YourSMC-Address-or-hostname:8082/api/policies
    # the PS comand
    Invoke-RestMethod -Method 'Get' -Uri $url -Credential $mycreds  | ConvertTo-Json


    second script just add the following  at the end of the previous script replacing "UUID" with the UUID from one of your policies found when you ran the first script

    $URL = "https://YourManagementcentrehost name or address:8082/api/policies/UUID"
    $URL1 = "https://YourManagementcentrehost name or address:8082/api/policies/UUID/content"
    $URL2 = "https://YourManagementcentrehost name or address:8082/api/policies/UUID/targets"
    $URL3 = "https://YourManagementcentrehost name or address:8082/api/policies/UUID/versions"
    $URL4 = "https://YourManagementcentrehost name or address:8082/api/policies/UUID/attributes"

    $URL
    Invoke-RestMethod -Method 'Get' -Uri $url -Credential $mycreds  | ConvertTo-Json
    $URL1
    Invoke-RestMethod -Method 'Get' -Uri $url1 -Credential $mycreds | ConvertTo-Json
    $URL2
    Invoke-RestMethod -Method 'Get' -Uri $url2 -Credential $mycreds  | ConvertTo-Json
    $URL3
    Invoke-RestMethod -Method 'Get' -Uri $url3 -Credential $mycreds  | ConvertTo-Json
    $URL4
    Invoke-RestMethod -Method 'Get' -Uri $url4 -Credential $mycreds  | ConvertTo-Json