четверг, 8 июня 2023 г.

VEEAM GUI - single Job sessions.1.0

 cls


Write-Host "VEEAM GUI - single Job sessions.1.0" -BackgroundColor DarkRed -ForegroundColor Yellow

Write-Host "Wait...Wait...Wait..."


# Importing necessary .NET Assembly

Add-Type -AssemblyName System.Windows.Forms

[System.Windows.Forms.Application]::EnableVisualStyles()


# Main Form

$MainForm = New-Object System.Windows.Forms.Form

$MainForm.Text = 'Veeam Tape Job'

$MainForm.Size = New-Object System.Drawing.Size(350, 400)

$MainForm.StartPosition = 'CenterScreen'

$MainForm.TopMost = $true


# Prepare for CSV Export

$OutputPath = 'C:\Veeam-Logs'


if (!(Test-Path -Path $OutputPath -PathType Container)) {

    $null = New-Item -ItemType Directory -Path $OutputPath

}


# Fetch all backup sessions upfront

$BackupSessions = Get-VBRBackupSession


# GUI elements


# Jobs ListBox

$ListBoxJobs = New-Object System.Windows.Forms.ListBox

$ListBoxJobs.Location = New-Object System.Drawing.Point(10, 10)

$ListBoxJobs.Size = New-Object System.Drawing.Size(300, 100)


# Checkbox for OGV output

$CheckboxOgvOutput = New-Object System.Windows.Forms.CheckBox

$CheckboxOgvOutput.Location = New-Object System.Drawing.Point(10, 130)

$CheckboxOgvOutput.Text = 'Enable OGV Output'

$CheckboxOgvOutput.AutoSize = $true


# Checkbox for sending only Error output

$CheckboxOnlyError = New-Object System.Windows.Forms.CheckBox

$CheckboxOnlyError.Location = New-Object System.Drawing.Point(10, 160)

$CheckboxOnlyError.Text = 'Only send Error output'

$CheckboxOnlyError.AutoSize = $true


# Email Label

$LabelEmail = New-Object System.Windows.Forms.Label

$LabelEmail.Text = 'Email'

$LabelEmail.AutoSize = $true

$LabelEmail.Location = New-Object System.Drawing.Point(10, 200)


# Email TextBox

$TextBoxEmail = New-Object System.Windows.Forms.TextBox

$TextBoxEmail.Location = New-Object System.Drawing.Point(10, 230)

$TextBoxEmail.Width = 300

$TextBoxEmail.BackColor = 'LightGray'


# Checkbox to include other recipients

$CheckboxIncludeOthers = New-Object System.Windows.Forms.CheckBox

$CheckboxIncludeOthers.Location = New-Object System.Drawing.Point(10, 260)

$CheckboxIncludeOthers.Text = 'Include other recipients'

$CheckboxIncludeOthers.AutoSize = $true


# Export Button

$ButtonExport = New-Object System.Windows.Forms.Button

$ButtonExport.Location = New-Object System.Drawing.Point(10, 310)

$ButtonExport.Size = New-Object System.Drawing.Size(100, 30)

$ButtonExport.Text = 'Export CSV'

$ButtonExport.BackColor = 'Blue'

$ButtonExport.ForeColor = 'White'


# Send Mail Button

$ButtonSendMail = New-Object System.Windows.Forms.Button

$ButtonSendMail.Location = New-Object System.Drawing.Point(120, 310)

$ButtonSendMail.Size = New-Object System.Drawing.Size(100, 30)

$ButtonSendMail.Text = 'Send Mail'

$ButtonSendMail.BackColor = 'Blue'

$ButtonSendMail.ForeColor = 'White'


# Open CSV Folder Button

$ButtonOpenFolder = New-Object System.Windows.Forms.Button

$ButtonOpenFolder.Location = New-Object System.Drawing.Point(230, 310)

$ButtonOpenFolder.Size = New-Object System.Drawing.Size(100, 30)

$ButtonOpenFolder.Text = 'Open Folder'

$ButtonOpenFolder.BackColor = 'Blue'

$ButtonOpenFolder.ForeColor = 'White'


# Label for authoring information

$LabelAuthoring = New-Object System.Windows.Forms.Label

$LabelAuthoring.Text = "Created by Vladisla2000 and ChatGPT"

$LabelAuthoring.AutoSize = $true

$LabelAuthoring.Location = New-Object System.Drawing.Point(10, 360)


# Progress Bar

$ProgressBar = New-Object System.Windows.Forms.ProgressBar

$ProgressBar.Location = New-Object System.Drawing.Point(10, 290)

$ProgressBar.Size = New-Object System.Drawing.Size(320, 15)

$ProgressBar.Style = "Continuous"

$ProgressBar.Visible = $false


# Add controls to MainForm

$MainForm.Controls.AddRange(@($ListBoxJobs, $CheckboxOgvOutput, $CheckboxOnlyError, $LabelEmail, $TextBoxEmail, $CheckboxIncludeOthers, $ButtonExport, $ButtonSendMail, $ButtonOpenFolder, $LabelAuthoring, $ProgressBar))


# Fill the ListBox with job names when the MainForm is loaded

$MainForm.Add_Load({

    $Jobs = Get-VBRJob

    foreach ($Job in $Jobs) {

        $ListBoxJobs.Items.Add($Job.Name)

    }

})


# Define the action for the button click to export CSV

$ButtonExport_Click = {

    $SelectedJob = $ListBoxJobs.SelectedItem

    if ($null -ne $SelectedJob) {


        # Get job information

        $Job = Get-VBRJob | Where-Object { $_.Name -eq $SelectedJob }


        # Define date one year ago

        $OneYearAgo = (Get-Date).AddYears(-1)


        # Filter job sessions for the selected job from the last year

        $JobSessions = $BackupSessions | Where-Object { $_.JobId -eq $Job.Id -and $_.CreationTime -gt $OneYearAgo }


        # Prepare CSV Export

        $CsvExport = $JobSessions | ForEach-Object {

            [PSCustomObject]@{

                'JobName' = $Job.Name

                'SessionName' = $_.Name

                'Result' = $_.Result

                'CreationTime' = $_.CreationTime

            }

        }


        # Define the output filename to include date and hour

        $OutputFileName = "$($Job.Name)_$(Get-Date -Format "yyyyMMdd_HHmm").csv"


        # Export to CSV

        $CsvExport | Export-Csv -Path "$OutputPath\$OutputFileName" -NoTypeInformation


        # If CheckboxOgvOutput is checked, output the CSV content to Out-GridView

        if ($CheckboxOgvOutput.Checked) {


            $CsvExport | Out-GridView

        }


        # If CheckboxOnlyError is checked, filter the jobs with 'Failed' status

        if ($CheckboxOnlyError.Checked) {

            $CsvExport = $CsvExport | Where-Object { $_.Result -eq 'Failed' }

        }


        # Show Export Completed message

        [System.Windows.Forms.MessageBox]::Show("Export completed", "Information")


    } else {

        [System.Windows.Forms.MessageBox]::Show("Please select a job from the list", "Information")

    }

}


$ButtonExport.Add_Click($ButtonExport_Click)


# Define the action for the button click to send mail

$ButtonSendMail_Click = {

    # Send Email

    if ($TextBoxEmail.Text) {

        $SmtpServer = "your.smtp.server"

        $From = vladsp@gamaf.co.il

        $To = vladsp@gamaf.co.il

        $Subject = "Veeam Backup Job Status"

        $Body = "Please find the attached CSV file for Veeam Backup Job Status."

        $Attachment = "$OutputPath\$($ListBoxJobs.SelectedItem)_$(Get-Date -Format "yyyyMMdd_HHmm").csv"


        if ($CheckboxIncludeOthers.Checked) {

            $To += ",additional.email@domain.com"

        }


        Send-MailMessage -From $From -To $To -SmtpServer $SmtpServer -Subject $Subject -Body $Body -Attachments $Attachment


        # Show Email Sent message

        [System.Windows.Forms.MessageBox]::Show("Email sent", "Information")


    } else {

        [System.Windows.Forms.MessageBox]::Show("Please enter an email address", "Warning")

    }

}


$ButtonSendMail.Add_Click($ButtonSendMail_Click)


# Define the action for the button click to open the CSV folder

$ButtonOpenFolder_Click = {

    # Open the folder in Explorer

    Invoke-Item -Path $OutputPath

}

$ButtonOpenFolder.Add_Click($ButtonOpenFolder_Click)


# Show MainForm

$MainForm.ShowDialog()

# Dispose MainForm after use

$MainForm.Dispose()

Комментариев нет:

Отправить комментарий