Skip to content

Instantly share code, notes, and snippets.

@recklessop
Created September 1, 2023 13:15
Show Gist options
  • Save recklessop/0c59b3a05f1b4238e41e3d93f09a6a63 to your computer and use it in GitHub Desktop.
Save recklessop/0c59b3a05f1b4238e41e3d93f09a6a63 to your computer and use it in GitHub Desktop.

Revisions

  1. recklessop created this gist Sep 1, 2023.
    47 changes: 47 additions & 0 deletions pwsh-client-secret.pwsh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    # Define variables
    $baseServer = "192.168.50.60"
    $keycloakBaseUrl = "https://$baseServer/auth" # Replace with your Keycloak server URL
    $zvmApiUrl = "https://$baseServer/v1"
    $realm = "zerto" # Replace with your Keycloak realm name
    $client_id = "api-script"
    $client_secret = "fcYMFuA5TkIUwp6b3hDUxim0f32z8erk" # Replace with your client's secret key

    $tokenUrl = "$keycloakBaseUrl/realms/$realm/protocol/openid-connect/token"

    # Define data as a hashtable
    $data = @{
    "grant_type" = "client_credentials"
    "client_id" = $client_id
    "client_secret" = $client_secret
    }

    # Disable SSL certificate verification (use with caution)
    $response = Invoke-RestMethod -Uri $tokenUrl -Method Post -Body $data -SkipCertificateCheck

    if ($response.status_code -eq 200) {
    $access_token = $response.access_token
    Write-Host "Authentication successful. Access token: $access_token"
    } else {
    Write-Host "Authentication failed. Status code: $($response.status_code)"
    Write-Host "Error message: $($response.text)"
    }

    $uri = "$zvmApiUrl/vpgs"
    Write-Host $uri

    $headers = @{
    "Authorization" = "Bearer $access_token"
    "accept" = "application/json"
    }

    # Make a GET request to the URI with the specified headers
    $response = Invoke-RestMethod -Uri $uri -Headers $headers -Method Get -SkipCertificateCheck

    # Check if the request was successful (HTTP status code 200)
    if ($response.status_code -eq 200) {
    # Output the response JSON
    Write-Host $response
    } else {
    Write-Host "Status code: $($response.status_code)"
    Write-Host "Error message: $($response)"
    }