258 lines
10 KiB
PowerShell
258 lines
10 KiB
PowerShell
$configFilePath = Join-Path -Path $PSScriptRoot -ChildPath "config.json"
|
|
|
|
# Check if the configuration file exists
|
|
if (-Not (Test-Path $configFilePath)) {
|
|
Write-Error "Configuration file not found at $configFilePath"
|
|
exit 1
|
|
}
|
|
|
|
# Load the configuration file
|
|
$config = Get-Content $configFilePath | ConvertFrom-Json
|
|
|
|
# Variables for script paths
|
|
$backupScriptPath = $config.BackupScriptPath
|
|
$compareScriptPath = $config.CompareScriptPath
|
|
$outputPath = $config.OutputPath
|
|
|
|
# Import necessary assemblies for GUI
|
|
Add-Type -AssemblyName System.Windows.Forms
|
|
Add-Type -AssemblyName System.Drawing
|
|
|
|
$processing = $false
|
|
|
|
# Create Form
|
|
$form = New-Object System.Windows.Forms.Form
|
|
$form.Text = "NWA Sentinel"
|
|
$form.Size = New-Object System.Drawing.Size(800, 700)
|
|
$form.StartPosition = "CenterScreen"
|
|
|
|
# Create Backup Button
|
|
$backupButton = New-Object System.Windows.Forms.Button
|
|
$backupButton.Text = "Backup"
|
|
$backupButton.Size = New-Object System.Drawing.Size(100, 30)
|
|
$backupButton.Location = New-Object System.Drawing.Point(10, 10)
|
|
$form.Controls.Add($backupButton)
|
|
|
|
# Create Company List Box
|
|
$companyList = New-Object System.Windows.Forms.ListBox
|
|
$companyList.Size = New-Object System.Drawing.Size(200, 300)
|
|
$companyList.Location = New-Object System.Drawing.Point(10, 50)
|
|
$form.Controls.Add($companyList)
|
|
|
|
# Create File Details List Box
|
|
$fileDetailsListBox = New-Object System.Windows.Forms.ListBox
|
|
$fileDetailsListBox.Size = New-Object System.Drawing.Size(550, 300)
|
|
$fileDetailsListBox.Location = New-Object System.Drawing.Point(220, 50)
|
|
$form.Controls.Add($fileDetailsListBox)
|
|
|
|
# Create Compare Button
|
|
$compareButton = New-Object System.Windows.Forms.Button
|
|
$compareButton.Text = "Compare"
|
|
$compareButton.Size = New-Object System.Drawing.Size(100, 30)
|
|
$compareButton.Location = New-Object System.Drawing.Point(10, 360)
|
|
$compareButton.Enabled = $false
|
|
$form.Controls.Add($compareButton)
|
|
|
|
# Create QXP Button
|
|
$qxpButton = New-Object System.Windows.Forms.Button
|
|
$qxpButton.Text = "QXP"
|
|
$qxpButton.Size = New-Object System.Drawing.Size(100, 30)
|
|
$qxpButton.Location = New-Object System.Drawing.Point(120, 360)
|
|
$qxpButton.Enabled = $false
|
|
$form.Controls.Add($qxpButton)
|
|
|
|
# Create Compare Output Text Box
|
|
$compareOutputBox = New-Object System.Windows.Forms.TextBox
|
|
$compareOutputBox.Multiline = $true
|
|
$compareOutputBox.ReadOnly = $true
|
|
$compareOutputBox.ScrollBars = "Vertical"
|
|
$compareOutputBox.Size = New-Object System.Drawing.Size(760, 200)
|
|
$compareOutputBox.Location = New-Object System.Drawing.Point(10, 400)
|
|
$form.Controls.Add($compareOutputBox)
|
|
|
|
# Load Company Names into ListBox from manifest.json
|
|
$manifestPath = "C:\Users\$env:USERNAME\Desktop\NWABackup\manifest.json"
|
|
$manifestData = $null
|
|
if (Test-Path $manifestPath) {
|
|
try {
|
|
$manifestData = Get-Content -Path $manifestPath | ConvertFrom-Json
|
|
$companyNames = $manifestData.PSObject.Properties.Name
|
|
$companyList.Items.AddRange($companyNames)
|
|
} catch {
|
|
[System.Windows.Forms.MessageBox]::Show("Failed to parse manifest.json: $_", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
|
|
}
|
|
} else {
|
|
[System.Windows.Forms.MessageBox]::Show("Manifest file not found at $manifestPath", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
|
|
}
|
|
|
|
# Event: When a company is selected, populate file details
|
|
$companyList.Add_SelectedIndexChanged({
|
|
$selectedCompany = $companyList.SelectedItem
|
|
$compareOutputBox.Text = ""
|
|
$qxpButton.Enabled = $false # Disable the QXP button when the company changes
|
|
|
|
if ($selectedCompany -and $manifestData) {
|
|
$fileDetailsListBox.Items.Clear()
|
|
try {
|
|
# Retrieve the file groups for the selected company
|
|
$fileGroups = $manifestData.$selectedCompany.PSObject.Properties
|
|
|
|
foreach ($group in $fileGroups) {
|
|
$fileData = $group.Value
|
|
$datFiles = $fileData.DAT
|
|
$qxpFiles = $fileData.QXP
|
|
|
|
# Extract dates from zip file paths
|
|
$dates = $datFiles | ForEach-Object {
|
|
if ($_ -match "\\(\d{8})_(\d{6})\.zip$") {
|
|
# Combine date and time into a single string
|
|
$dateTimeString = "$($matches[1])$($matches[2])"
|
|
[datetime]::ParseExact($dateTimeString, "yyyyMMddHHmmss", $null)
|
|
} else {
|
|
$null
|
|
}
|
|
}
|
|
|
|
# Get the latest date
|
|
$latestDate = $dates | Where-Object { $_ } | Sort-Object -Descending | Select-Object -First 1
|
|
|
|
# Display the file group and its latest date
|
|
$latestDateText = if ($latestDate) {
|
|
$latestDate.ToString("yyyy-MM-dd HH:mm:ss")
|
|
} else {
|
|
"No valid date found"
|
|
}
|
|
|
|
$groupName = [string]$group.Name
|
|
$fileDetailsListBox.Items.Add("$latestDateText :: $groupName")
|
|
}
|
|
} catch {
|
|
[System.Windows.Forms.MessageBox]::Show("Error processing files for company '$selectedCompany': $_", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
|
|
}
|
|
}
|
|
})
|
|
|
|
$fileDetailsListBox.Add_SelectedIndexChanged({
|
|
$compareOutputBox.Text = "" # Clear the output box
|
|
$compareButton.Enabled = $true # Enable the Compare button
|
|
|
|
$selectedFileGroup = $fileDetailsListBox.SelectedItem
|
|
$selectedCompany = $companyList.SelectedItem
|
|
$qxpButton.Enabled = $false # Disable the QXP button by default
|
|
|
|
if ($selectedFileGroup -and $selectedCompany -and $manifestData) {
|
|
try {
|
|
# Extract the part name from the selected file group
|
|
$partName = $selectedFileGroup -replace ".* :: ", ""
|
|
|
|
# Check if the selected part has QXP files
|
|
$qxpFiles = $manifestData.$selectedCompany.$partName.QXP
|
|
|
|
if ($qxpFiles -and $qxpFiles.Count -gt 0) {
|
|
$qxpButton.Enabled = $true
|
|
}
|
|
} catch {
|
|
[System.Windows.Forms.MessageBox]::Show("Error checking QXP availability for '$selectedFileGroup': $_", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
|
|
}
|
|
}
|
|
})
|
|
|
|
# Backup Button Click Event
|
|
$backupButton.Add_Click({
|
|
if (Test-Path $backupScriptPath) {
|
|
Start-Process -FilePath "powershell.exe" -ArgumentList "-File `"$backupScriptPath`"" -NoNewWindow -Wait
|
|
[System.Windows.Forms.MessageBox]::Show("Backup completed.", "Information", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
|
|
} else {
|
|
[System.Windows.Forms.MessageBox]::Show("Backup script not found at $backupScriptPath", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
|
|
}
|
|
})
|
|
|
|
# Compare Button Click Event
|
|
$compareButton.Add_Click({
|
|
if ($processing) {
|
|
[System.Windows.Forms.MessageBox]::Show("Please wait for the current operation to complete.", "Info", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
|
|
return
|
|
}
|
|
|
|
$selectedCompany = $companyList.SelectedItem
|
|
$selectedFileGroup = $fileDetailsListBox.SelectedItem
|
|
|
|
if (-not $selectedCompany) {
|
|
[System.Windows.Forms.MessageBox]::Show("Please select a company from the list.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
|
|
return
|
|
}
|
|
|
|
if (-not $selectedFileGroup) {
|
|
[System.Windows.Forms.MessageBox]::Show("Please select a file group from the list.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
|
|
return
|
|
}
|
|
|
|
try {
|
|
$processing = $true
|
|
$backupButton.Enabled = $false
|
|
$compareButton.Enabled = $false
|
|
|
|
$compareOutputBox.text = ""
|
|
# Extract the file name (ignore the date and time)
|
|
$fileName = $selectedFileGroup -replace ".* :: ", ""
|
|
|
|
# Construct the PowerShell command
|
|
$command = "$compareScriptPath -CompanyName '$selectedCompany' -FileName '$fileName.DAT'"
|
|
|
|
# Run the compare script and redirect output
|
|
$processInfo = New-Object System.Diagnostics.ProcessStartInfo
|
|
$processInfo.FileName = "powershell.exe"
|
|
$processInfo.Arguments = "-NoProfile -Command `"$command`""
|
|
$processInfo.RedirectStandardOutput = $true
|
|
$processInfo.RedirectStandardError = $true
|
|
$processInfo.UseShellExecute = $false
|
|
$processInfo.CreateNoWindow = $true
|
|
|
|
$process = New-Object System.Diagnostics.Process
|
|
$process.StartInfo = $processInfo
|
|
$process.Start() | Out-Null
|
|
|
|
# Read both standard output and error output
|
|
$output = $process.StandardOutput.ReadToEnd()
|
|
$errorOutput = $process.StandardError.ReadToEnd()
|
|
$process.WaitForExit()
|
|
|
|
# Display the output or errors in the Compare Output TextBox
|
|
if ($output) {
|
|
$compareOutputBox.Text = $output -join "`r`n"
|
|
} elseif ($errorOutput) {
|
|
$compareOutputBox.Text = "Error: $errorOutput"
|
|
} else {
|
|
$compareOutputBox.Text = "No output received from the compare script."
|
|
}
|
|
|
|
} catch {
|
|
[System.Windows.Forms.MessageBox]::Show("Error running compare script: $($_.Exception.Message)", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
|
|
} finally {
|
|
$processing = $false
|
|
$backupButton.Enabled = $true
|
|
}
|
|
})
|
|
|
|
# Event: QXP Button Click
|
|
$qxpButton.Add_Click({
|
|
$selectedCompany = $companyList.SelectedItem
|
|
if (-not $selectedCompany) {
|
|
[System.Windows.Forms.MessageBox]::Show("Please select a company from the list.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
|
|
return
|
|
}
|
|
|
|
$selectedFileGroup = $fileDetailsListBox.SelectedItem
|
|
if (-not $selectedFileGroup) {
|
|
[System.Windows.Forms.MessageBox]::Show("Please select a file group from the list.", "Error", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
|
|
return
|
|
}
|
|
|
|
$fileName = $selectedFileGroup -replace ".* - ", ""
|
|
[System.Windows.Forms.MessageBox]::Show("Opening QXP file for ${selectedCompany}: $fileName", "QXP File", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
|
|
|
|
# Implement the actual logic for opening the QXP file if needed
|
|
})
|
|
|
|
# Show Form
|
|
$form.ShowDialog() |