Changed ${fileSizeKB} to $fileSizeKB to fix parsing error on some systems.
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude <noreply@anthropic.com>
277 lines
9.0 KiB
PowerShell
277 lines
9.0 KiB
PowerShell
# Backup-GERegistry.ps1
|
|
# Backs up GE Aircraft Engines registry keys from both 32-bit and 64-bit locations
|
|
# Saves to S:\DT\cameron\scan\backup\reg with naming: [machinenumber-]serialnumber-date.reg
|
|
|
|
param(
|
|
[Parameter(Mandatory=$false)]
|
|
[string]$BackupPath = "S:\DT\cameron\scan\backup\reg",
|
|
|
|
[Parameter(Mandatory=$false)]
|
|
[switch]$Silent = $false
|
|
)
|
|
|
|
# Function to write colored output (only if not silent)
|
|
function Write-ColorOutput {
|
|
param(
|
|
[string]$Message,
|
|
[string]$Color = "White"
|
|
)
|
|
if (-not $Silent) {
|
|
Write-Host $Message -ForegroundColor $Color
|
|
}
|
|
}
|
|
|
|
# Function to get machine number from registry
|
|
function Get-MachineNumber {
|
|
$machineNumber = $null
|
|
|
|
# Check GE Aircraft Engines registry paths for MachineNo
|
|
$gePaths = @(
|
|
"HKLM:\Software\GE Aircraft Engines\DNC\General",
|
|
"HKLM:\Software\WOW6432Node\GE Aircraft Engines\DNC\General"
|
|
)
|
|
|
|
foreach ($path in $gePaths) {
|
|
if (Test-Path $path) {
|
|
try {
|
|
$regKey = Get-ItemProperty -Path $path -ErrorAction SilentlyContinue
|
|
if ($regKey.MachineNo) {
|
|
$machineNumber = $regKey.MachineNo
|
|
Write-ColorOutput " Found MachineNo in registry: $machineNumber" "Green"
|
|
break
|
|
}
|
|
}
|
|
catch {
|
|
Write-ColorOutput " Could not read MachineNo from $path" "Yellow"
|
|
}
|
|
}
|
|
}
|
|
|
|
# If not found in registry, try hostname pattern matching
|
|
if (-not $machineNumber) {
|
|
$hostname = $env:COMPUTERNAME
|
|
if ($hostname -match '(\d{4})') {
|
|
$machineNumber = $matches[1]
|
|
Write-ColorOutput " Extracted machine number from hostname: $machineNumber" "Cyan"
|
|
}
|
|
}
|
|
|
|
return $machineNumber
|
|
}
|
|
|
|
# Function to get serial number
|
|
function Get-SerialNumber {
|
|
$serialNumber = $null
|
|
|
|
try {
|
|
# Get serial number from WMI
|
|
$bios = Get-WmiObject Win32_BIOS
|
|
if ($bios.SerialNumber -and $bios.SerialNumber -ne "To be filled by O.E.M.") {
|
|
$serialNumber = $bios.SerialNumber
|
|
Write-ColorOutput " Found serial number: $serialNumber" "Green"
|
|
}
|
|
}
|
|
catch {
|
|
Write-ColorOutput " Could not retrieve serial number from BIOS" "Yellow"
|
|
}
|
|
|
|
# If no serial number, use computer name as fallback
|
|
if (-not $serialNumber) {
|
|
$serialNumber = $env:COMPUTERNAME
|
|
Write-ColorOutput " Using computer name as identifier: $serialNumber" "Cyan"
|
|
}
|
|
|
|
return $serialNumber
|
|
}
|
|
|
|
# Function to export registry key
|
|
function Export-RegistryKey {
|
|
param(
|
|
[string]$RegistryPath,
|
|
[string]$OutputFile,
|
|
[string]$Description
|
|
)
|
|
|
|
# Convert PowerShell path to reg.exe format
|
|
$regPath = $RegistryPath -replace 'HKLM:', 'HKEY_LOCAL_MACHINE'
|
|
|
|
try {
|
|
# Use reg export command
|
|
$result = reg export "$regPath" "$OutputFile" /y 2>&1
|
|
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-ColorOutput " ✓ Exported $Description" "Green"
|
|
return $true
|
|
} else {
|
|
Write-ColorOutput " ✗ Failed to export $Description" "Red"
|
|
Write-ColorOutput " Error: $result" "Red"
|
|
return $false
|
|
}
|
|
}
|
|
catch {
|
|
Write-ColorOutput " ✗ Exception exporting $Description`: $_" "Red"
|
|
return $false
|
|
}
|
|
}
|
|
|
|
# Main script
|
|
Write-ColorOutput "`n=== GE Aircraft Engines Registry Backup ===" "Cyan"
|
|
Write-ColorOutput "Starting registry backup process..." "White"
|
|
|
|
# Get machine number and serial number
|
|
Write-ColorOutput "`nGathering system information:" "Yellow"
|
|
$machineNumber = Get-MachineNumber
|
|
$serialNumber = Get-SerialNumber
|
|
|
|
# Create backup filename
|
|
$dateStamp = Get-Date -Format "yyyyMMdd-HHmmss"
|
|
if ($machineNumber) {
|
|
$backupFileName = "${machineNumber}-${serialNumber}-${dateStamp}.reg"
|
|
} else {
|
|
$backupFileName = "${serialNumber}-${dateStamp}.reg"
|
|
}
|
|
|
|
# Create backup directory if it doesn't exist
|
|
if (-not (Test-Path $BackupPath)) {
|
|
try {
|
|
New-Item -ItemType Directory -Path $BackupPath -Force | Out-Null
|
|
Write-ColorOutput "`nCreated backup directory: $BackupPath" "Green"
|
|
}
|
|
catch {
|
|
Write-ColorOutput "`nFailed to create backup directory: $_" "Red"
|
|
exit 1
|
|
}
|
|
}
|
|
|
|
# Define registry paths to backup
|
|
$registryPaths = @{
|
|
'64bit' = @{
|
|
Path = 'HKLM:\SOFTWARE\GE Aircraft Engines'
|
|
Description = 'GE Aircraft Engines (64-bit)'
|
|
OutputFile = Join-Path $BackupPath "64bit-$backupFileName"
|
|
}
|
|
'32bit' = @{
|
|
Path = 'HKLM:\SOFTWARE\WOW6432Node\GE Aircraft Engines'
|
|
Description = 'GE Aircraft Engines (32-bit on 64-bit system)'
|
|
OutputFile = Join-Path $BackupPath "32bit-$backupFileName"
|
|
}
|
|
}
|
|
|
|
# Backup registries
|
|
Write-ColorOutput "`nBacking up GE Aircraft Engines registry keys:" "Yellow"
|
|
$backupResults = @()
|
|
|
|
foreach ($type in $registryPaths.Keys) {
|
|
$regInfo = $registryPaths[$type]
|
|
|
|
Write-ColorOutput "`n Checking $($regInfo.Description)..." "White"
|
|
|
|
if (Test-Path $regInfo.Path) {
|
|
Write-ColorOutput " Found registry key at: $($regInfo.Path)" "Green"
|
|
|
|
$success = Export-RegistryKey -RegistryPath $regInfo.Path `
|
|
-OutputFile $regInfo.OutputFile `
|
|
-Description $regInfo.Description
|
|
|
|
if ($success -and (Test-Path $regInfo.OutputFile)) {
|
|
$fileSize = (Get-Item $regInfo.OutputFile).Length
|
|
$fileSizeKB = [math]::Round($fileSize / 1KB, 2)
|
|
Write-ColorOutput " Saved to: $($regInfo.OutputFile) ($fileSizeKB KB)" "Green"
|
|
|
|
$backupResults += @{
|
|
Type = $type
|
|
File = $regInfo.OutputFile
|
|
Size = $fileSizeKB
|
|
Success = $true
|
|
}
|
|
} else {
|
|
$backupResults += @{
|
|
Type = $type
|
|
File = $regInfo.OutputFile
|
|
Success = $false
|
|
}
|
|
}
|
|
} else {
|
|
Write-ColorOutput " Registry key not found: $($regInfo.Path)" "Yellow"
|
|
Write-ColorOutput " Skipping $type backup" "Yellow"
|
|
}
|
|
}
|
|
|
|
# Combined backup (both 32-bit and 64-bit in one file)
|
|
Write-ColorOutput "`nCreating combined backup:" "Yellow"
|
|
$combinedFile = Join-Path $BackupPath $backupFileName
|
|
$combinedContent = @()
|
|
|
|
# Add header
|
|
$combinedContent += "Windows Registry Editor Version 5.00"
|
|
$combinedContent += ""
|
|
$combinedContent += "; GE Aircraft Engines Registry Backup"
|
|
$combinedContent += "; Machine: $machineNumber"
|
|
$combinedContent += "; Serial: $serialNumber"
|
|
$combinedContent += "; Date: $dateStamp"
|
|
$combinedContent += "; Computer: $env:COMPUTERNAME"
|
|
$combinedContent += ""
|
|
|
|
# Merge successful backups
|
|
$mergedCount = 0
|
|
foreach ($result in $backupResults | Where-Object { $_.Success }) {
|
|
if (Test-Path $result.File) {
|
|
Write-ColorOutput " Merging $($result.Type) backup..." "White"
|
|
|
|
# Read the file and skip the header
|
|
$content = Get-Content $result.File
|
|
if ($content.Count -gt 1) {
|
|
# Skip the "Windows Registry Editor Version 5.00" line
|
|
$contentToAdd = $content | Select-Object -Skip 1
|
|
|
|
$combinedContent += "; === $($result.Type) Registry ==="
|
|
$combinedContent += $contentToAdd
|
|
$combinedContent += ""
|
|
$mergedCount++
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($mergedCount -gt 0) {
|
|
try {
|
|
# Save combined file
|
|
$combinedContent | Out-File -FilePath $combinedFile -Encoding Unicode
|
|
|
|
$fileSize = (Get-Item $combinedFile).Length
|
|
$fileSizeKB = [math]::Round($fileSize / 1KB, 2)
|
|
|
|
Write-ColorOutput " Combined backup saved: $combinedFile ($fileSizeKB KB)" "Green"
|
|
|
|
# Clean up individual files if combined was successful
|
|
foreach ($result in $backupResults | Where-Object { $_.Success }) {
|
|
if (Test-Path $result.File) {
|
|
Remove-Item $result.File -Force
|
|
Write-ColorOutput " Cleaned up temporary file: $($result.Type)" "Gray"
|
|
}
|
|
}
|
|
}
|
|
catch {
|
|
Write-ColorOutput " ✗ Failed to create combined backup: $_" "Red"
|
|
}
|
|
} else {
|
|
Write-ColorOutput " ✗ No registry keys were successfully backed up" "Red"
|
|
}
|
|
|
|
# Summary
|
|
Write-ColorOutput "`n=== Backup Summary ===" "Cyan"
|
|
Write-ColorOutput "Machine Number: $(if ($machineNumber) { $machineNumber } else { 'Not found' })" "White"
|
|
Write-ColorOutput "Serial Number: $serialNumber" "White"
|
|
Write-ColorOutput "Backup Location: $BackupPath" "White"
|
|
Write-ColorOutput "Backup File: $backupFileName" "White"
|
|
|
|
$successCount = ($backupResults | Where-Object { $_.Success }).Count
|
|
$totalCount = $backupResults.Count
|
|
|
|
if ($successCount -gt 0) {
|
|
Write-ColorOutput "`nBackup completed: $successCount of $totalCount registries backed up successfully" "Green"
|
|
} else {
|
|
Write-ColorOutput "`nNo GE Aircraft Engines registry keys found to backup" "Yellow"
|
|
}
|
|
|
|
# Return success status
|
|
exit $(if ($successCount -gt 0) { 0 } else { 1 }) |