this post was submitted on 02 Nov 2024
8 points (100.0% liked)

Powershell

1034 readers
2 users here now

PowerShell (POSH) is a a task automation command-line shell and scripting language created by Microsoft. It became part of the FOSS community in 2016 and is now available across Windows, Linux, and macOS

Resources:


Rules:

Self-promotion rules:


founded 2 years ago
MODERATORS
 

Script is made to control Veeam VBR
Thanks for taking a look at my massive feature creep ;)

<#
    .SYNOPSIS
        Startet ein Veeam VBR Job

    .DESCRIPTION
        Startet einen VBR Job basierend auf den Namen.
        Ursprünglicher Zweck war ein Verknüpfung von Jobs (z.B. als Pre-Execution Skript)

    .PARAMETER JobName
        Job-Name des Backup Jobs

    .PARAMETER JobType
        Typ des Backup Jobs
        Erlaubte Typen: VAW, VAL, VMware,
        Nicht erlaubte: Typen: HyperV, PVE

        $Get-VBRBackup | Select-Object -Property Name,TypeToString,JobType
        Backup              Pretty                  Verbose                 Typ im Skript   Notizen
        ########################################################################################################
        Backup Copy Job     Backup Copy             SimpleBackupCopyPolicy  /               /
        VAW Managed SRV     Windows Agent Backup    EpAgentBackup           VAW             CMDlet deprecated for Agent backups
        VAW Managed PC      Windows Agent Policy    EpAgentPolicy           VAW             CMDlet deprecated for Agent backups
        VAL Managed SRV     Linux Agent Backup      EpAgentBackup           VAL             CMDlet deprecated for Agent backups
        VAL Managed PC      Linux Agent Policy      EpAgentPolicy           VAL             CMDlet (probably) deprecated for Agent backups # UNGETESTET WERTE! 
        Proxmox VE          Proxmox Backup          VmbApiPolicyTempJob     PVE             Nicht nutzbar mit Powershell via Start-VBRJob
        VMware              VMware Backup Backup    Backup                  VMware
        Hyper-V

    .EXAMPLE
        Start-VeeamJob.ps1 -JobName 
        passes F1234567-1abc-1234-ab1c-1a2345b6c78d to $JobName
        
    .NOTES
        Author  : Appoxo
        Version : 2.0
    
    .LINK
        Job-ID auslesen:
            Get-VBRComputerBackupJob | Where-Object Name -CLike "*Name*" | Select-Object -Property Id, Name
#>

[CmdletBinding()]
Param(
    [Parameter(Mandatory = $true,
    HelpMessage = "Enter Job-Name of the VBR-Job")]
    [String]
    $JobName,

    [Parameter(Mandatory = $true,
    HelpMessage = "Art des VBR-Jobs. Die Bezichnung ist NICHT canse-sensitiv!")]
    [string]
    $JobType
)

Begin {
    Write-Host "Script started successfully"
    $ExitCode = 0  

    #TimeStamp Logging:
    function Get-TimeStamp {return "{0:yy/MM/dd} {0:HH:mm:ss}" -f (Get-Date)}
    
    <#
    #Debug Values:
    $JobName = "L1 Backup Appoxo-PC2 (Games)"
    $JobType = "VAW"
    #>

    # Variablen
    $workingDir = "C:\Skripte\SkriptLogs"
    $log = "$($workingDir)\Log-StartVeeamJob.log"
    $JobDetails = Get-VBRBackup | Where-Object Name -EQ "$($JobName)"
    $timeout = 9

    # Vorbereitung
    if ($JobType -in @("VAW","VAL","VMware")){
        Write-Host "Valid backup type selected"
        $JobTypUnbestimmt = 0
    }
    else {
        Write-Host "Invalid backup type selected. Please choose something else :)"
        $ExitCode = 1
        exit $ExitCode
    }

    if (Test-Path -Path $workingDir) {
    } else {
        New-Item -ItemType Directory -Path "$workingDir"
    }

    if (-not (Test-Path -Path $log -PathType Leaf)) {
        New-Item -ItemType file -Path $log
        Add-Content -Path $log "Log zur Überprüfung der Start von VBR-Jobs"
    }
}

Process {
    Write-Host "You passed the following information:"
    $data = @([PSCustomObject]@{"Job Details"="$($JobDetails.Name)"; "Selected Job Type"="$($JobType)"})
    $data | Format-Table -AutoSize
    Write-Host "The following Job-ID was found for this job: $($JobDetails.JobId)"

    Write-Host "If there is an error please abort NOW." 
    while ($timeout -gt 0) {
        Write-Host -NoNewline "`rThe script starts in $($timeout)"
        Start-Sleep -Seconds 1
        $timeout--
    }
    Write-Host "Starting script now!"
    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

    try{
        $startTime = Get-Date
        Write-Host "Validating input... This may take a while"
        if((($JobType -in @("VAW","VAL"))) -AND (($JobDetails.JobType -in @("EpAgentBackup","EpAgentPolicy")))) {
            Write-Host "Valid backup type '$($JobDetails.TypeToString)' was found. Starting now!"
            Start-VBRComputerBackupJob -Job $JobName | Select-Object -OutVariable JobResult
        }
        elseif (($JobType -in @("VMware")) -AND (($JobDetails.JobType -in @("Backup")))) {
            Write-Host "Valid backup type '$($JobDetails.TypeToString)' was found. Starting now!"
            Start-VBRJob -Job $JobName | Select-Object -OutVariable JobResult
        }
        elseif (($JobType -in @("PVE")) -AND (($JobDetails.JobType -in @("VmbApiPolicyTempJob")))) {
            Write-Host "Der Job des Typs $JobType ist aktuell nicht implementiert"
            $ExitCode = 1
            exit $ExitCode
            <#
            Write-Host "Valid backup type '$($JobDetails.TypeToString)' was found. Starting now!"
            Start-VBRJob -Job $JobName | Select-Object -OutVariable JobResult
            #>
        }
        else {
            Write-Host "Invalid backup type '$($JobDetails.TypeToString)' was found. Please restart the script!"
            Write-Output "$(Get-TimeStamp) Bestimmung des Typs für den Job '$($JobDetails.Name)' nicht erfolgreich. Angegeben wurde '$($JobType)'" | Add-Content -Path $log
            $ExitCode = 1
            $JobTypUnbestimmt = 1
        }

        # Job Result report
        if(($JobTypUnbestimmt -EQ 0) -AND ($JobResult.State -EQ "Stopped") -AND ($JobResult.Result -EQ "Success")){
            Write-Host "Execution of the Job '$($JobName) was successful"
            Write-Output "$(Get-TimeStamp) Backup Job $($JobDetails.Name) erfolgreich ausgeführt" | Add-Content -Path $log
            $ExitCode = 0
        } else{
            Write-Host "Execution of the Job '$($JobName) encountered an error. Please check the VBR-Console"
            Write-Output "$(Get-TimeStamp) Fehler beim ausführen vom Backup Job '$($JobDetails.Name)'" | Add-Content -Path $log
            $ExitCode = 1
        }
        #Stats
        $endTime = Get-Date
        $executionTime = $endTime - $startTime
    } catch {
        Write-Host "Something went wrong during execution"
        Write-Host $_  # This prints the actual error
        Write-Output "$(Get-TimeStamp) Error: $($_)" | Add-Content -Path $log
        $ExitCode = 1 
    }
}

End {
    Write-Output "$(Get-TimeStamp) Skript abgeschlossen für $($JobDetails.Name) Job-ID $($JobDetails.Id)" | Add-Content -Path $log
    Write-Host "Script ended."
    $seconds = "{0:N2}" -f $executionTime.TotalSeconds
    $minutes = "{0:N2}" -f ($executionTime.TotalSeconds / 60)
    Write-Host "Time for stats! The script took $($seconds) seconds or $($minutes) minutes)"
    exit $ExitCode
}
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 2 points 1 month ago

Saved for reference. Most likely I won't improve this script as it was just a hobby experiment and has little value at work but I will definitely keep your advice in mind for future scripts with actual value.
And I believe I already have some scripts in mind. :)

Thank you very much for taking a look!