import subprocess import json # Define the PowerShell script as a multiline string. # This script retrieves all Veeam jobs, finds the last session for each job, # and outputs a JSON array with job Name, Last Run Status, and Last Run Time. powershell_script = r''' $ErrorActionPreference = "Stop" # Import Veeam PowerShell Snap-In (if not already loaded) if (-not (Get-PSSnapin -Name VeeamPSSnapin -ErrorAction SilentlyContinue)) { Add-PSSnapin VeeamPSSnapin } $jobs = Get-VBRJob $jobInfo = @() foreach ($job in $jobs) { $session = $job.FindLastSession() if ($session -ne $null) { $jobInfo += [PSCustomObject]@{ Name = $job.Name LastRunStatus = $session.Result LastRunTime = $session.EndTime } } else { $jobInfo += [PSCustomObject]@{ Name = $job.Name LastRunStatus = "Never run" LastRunTime = "" } } } $jobInfo | ConvertTo-Json -Depth 3 ''' try: # Execute the PowerShell script via subprocess. result = subprocess.run(["powershell", "-Command", powershell_script], capture_output=True, text=True, check=True) except subprocess.CalledProcessError as err: print("Error running PowerShell command:") print(err.stderr) exit(1) try: # Parse the JSON output from PowerShell. jobs = json.loads(result.stdout) # In case only one job is returned, wrap it in a list. if isinstance(jobs, dict): jobs = [jobs] except Exception as e: print("Error parsing JSON output:", e) exit(1) # Display the job information. for job in jobs: print(f"Job: {job['Name']}") print(f" Last Run Status: {job['LastRunStatus']}") print(f" Last Run Time: {job['LastRunTime']}\n")