Advertisement
Microx007

Start-VeeamJob.ps1

Nov 3rd, 2024 (edited)
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2.     .SYNOPSIS
  3.         Startet ein Veeam VBR Job
  4.  
  5.     .DESCRIPTION
  6.         Startet einen VBR Job basierend auf den Job-Namen.
  7.         Ursprünglicher Zweck war einen Job (z.B. als Pre-Execution Skript) mit anderen Jobs zu verketten.
  8.  
  9.     .PARAMETER JobName
  10.         Eingabe des Jobs als Namen
  11.  
  12.     .PARAMETER JobType
  13.         Typ des Backup Jobs
  14.  
  15.         Erlaubte Typen: VAW, VAL, VMware,
  16.         Nicht erlaubte: Typen: HyperV, PVE
  17.  
  18.         Backup Typ          Pretty Name             Verbose                 Typ im Skript   Notizen
  19.         ########################################################################################################
  20.         Backup Copy Job     Backup Copy             SimpleBackupCopyPolicy  /               /
  21.         VAW Managed SRV     Windows Agent Backup    EpAgentBackup           VAW             CMDlet deprecated for Agent backups
  22.         VAW Managed PC      Windows Agent Policy    EpAgentPolicy           VAW             CMDlet deprecated for Agent backups
  23.         VAL Managed SRV     Linux Agent Backup      EpAgentBackup           VAL             CMDlet deprecated for Agent backups
  24.         VAL Managed PC      Linux Agent Policy      EpAgentPolicy           VAL             CMDlet (probably) deprecated for Agent backups # UNGETESTET WERTE!
  25.         Proxmox VE          Proxmox Backup          VmbApiPolicyTempJob     PVE             Aktuell nicht nutzbar mit Powershell via Start-VBRJob
  26.         VMware              VMware Backup Backup    Backup                  VMware
  27.         Hyper-V
  28.  
  29.     .EXAMPLE
  30.         .\Start-VeeamJob.ps1 -JobName "L1 Backup SERVER" -JobType "VMware"
  31.         Übergibt die Werte "L1 Backup SERVER" und "VMware" an das Skript
  32.        
  33.     .NOTES
  34.         Author  : Appoxo
  35.         Version : 2.4
  36.         Anmerkungen: Im Veeam Forum wird von Jobs verketten abgeraten -> https://forums.veeam.com/kvm-rhv-olvm-proxmox-f62/proxmox-issues-t95239.html#p526738
  37.         Job-Type und Job-ID auslesen: Get-VBRBackup | Select-Object -Property Name,TypeToString,JobType,Jobid,id,uid | Format-Table -AutoSize
  38.    
  39.     .LINK
  40.         -
  41. #>
  42.  
  43. [CmdletBinding()]
  44. Param(
  45.     [Parameter(Mandatory = $true,
  46.     HelpMessage = "Enter Job-Name of the VBR-Job")]
  47.     [String]
  48.     $JobName,
  49.  
  50.     [Parameter(Mandatory = $true,
  51.     HelpMessage = "Art des VBR-Jobs. Die Bezichnung ist NICHT canse-sensitiv!")]
  52.     [ValidateSet("VAW","VAL","VMware")] #Tab Vervollständigung der möglichen Werte
  53.     [string[]]
  54.     $JobType
  55. )
  56.  
  57. Begin {
  58.     Write-Host "Script started successfully"
  59.     $ExitCode = 0  
  60.  
  61.     #TimeStamp Logging:
  62.     function Get-TimeStamp {"{0:yyyy-MM-dd} {0:HH:mm:ss}" -f (Get-Date)}
  63.    
  64.     <#
  65.     #Debug Values:
  66.     $JobName = "L1 Backup Appoxo-PC2 (Games)"
  67.     $JobType = "VAW"
  68.     #>
  69.  
  70.     # Variablen
  71.     $workingDir = "C:\Skripte\SkriptLogs"
  72.     $log = "$($workingDir)\Log-StartVeeamJob.log"
  73.     $JobDetails = Get-VBRBackup -Name $JobName
  74.     $timeout = 9
  75.  
  76.     # Vorbereitung
  77.     if ($JobType -in @("VAW","VAL","VMware")){
  78.         Write-Host "Valid backup type selected"
  79.         $JobTypUnbestimmt = 0
  80.     }
  81.     else {
  82.         Write-Host "Invalid backup type selected. Please choose something else :)"
  83.         $ExitCode = 1
  84.         exit $ExitCode
  85.     }
  86.  
  87.     if (Test-Path -Path $workingDir) {
  88.     } else {
  89.         New-Item -ItemType Directory -Path "$workingDir"
  90.     }
  91.  
  92.     if (-not (Test-Path -Path $log -PathType Leaf)) {
  93.         New-Item -ItemType file -Path $log
  94.         Add-Content -Path $log "Log zur Überprüfung der Start von VBR-Jobs"
  95.     }
  96. }
  97.  
  98. Process {
  99.     Write-Host "You passed the following information:"
  100.     $data = @([PSCustomObject]@{"Selected Job Type"="$($JobType)";"Job Details"="$($JobDetails.Name)"})
  101.     $data | Format-Table -AutoSize
  102.     Write-Host "The following Job-ID was found for this job: $($JobDetails.JobId)"
  103.  
  104.     Write-Host "If there is an error please abort NOW."
  105.     while ($timeout -gt 0) {
  106.         Write-Host -NoNewline "`rThe backup starts in $($timeout)"
  107.         Start-Sleep -Seconds 1
  108.         $timeout--
  109.     }
  110.     Write-Host "`nStarting backup now!"
  111.     Write-Output "$(Get-TimeStamp) Start des Backup Job Skripts. Für den Job '$($JobDetails.Name)' wurde die Job-ID $($JobDetails.JobId) gefunden!" | Add-Content -Path $log
  112.  
  113.     try{
  114.         $startTime = Get-Date
  115.         Write-Host "Validating input... This may take a while"
  116.         if((($JobType -in @("VAW","VAL"))) -AND (($JobDetails.JobType -in @("EpAgentBackup","EpAgentPolicy")))) {
  117.             Write-Host "Valid backup type '$($JobDetails.TypeToString)' was found. Starting now!"
  118.             Start-VBRComputerBackupJob -Job $JobName | Select-Object -OutVariable JobResult
  119.         }
  120.         elseif (($JobType -in @("VMware")) -AND (($JobDetails.JobType -in @("Backup")))) {
  121.             Write-Host "Valid backup type '$($JobDetails.TypeToString)' was found. Starting now!"
  122.             Start-VBRJob -Job $JobName | Select-Object -OutVariable JobResult
  123.         }
  124.         elseif (($JobType -in @("PVE")) -AND (($JobDetails.JobType -in @("VmbApiPolicyTempJob")))) {
  125.             Write-Host "Der Job des Typs $JobType ist aktuell nicht implementiert"
  126.             $ExitCode = 1
  127.             exit $ExitCode
  128.             <#
  129.             Write-Host "Valid backup type '$($JobDetails.TypeToString)' was found. Starting now!"
  130.             Start-VBRJob -Job $JobName | Select-Object -OutVariable JobResult
  131.             #>
  132.         }
  133.         else {
  134.             Write-Host "Invalid backup type '$($JobDetails.TypeToString)' was found. Please restart the script!"
  135.             Write-Output "$(Get-TimeStamp) Bestimmung des Typs für den Job '$($JobDetails.Name)' nicht erfolgreich. Angegeben wurde '$($JobType)'" | Add-Content -Path $log
  136.             $ExitCode = 1
  137.             $JobTypUnbestimmt = 1
  138.         }
  139.  
  140.         # Job Result report
  141.         if(($JobTypUnbestimmt -EQ 0) -AND ($JobResult.State -EQ "Stopped") -AND ($JobResult.Result -EQ "Success")){
  142.             Write-Host "Execution of the Job '$($JobName) was successful"
  143.             Write-Output "$(Get-TimeStamp) Backup Job $($JobDetails.Name) erfolgreich ausgeführt" | Add-Content -Path $log
  144.             $ExitCode = 0
  145.         } else{
  146.             Write-Host "Execution of the Job '$($JobName) encountered an error. Please check the VBR-Console for more information"
  147.             Write-Output "$(Get-TimeStamp) Fehler beim ausführen vom Backup Job '$($JobDetails.Name)'" | Add-Content -Path $log
  148.             $ExitCode = 1
  149.         }
  150.         #Stats
  151.         $endTime = Get-Date
  152.         $executionTime = $endTime - $startTime
  153.     } catch {
  154.         Write-Host "Something went wrong during execution"
  155.         Write-Host $_  # This prints the actual error
  156.         Write-Output "$(Get-TimeStamp) Error: $($_)" | Add-Content -Path $log
  157.         $ExitCode = 1
  158.     }
  159. }
  160.  
  161. End {
  162.     Write-Output "$(Get-TimeStamp) Skript abgeschlossen für $($JobDetails.Name) Job-ID $($JobDetails.Id)" | Add-Content -Path $log
  163.     Write-Host "Script ended."
  164.     'Time for stats! The script took {0:N2} seconds or {1:N2} minutes)' -f $executionTime.TotalSeconds, ($executionTime.TotalSeconds / 60) | Write-Host #Suggestion from: https://www.reddit.com/r/PowerShell/comments/1gi6a2g/comment/lv3wcoa
  165.     exit $ExitCode
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement