commit 6a9213812ad545726f4f1ac1a6db178d9556552c Author: Bryan Cerrati Date: Wed Apr 2 09:55:43 2025 -0400 first commit diff --git a/test1.py b/test1.py new file mode 100644 index 0000000..f644747 --- /dev/null +++ b/test1.py @@ -0,0 +1,58 @@ +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") \ No newline at end of file