<#
.SYNOPSIS
This script updates a local Access database from a server and launches Microsoft Access with the updated database.
.DESCRIPTION
The script performs the following actions:
- Checks if the required local directory exists and creates it if it does not.
- Copies the specified files from a server to the local machine.
- Attempts to launch Microsoft Access with the updated database file.
- Logs actions to a log file and displays status messages via GUI pop-ups.
.VERSION
1.0
.AUTHOR
John Clark (with extensive help from ChatGPT!)
Design concept from Bill Mosca
.DATE
May 9, 2024
.NOTES
Ensure that the paths and permissions are correctly set before running this script.
Requires PowerShell 5.0 or later and appropriate permissions to access network resources and local paths.
.EXAMPLE
PS> .\UpdateAndLaunchAccess.ps1
#>
# Define constants
$cTXTFILE = "AUG_Demo_DB_feVer20240509b.pdf"
$cFE = "AUG_Demo.accdb"
$cICON = "aug_demo_icon.ico"
$cSVRPATH = "\\ls210d202\AccessUserGroups"
$cLOCPATH = "C:\AUG_Demo"
$cScriptPath = "\\ls210d202\AccessUserGroups\LaunchAUG_Demo.ps1"
$cLogPath = "\\ls210d202\AccessUserGroups\AUG_Demo_LogFile.log"
# Function to log messages
function Write-Log {
param ([string]$Message)
Add-content -Path $cLogPath -Value "$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss'): $Message"
}
# Function to create a desktop shortcut to this script if it doesn't exist
function CreateShortcut {
$DesktopPath = [System.Environment]::GetFolderPath('Desktop')
$ShortcutPath = Join-Path -Path $DesktopPath -ChildPath "Update and Launch AUG Demo.lnk"
if (-not (Test-Path $ShortcutPath)) {
$WshShell = New-Object -ComObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($ShortcutPath)
$Shortcut.TargetPath = "powershell.exe"
$Shortcut.Arguments = "-ExecutionPolicy Bypass -File `"$cScriptPath`""
$Shortcut.IconLocation = "$cLOCPATH\$cICON"
$Shortcut.Description = "Launch and Update AUG Demo Database"
$Shortcut.Save()
Write-Log "Shortcut created on Desktop."
}
}
# Function to copy database files from the server to the local machine
function UpdateDBFiles {
try {
Write-Log "Checking if local path exists."
if (-not (Test-Path $cLOCPATH)) {
New-Item -Path $cLOCPATH -ItemType Directory | Out-Null
Write-Log "Created local directory at $cLOCPATH."
}
Copy-Item -Path "$cSVRPATH\$cTXTFILE" -Destination $cLOCPATH -Force
Copy-Item -Path "$cSVRPATH\$cFE" -Destination $cLOCPATH -Force
Copy-Item -Path "$cSVRPATH\$cICON" -Destination $cLOCPATH -Force
Write-Log "Files updated successfully from $cSVRPATH to $cLOCPATH."
} catch {
Write-Log "Failed to update files: $_"
[System.Windows.MessageBox]::Show("Failed to update files: $_", "Error", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Error)
exit
}
}
# Function to launch the Access application with the updated database
function LaunchAccess {
try {
$msAccessPath = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\MSACCESS.EXE\').Path
if (-not $msAccessPath) {
Write-Log "MS Access is not installed."
[System.Windows.MessageBox]::Show("MS Access is not installed.", "Error", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Error)
exit
}
Start-Process -FilePath "$msAccessPath\msaccess.exe" -ArgumentList "`"$cLOCPATH\$cFE`""
Write-Log "MS Access launched with database: $cLOCPATH\$cFE."
} catch {
Write-Log "Failed to launch MS Access: $_"
[System.Windows.MessageBox]::Show("Failed to launch MS Access: $_", "Error", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Error)
exit
}
}
# Main function that runs update, launch, and shortcut check operations
function Main {
CreateShortcut
UpdateDBFiles
LaunchAccess
[System.Windows.MessageBox]::Show("Operation completed successfully!", "Success", [System.Windows.MessageBoxButton]::OK, [System.Windows.MessageBoxImage]::Information)
}
# Adding necessary .NET assembly for MessageBox
Add-Type -AssemblyName System.Windows.Forms
# Execute the main function
Main