Skip to content

Commit e79a5fd

Browse files
updated the post deployment script
1 parent 8033a84 commit e79a5fd

1 file changed

Lines changed: 76 additions & 25 deletions

File tree

Deployment/send-filestoendpoint.psm1

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ function Send-FilesToEndpoint {
2727

2828
$totalFiles = $files.Count
2929
$currentFileIndex = 0
30+
$maxRetries = 3
31+
$retryDelaySeconds = 5
32+
$failedFiles = @()
33+
$successfulFiles = 0
3034

3135
foreach ($file in $files) {
3236
$currentFileIndex++
@@ -35,50 +39,97 @@ function Send-FilesToEndpoint {
3539

3640
# Check file size
3741
if ($file.Length -eq 0) {
38-
Write-Host "File cannot be uploaded: $($file.Name) (File size is 0)"
42+
Write-Host "⚠️ File cannot be uploaded: $($file.Name) (File size is 0)" -ForegroundColor Yellow
43+
$failedFiles += @{FileName = $file.Name; Reason = "File size is 0"}
3944
continue
4045
}
4146

4247
# Check file type
4348
$allowedExtensions = @(".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".tif", ".tiff", ".jpg", ".jpeg", ".png", ".bmp", ".txt")
4449
if (-Not ($allowedExtensions -contains $file.Extension.ToLower())) {
45-
Write-Host "File cannot be uploaded: $($file.Name) (Unsupported file type)"
50+
Write-Host "⚠️ File cannot be uploaded: $($file.Name) (Unsupported file type)" -ForegroundColor Yellow
51+
$failedFiles += @{FileName = $file.Name; Reason = "Unsupported file type"}
4652
continue
4753
}
4854

49-
try {
50-
# Read the file content as byte array
51-
$fileContent = [System.IO.File]::ReadAllBytes($file.FullName)
55+
# Retry logic for file upload
56+
$uploadSuccess = $false
57+
$attempt = 0
58+
59+
while ($attempt -lt $maxRetries -and -not $uploadSuccess) {
60+
$attempt++
61+
try {
62+
if ($attempt -gt 1) {
63+
Write-Host "🔄 Retry attempt $attempt of $maxRetries for file: $($file.Name)" -ForegroundColor Cyan
64+
Start-Sleep -Seconds $retryDelaySeconds
65+
}
66+
67+
# Read the file content as byte array
68+
$fileContent = [System.IO.File]::ReadAllBytes($file.FullName)
5269

53-
# Create the multipart form data content
54-
$content = [System.Net.Http.MultipartFormDataContent]::new()
55-
$fileContentByteArray = [System.Net.Http.ByteArrayContent]::new($fileContent)
56-
$fileContentByteArray.Headers.ContentDisposition = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
57-
$fileContentByteArray.Headers.ContentDisposition.Name = '"file"'
58-
$fileContentByteArray.Headers.ContentDisposition.FileName = '"' + $file.Name + '"'
59-
$content.Add($fileContentByteArray)
70+
# Create the multipart form data content
71+
$content = [System.Net.Http.MultipartFormDataContent]::new()
72+
$fileContentByteArray = [System.Net.Http.ByteArrayContent]::new($fileContent)
73+
$fileContentByteArray.Headers.ContentDisposition = [System.Net.Http.Headers.ContentDispositionHeaderValue]::new("form-data")
74+
$fileContentByteArray.Headers.ContentDisposition.Name = '"file"'
75+
$fileContentByteArray.Headers.ContentDisposition.FileName = '"' + $file.Name + '"'
76+
$content.Add($fileContentByteArray)
6077

61-
# Upload the file content to the HTTP endpoint
62-
$response = $httpClient.PostAsync($EndpointUrl, $content).GetAwaiter().GetResult()
63-
64-
65-
# Check the response status
66-
if ($response.IsSuccessStatusCode) {
67-
Write-Host "File uploaded successfully: $($file.Name)"
68-
}
69-
else {
70-
Write-Error "Failed to upload file: $($file.Name). Status code: $($response.StatusCode)"
78+
# Upload the file content to the HTTP endpoint
79+
$response = $httpClient.PostAsync($EndpointUrl, $content).GetAwaiter().GetResult()
80+
81+
82+
# Check the response status
83+
if ($response.IsSuccessStatusCode) {
84+
Write-Host "✅ File uploaded successfully: $($file.Name)" -ForegroundColor Green
85+
$uploadSuccess = $true
86+
$successfulFiles++
87+
}
88+
else {
89+
$statusCode = $response.StatusCode
90+
if ($attempt -lt $maxRetries) {
91+
Write-Host "⚠️ Failed to upload file: $($file.Name). Status code: $statusCode. Will retry..." -ForegroundColor Yellow
92+
} else {
93+
Write-Host "❌ Failed to upload file: $($file.Name). Status code: $statusCode. Max retries reached." -ForegroundColor Red
94+
$failedFiles += @{FileName = $file.Name; Reason = "HTTP Status: $statusCode"}
95+
}
96+
}
97+
}
98+
catch {
99+
if ($attempt -lt $maxRetries) {
100+
Write-Host "⚠️ Error uploading file: $($file.Name). Error: $($_.Exception.Message). Will retry..." -ForegroundColor Yellow
101+
} else {
102+
Write-Host "❌ Error uploading file: $($file.Name). Error: $($_.Exception.Message). Max retries reached." -ForegroundColor Red
103+
$failedFiles += @{FileName = $file.Name; Reason = $_.Exception.Message}
104+
}
71105
}
72-
}
73-
catch {
74-
Write-Error "An error occurred while uploading the file: $($file.Name). Error: $_"
75106
}
76107
}
77108
# Dispose HttpClient
78109
$httpClient.Dispose()
79110

80111
# Clear the progress bar
81112
Write-Progress -Activity "Uploading Files" -Status "Completed" -PercentComplete 100
113+
114+
# Print summary report
115+
Write-Host "`n========================================" -ForegroundColor Cyan
116+
Write-Host "📊 File Upload Summary" -ForegroundColor Cyan
117+
Write-Host "========================================" -ForegroundColor Cyan
118+
Write-Host "Total files processed: $totalFiles" -ForegroundColor White
119+
Write-Host "✅ Successfully uploaded: $successfulFiles" -ForegroundColor Green
120+
Write-Host "❌ Failed uploads: $($failedFiles.Count)" -ForegroundColor Red
121+
122+
if ($failedFiles.Count -gt 0) {
123+
Write-Host "`n❌ Failed Files Details:" -ForegroundColor Red
124+
foreach ($failed in $failedFiles) {
125+
Write-Host "$($failed.FileName) - Reason: $($failed.Reason)" -ForegroundColor Yellow
126+
}
127+
Write-Host "`n⚠️ Warning: Some files failed to upload after $maxRetries retry attempts." -ForegroundColor Yellow
128+
Write-Host "You can manually retry uploading the failed files later." -ForegroundColor Yellow
129+
} else {
130+
Write-Host "`n✅ All files uploaded successfully!" -ForegroundColor Green
131+
}
132+
Write-Host "========================================`n" -ForegroundColor Cyan
82133
}
83134

84135
Export-ModuleMember -Function Send-FilesToEndpoint

0 commit comments

Comments
 (0)