Skip to content

Instantly share code, notes, and snippets.

@recklessop
Created September 1, 2023 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save recklessop/368e1ce2a1e84db5be22f218c8c79abc to your computer and use it in GitHub Desktop.
Save recklessop/368e1ce2a1e84db5be22f218c8c79abc to your computer and use it in GitHub Desktop.
powershell script to authenticate to zerto 10 using username and password
# 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 = "zerto-client" # Replace with your Keycloak client ID
$username = "admin"
$password = "Zertodata987!"
$tokenUrl = "$keycloakBaseUrl/realms/$realm/protocol/openid-connect/token"
# Define data as a hashtable
$data = @{
"grant_type" = "password"
"client_id" = $client_id
"username" = $username
"password" = $password
}
# Disable SSL certificate verification (use with caution)
$response = Invoke-RestMethod -Uri $tokenUrl -Method Post -Body $data -SkipCertificateCheck
# Check if the request was successful (HTTP status code 200)
if ($response.status_code -eq 200) {
$access_token = $response.access_token
# Output the access token or perform further actions
# 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"
$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)"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment