param([switch]$UpdateIcon) $ErrorActionPreference = 'Stop' $shortcutProject = $PSScriptRoot $shortcutMarker = Join-Path $shortcutProject 'runtime\desktop-shortcut-created.txt' if ((Test-Path -LiteralPath $shortcutMarker) -and -not $UpdateIcon) { return } try { # Resolves the real Desktop, including a redirected/OneDrive Desktop. $shortcutDesktop = [Environment]::GetFolderPath('DesktopDirectory') if (-not $shortcutDesktop) { throw 'Windows Desktop folder is unavailable.' } $shortcutPath = Join-Path $shortcutDesktop 'SUB Control.lnk' # Never replace a shortcut the user already has. if (-not (Test-Path -LiteralPath $shortcutPath)) { $shortcutShell = New-Object -ComObject WScript.Shell $shortcutLink = $shortcutShell.CreateShortcut($shortcutPath) $shortcutLink.TargetPath = Join-Path $env:SystemRoot 'System32\WindowsPowerShell\v1.0\powershell.exe' $shortcutLink.Arguments = '-NoProfile -WindowStyle Hidden -File "' + (Join-Path $shortcutProject 'run.ps1') + '"' $shortcutLink.WorkingDirectory = $shortcutProject $shortcutLink.Description = 'Launch SUB Control' $shortcutLink.WindowStyle = 7 $shortcutIcon = Join-Path $shortcutProject 'assets\sub-control.ico' if (Test-Path -LiteralPath $shortcutIcon) { $shortcutLink.IconLocation = "$shortcutIcon,0" } $shortcutLink.Save() } elseif ($UpdateIcon) { $shortcutShell = New-Object -ComObject WScript.Shell $shortcutLink = $shortcutShell.CreateShortcut($shortcutPath) $expectedScript = '"' + (Join-Path $shortcutProject 'run.ps1') + '"' if ($shortcutLink.Arguments.Contains($expectedScript)) { $shortcutLink.IconLocation = (Join-Path $shortcutProject 'assets\sub-control.ico') + ',0' $shortcutLink.Save() } else { throw 'Existing shortcut belongs to another installation; left unchanged.' } } New-Item -ItemType Directory -Path (Split-Path -Parent $shortcutMarker) -Force | Out-Null Set-Content -LiteralPath $shortcutMarker -Value $shortcutPath } catch { # Desktop permissions must not prevent the controller from starting. Write-Warning "Could not create the SUB Control desktop shortcut: $_" }