-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp-launcher.ps1
More file actions
154 lines (124 loc) · 4.97 KB
/
app-launcher.ps1
File metadata and controls
154 lines (124 loc) · 4.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# Load forms assembly
Add-Type -AssemblyName System.Windows.Forms
# Function to get file checksum
function Get-FileChecksum {
param ([string]$filePath)
$hashOutput = certutil -hashfile $filePath SHA256
return $hashOutput[1] # Extract only the hash value
}
# Function to copy file with progress and verification
function Copy-FileWithVerification {
param (
[string]$source,
[string]$destination
)
$maxRetries = 3
$retryCount = 0
$success = $false
while (-not $success -and $retryCount -lt $maxRetries) {
Write-Host "Copying file... (Attempt $($retryCount + 1)/$maxRetries)" -ForegroundColor Yellow
# File size for progress tracking
$fileSize = (Get-Item $source).Length
$bufferSize = 1MB
$bytesCopied = 0
# Open file streams
$sourceStream = [System.IO.File]::OpenRead($source)
$destStream = [System.IO.File]::Create($destination)
$buffer = New-Object byte[] $bufferSize
$read = 0
while (($read = $sourceStream.Read($buffer, 0, $bufferSize)) -gt 0) {
$destStream.Write($buffer, 0, $read)
$bytesCopied += $read
# Update progress bar
$percentComplete = ($bytesCopied / $fileSize) * 100
Write-Progress -Activity "Copying File..." -PercentComplete $percentComplete -Status "$([math]::Round($percentComplete, 2))% Complete"
}
# Close file streams
$sourceStream.Close()
$destStream.Close()
# Verify checksum
if ((Get-FileChecksum -filePath $source) -eq (Get-FileChecksum -filePath $destination)) {
Write-Host "File copied successfully!" -ForegroundColor Green
$success = $true
} else {
Write-Host "Copy file verification did not succeed, retrying..." -ForegroundColor Red
$retryCount++
Start-Sleep -Seconds 2
}
}
return $success
}
# begin
# define user profile path
$localPath = "$env:USERPROFILE\Documents\SampleApp\"
# Ensure the directory exists
if (!(Test-Path $localPath)) {
New-Item -ItemType Directory -Path $localPath -Force
Write-Host "Created Directory: $localPath"
}
# clean up folder on userprofile / housekeeping / optional
$removeFileTypes = @("*.jpg", "*.copied", "*.bat", "*.ps1")
foreach ($file in $removeFileTypes) {
Get-ChildItem -Path $localPath -Filter $file -ErrorAction SilentlyContinue |
ForEach-Object {
try {
Remove-Item $_.FullName -Force -ErrorAction Stop
Write-Host "Removed: $($_.Name)" -ForegroundColor Cyan
}
catch {
Write-Host "Could not remove: $($_.Name): $_" -ForegroundColor DarkMagenta
}
}
}
# find newest .accde in local folder
$localFile = $null
$localFile = Get-ChildItem -Path $localPath -Filter *.accde |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1
$localFilePath = if ($localFile) { $localFile.FullName } else { $null }
# Get Current UserName
$user = $env:USERNAME
# Determine source directory for update file
$qUsers = @("admin1","admin2", "admin3")
if ($qUsers -contains $user) {
$sourcePath = "$env:USERPROFILE\OneDrive\SourceFileLocation1\"
} else {
$sourcePath = "$env:USERPROFILE\OneDrive\SourceFileLocation2\"
}
# Determine availability of $sourcePath and get name of .accde file
$sourceFile = $null
if (Test-Path $sourcePath) {
$sourceFile = Get-ChildItem -Path $sourcePath -Filter "*.accde" |
Sort-Object LastWriteTime -Descending |
Select-Object -First 1
$sourceFilePath = if ($sourceFile) { $sourceFile.FullName } else { $null }
}
if ($localFile -and (-not $sourceFile -or $sourceFile.Name -eq $localFile.Name)) {
Start-Process -FilePath $localFile.FullName
Exit
}
if ($sourceFile -and (!$localFile -or $sourceFile.Name -ne $localFile.Name)) {
#delete any .laccd or .accd files from local
Remove-Item (Join-Path $localPath "*.accde") -ErrorAction SilentlyContinue
Remove-Item (Join-Path $localPath "*.laccdb") -ErrorAction SilentlyContinue
# build the new file path
$localFileCopy = Join-Path $localPath $sourceFile.Name
#use I/O stream to copy file from $sourceFilePath
#checksum verify
#Copy and verify the new file
if (Copy-FileWithVerification -source $sourceFilePath -destination $localFileCopy) {
Start-Process -FilePath $localFileCopy
Exit
} else {
[System.Windows.Forms.MessageBox]::Show("File copy did not succeed after multiple attempts. Exiting.", "App Launcher",
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Exclamation)
Exit
}
}
If (-not $sourceFile -and -not $localFile) {
[System.Windows.Forms.MessageBox]::Show("No source or local .accde file available. Exiting.","App Launcher",
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Exclamation)
Exit
}