11Set-StrictMode - Version Latest
22$ErrorActionPreference = ' Continue'
33
4- # Fetch the latest release tag for downloading scripts
5- $releaseTag = " main"
6- try {
7- $latestRelease = Invoke-RestMethod - Uri " https://api.github.com/repos/Stensel8/WinDeploy/releases/latest" - ErrorAction SilentlyContinue
8- if ($latestRelease.tag_name ) {
9- $releaseTag = $latestRelease.tag_name
4+ # Fetch latest release with retry logic
5+ function Get-LatestRelease {
6+ param (
7+ [int ]$MaxRetries = 3 ,
8+ [int ]$RetryDelay = 5
9+ )
10+
11+ for ($attempt = 1 ; $attempt -le $MaxRetries ; $attempt ++ ) {
12+ try {
13+ $latestRelease = Invoke-RestMethod - Uri " https://api.github.com/repos/Stensel8/WinDeploy/releases/latest" - ErrorAction Stop
14+ if ($latestRelease.tag_name ) {
15+ return $latestRelease.tag_name
16+ }
17+ } catch {
18+ $errorMsg = $_.Exception.Message
19+ Write-Warning " Failed to fetch latest release (attempt $attempt /$MaxRetries ): $errorMsg "
20+
21+ if ($attempt -lt $MaxRetries ) {
22+ if ($errorMsg -match " 403|401" ) {
23+ Write-Host " Possible issue: GitHub API rate limit or authentication required" - ForegroundColor Yellow
24+ } elseif ($errorMsg -match " 404" ) {
25+ Write-Host " Possible issue: No releases found in repository" - ForegroundColor Yellow
26+ }
27+ Write-Host " Waiting $RetryDelay seconds before retry..." - ForegroundColor Cyan
28+ Start-Sleep - Seconds $RetryDelay
29+ }
30+ }
1031 }
11- } catch {
12- Write-Warning " Failed to fetch latest release, using main branch."
32+ return $null
33+ }
34+
35+ # Fetch the latest release tag - REQUIRED for deployment
36+ $releaseTag = Get-LatestRelease
37+ if (! $releaseTag ) {
38+ Write-Output " ERROR: Failed to fetch latest release from GitHub."
39+ Write-Output " Releases are required for deployment. Development branches are not used."
40+ Write-Output " Please check:"
41+ Write-Output " 1. Your internet connection"
42+ Write-Output " 2. GitHub API accessibility"
43+ Write-Output " 3. Repository has published releases: github.com/Stensel8/WinDeploy/releases"
44+ Read-Host " Press Enter to exit"
45+ exit 1
1346}
1447
1548# Read version
1649$version = $null
1750try {
1851 $version = Invoke-RestMethod - Uri " https://raw.githubusercontent.com/Stensel8/WinDeploy/$releaseTag /VERSION" - ErrorAction SilentlyContinue
1952 $version = $version.Trim ()
53+ if ($version ) {
54+ Write-Output " Version: $version "
55+ }
2056} catch {
2157 Write-Warning " Failed to fetch version information."
2258}
@@ -65,11 +101,61 @@ if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdenti
65101 exit 1
66102}
67103
104+ # Log execution context for debugging
105+ $scriptExecutionContext = Get-ScriptDisplay
106+ Write-DeployLog " Deployment started. Execution context: $scriptExecutionContext "
107+
108+ # Helper function to download scripts with retry logic
109+ function Get-DeploymentScript {
110+ param (
111+ [string ]$ScriptName ,
112+ [string ]$LocalPath ,
113+ [int ]$MaxRetries = 3 ,
114+ [int ]$RetryDelay = 5
115+ )
116+
117+ for ($attempt = 1 ; $attempt -le $MaxRetries ; $attempt ++ ) {
118+ try {
119+ Invoke-WebRequest - Uri " https://raw.githubusercontent.com/Stensel8/WinDeploy/$releaseTag /Scripts/Deployment/$ScriptName " - OutFile $LocalPath - UseBasicParsing - ErrorAction Stop
120+ Write-DeployLog " Downloaded $ScriptName to $LocalPath "
121+ return $true
122+ } catch {
123+ $errorMsg = $_.Exception.Message
124+ Write-Warning " Download attempt $attempt /$MaxRetries failed for $ScriptName `: $errorMsg "
125+
126+ if ($attempt -lt $MaxRetries ) {
127+ # Diagnose the issue
128+ if ($errorMsg -match " 403|401" ) {
129+ Write-Host " Possible issue: GitHub API rate limit reached" - ForegroundColor Yellow
130+ } elseif ($errorMsg -match " 404" ) {
131+ Write-Host " Possible issue: $ScriptName not found in release $releaseTag " - ForegroundColor Yellow
132+ } elseif ($errorMsg -match " timeout|timed out" ) {
133+ Write-Host " Possible issue: Network timeout" - ForegroundColor Yellow
134+ }
135+
136+ Write-Host " Waiting $RetryDelay seconds before retry..." - ForegroundColor Cyan
137+ Start-Sleep - Seconds $RetryDelay
138+ }
139+ }
140+ }
141+
142+ # If all download attempts failed, try local copy
143+ $localSourcePath = Join-Path - Path (Join-Path - Path $PSScriptRoot - ChildPath " Deployment" ) - ChildPath $ScriptName
144+ if (Test-Path $localSourcePath ) {
145+ Copy-Item $localSourcePath $LocalPath - Force
146+ Write-DeployLog " Copied local $ScriptName to $LocalPath "
147+ return $true
148+ }
149+
150+ Write-Warning " Cannot download or find $ScriptName after $MaxRetries attempts"
151+ return $false
152+ }
153+
68154# Define deployment steps (customize as needed)
69155$deploymentSteps = @ (
70156 @ { Name = " Driver Installation" ; ScriptName = " Install-Drivers.ps1" }
71157 @ { Name = " RMM Agent Installation" ; ScriptName = " Install-RMMAgent.ps1" }
72- @ { Name = " AutoRun Disable " ; ScriptName = " Disable-AutoRun .ps1" }
158+ @ { Name = " Windows Hardening " ; ScriptName = " Harden-Windows .ps1" }
73159 @ { Name = " Application Installation" ; ScriptName = " Install-Applications.ps1" }
74160 @ { Name = " Bloatware Removal" ; ScriptName = " Remove-Bloat.ps1" }
75161 @ { Name = " Theme Configuration" ; ScriptName = " Set-Theme.ps1" }
@@ -88,39 +174,46 @@ foreach ($step in $deploymentSteps) {
88174 Write-Output " $ ( $step.Name ) "
89175 Write-Output " ======================================== "
90176 Write-Output " "
177+
91178 $localPath = Join-Path - Path $dlRoot - ChildPath $step.ScriptName
92- $scriptAvailable = $false
93- try {
94- Invoke-WebRequest - Uri " https://raw.githubusercontent.com/Stensel8/WinDeploy/$releaseTag /Scripts/Deployment/$ ( $step.ScriptName ) " - OutFile $localPath - UseBasicParsing - ErrorAction Stop
95- Write-DeployLog " Downloaded $ ( $step.ScriptName ) to $localPath "
96- $scriptAvailable = $true
97- } catch {
98- # If download fails, use local copy if available
99- $localSourcePath = Join-Path - Path (Join-Path - Path $PSScriptRoot - ChildPath " Deployment" ) - ChildPath $step.ScriptName
100- if (Test-Path $localSourcePath ) {
101- Copy-Item $localSourcePath $localPath - Force
102- Write-DeployLog " Copied local $ ( $step.ScriptName ) to $localPath "
103- $scriptAvailable = $true
104- } else {
105- Write-Warning " Cannot download or find $ ( $step.ScriptName ) : $_ "
106- $allSuccessful = $false
107- continue
108- }
109- }
179+
180+ # Download script with retry logic
181+ $scriptAvailable = Get-DeploymentScript - ScriptName $step.ScriptName - LocalPath $localPath
182+
110183 if ($scriptAvailable ) {
111184 $argumentList = " -ExecutionPolicy Bypass -File `" $localPath `" "
112185 $proc = Start-Process pwsh - ArgumentList $argumentList - Wait - NoNewWindow - PassThru
113186 if ($proc.ExitCode -ne 0 ) {
187+ Write-Warning " $ ( $step.Name ) completed with errors (Exit Code: $ ( $proc.ExitCode ) )"
114188 $allSuccessful = $false
115189 }
190+ } else {
191+ Write-Warning " Skipping $ ( $step.Name ) - script unavailable"
192+ $allSuccessful = $false
116193 }
117194}
118195
119- # Download optional fix Spotlight script. Sometimes Spotlight option is not present and needs a little help.
196+ # Download optional fix Spotlight script with retry
197+ Write-Output " "
198+ Write-Output " Downloading optional scripts..."
120199try {
121200 $fixScriptPath = Join-Path $dlRoot " Fix-Spotlight.ps1"
122- Invoke-WebRequest - Uri " https://raw.githubusercontent.com/Stensel8/WinDeploy/$releaseTag /Scripts/Archived/Fix-Spotlight.ps1" - OutFile $fixScriptPath - UseBasicParsing - ErrorAction Stop
123- Write-DeployLog " Downloaded Fix-Spotlight.ps1 to $fixScriptPath as an optional script to fix missing Spotlight options on the system."
201+ $downloaded = $false
202+ for ($attempt = 1 ; $attempt -le 3 ; $attempt ++ ) {
203+ try {
204+ Invoke-WebRequest - Uri " https://raw.githubusercontent.com/Stensel8/WinDeploy/$releaseTag /Scripts/Archived/Fix-Spotlight.ps1" - OutFile $fixScriptPath - UseBasicParsing - ErrorAction Stop
205+ Write-DeployLog " Downloaded Fix-Spotlight.ps1 to $fixScriptPath as an optional script to fix missing Spotlight options on the system."
206+ $downloaded = $true
207+ break
208+ } catch {
209+ if ($attempt -lt 3 ) {
210+ Start-Sleep - Seconds 5
211+ }
212+ }
213+ }
214+ if (-not $downloaded ) {
215+ Write-DeployLog " Optional: Could not download Fix-Spotlight.ps1 after 3 attempts"
216+ }
124217} catch {
125218 Write-DeployLog " Optional: Could not download Fix-Spotlight.ps1: $_ "
126219}
0 commit comments