Is it possible to get a list of VMs from a job that never ran?
Thanks
Code: Select all
import requests
import json
import urllib3
from urllib.parse import quote
# Disable InsecureRequestWarning
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
# Define ANSI escape codes for colors
GREEN = '\033[92m' # Green text
RED = '\033[91m' # Red text
RESET = '\033[0m' # Reset to default color
# Define your Veeam server API URL and credentials
veeam_api_url = "https://vbr.server.local:9419"
username = "veeamAPI"
password = "******************"
# Define headers
headers = {
"Content-Type": "application/x-www-form-urlencoded",
"accept": "application/json",
"x-api-version": "1.2-rev0"
}
data = f"grant_type=password&username={quote(username)}&password={quote(password)}"
# Bypass SSL certificate validation (optional)
response = requests.post(f"{veeam_api_url}/api/oauth2/token", data=data, headers=headers, verify=False)
print(response)
if response.status_code == 200:
token = f"Bearer {json.loads(response.content).get('access_token')}"
# Check if the authentication was successful
print(f"{GREEN}Successfully authenticated to the Veeam API{RESET}")
print(f"{GREEN}========================================================================================={RESET}")
headers['Authorization'] = token
# Get backup jobs
backup_jobs_url = f"{veeam_api_url}/api/v1/jobs/states"
backup_jobs_response = requests.get(backup_jobs_url, headers=headers, verify=False)
if backup_jobs_response.status_code == 200:
backup_jobs = backup_jobs_response.json()
# Process and display backup jobs
for job in backup_jobs.get('data', []):
job_name = job['name']
job_id = job['id']
job_type = job['type']
job_status = job['status']
job_lastResult = job['lastResult']
print(f"Backup Job Name: {job_name}\nType: {job_type}\nStatus: {job_status}\nLast Result: {job_lastResult}\nJOB ID: {job_id}")
# Get backup objects for the job
backup_objects = f"{veeam_api_url}/api/v1/backups?jobIdFilter={job_id}"
backup_objects_response = requests.get(backup_objects, headers=headers, verify=False)
if backup_objects_response.status_code == 200:
backup_objects_data = backup_objects_response.json()
for obj in backup_objects_data.get("data", []):
print(f"Backup Object ID: {obj['id']}")
# print(f"{GREEN}========================================================================================={RESET}")
# Now, fetch the list of VMs in this backup object
backup_object_id = obj['id']
backup_objects_vms_url = f"{veeam_api_url}/api/v1/backups/{backup_object_id}/objects"
backup_objects_vms_response = requests.get(backup_objects_vms_url, headers=headers, verify=False)
if backup_objects_vms_response.status_code == 200:
vms_data = backup_objects_vms_response.json()
print(f" - VMs in Backup Object {backup_object_id}:")
for vm in vms_data.get("data", []):
vm_name = vm['name']
vm_id = vm['id']
print(f"VM Name: {vm_name}") # , VM ID: {vm_id}")
else:
print(f"{RED}Failed to retrieve VMs for backup object {backup_object_id}, status code: {backup_objects_vms_response.status_code}{RESET}")
print(backup_objects_vms_response.text)
print(f"{GREEN}========================================================================================={RESET}")
else:
print(f"Failed to retrieve backup objects, status code: {backup_objects_response.status_code}")
print(backup_objects_response.text)
else:
print(f"{RED}Failed to retrieve backup jobs, status code: {backup_jobs_response.status_code}{RESET}")
print(backup_jobs_response.text)
else:
print(f"{RED}Authentication failed with status code: {response.status_code}{RESET}")
print(f"{RED}{response.text}{RESET}")
exit()