From 327423abb5ba738ddd5989849f2cdec1d2af5c90 Mon Sep 17 00:00:00 2001 From: oqyude Date: Fri, 10 Oct 2025 22:23:40 +0300 Subject: [PATCH] autostart module maybe? --- run.ps1 | 31 +++++++++++--- run.ps1.old | 39 ++++++++++++++++++ src/modules/autostart-manager.ps1 | 67 +++++++++++++++++++++++++++++++ src/vars.ps1 | 2 + 4 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 run.ps1.old create mode 100644 src/modules/autostart-manager.ps1 diff --git a/run.ps1 b/run.ps1 index dc28648..cd86ce6 100644 --- a/run.ps1 +++ b/run.ps1 @@ -12,14 +12,35 @@ if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdent Write-Host "Administrator privileges confirmed." -# Define available actions -$actions = @("reconnect", "connect", "disconnect") +# Define available modules with their respective actions +$modules = @{ + "appsDataManager" = @("reconnect", "connect", "disconnect") + "autostartManager" = @("update", "remove") +} + +# Interactive module selection +Write-Host "Select a module:" +$moduleNames = $modules.Keys | Sort-Object +for ($i = 0; $i -lt $moduleNames.Count; $i++) { + Write-Host "[$($i+1)] $($moduleNames[$i])" +} + +do { + $moduleSelection = Read-Host "Enter the number of your choice" + $validModule = ($moduleSelection -as [int]) -and ($moduleSelection -ge 1) -and ($moduleSelection -le $moduleNames.Count) + if (-not $validModule) { Write-Host "Invalid module selection. Try again." } +} until ($validModule) + +$selectedModule = $moduleNames[$moduleSelection - 1] +$actions = $modules[$selectedModule] + +Write-Host "Selected module: $selectedModule" # Determine action: from argument or interactive menu if ($args.Count -ge 1) { $action = $args[0] } else { - Write-Host "Select an action:" + Write-Host "Select an action for $selectedModule :" for ($i = 0; $i -lt $actions.Count; $i++) { Write-Host "[$($i+1)] $($actions[$i])" } @@ -35,5 +56,5 @@ if ($args.Count -ge 1) { Write-Host "Selected action: $action" -# Call with the chosen action -. $appsDataManager $action +# Call the selected module with the chosen action +. (Get-Variable $selectedModule).Value $action \ No newline at end of file diff --git a/run.ps1.old b/run.ps1.old new file mode 100644 index 0000000..dc28648 --- /dev/null +++ b/run.ps1.old @@ -0,0 +1,39 @@ +$initFile = Join-Path $PSScriptRoot ".\src\init.ps1" +. $initFile + +# Check for administrator privileges +if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { + Write-Host "The script requires administrator privileges. Restarting..." + + # Restart the script with admin rights + Start-Process -FilePath "powershell.exe" -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`"" -Verb RunAs + exit +} + +Write-Host "Administrator privileges confirmed." + +# Define available actions +$actions = @("reconnect", "connect", "disconnect") + +# Determine action: from argument or interactive menu +if ($args.Count -ge 1) { + $action = $args[0] +} else { + Write-Host "Select an action:" + for ($i = 0; $i -lt $actions.Count; $i++) { + Write-Host "[$($i+1)] $($actions[$i])" + } + + do { + $selection = Read-Host "Enter the number of your choice" + $valid = ($selection -as [int]) -and ($selection -ge 1) -and ($selection -le $actions.Count) + if (-not $valid) { Write-Host "Invalid selection. Try again." } + } until ($valid) + + $action = $actions[$selection - 1] +} + +Write-Host "Selected action: $action" + +# Call with the chosen action +. $appsDataManager $action diff --git a/src/modules/autostart-manager.ps1 b/src/modules/autostart-manager.ps1 new file mode 100644 index 0000000..d864f85 --- /dev/null +++ b/src/modules/autostart-manager.ps1 @@ -0,0 +1,67 @@ +param( + [string]$action = "update" # update, remove +) + +# $autostartDir = "C:\Path\To\Autostart" +$taskPrefix = "winos_" + +function Get-ManagedTasks { + Get-ScheduledTask | Where-Object {$_.TaskName -like "$taskPrefix*"} +} +function Update-Tasks($shortcut) { + $shell = New-Object -ComObject WScript.Shell + $sc = $shell.CreateShortcut($shortcut.FullName) + $taskName = "$taskPrefix$($shortcut.BaseName)" + + $existingTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue + + # Добавляем аргументы из ярлыка + $actionObj = New-ScheduledTaskAction -Execute $sc.TargetPath -Argument $sc.Arguments + if ($sc.WorkingDirectory) { $actionObj.WorkingDirectory = $sc.WorkingDirectory } + $trigger = New-ScheduledTaskTrigger -AtLogOn + + if ($existingTask) { + Set-ScheduledTask -TaskName $taskName -Action $actionObj -Trigger $trigger + } else { + Register-ScheduledTask -TaskName $taskName -Action $actionObj -Trigger $trigger -User $env:USERNAME -RunLevel Highest -Force + } +} + + +function Remove-AllTasks($tasks) { + foreach ($t in $tasks) { + Unregister-ScheduledTask -TaskName $t.TaskName -Confirm:$false + } +} + +switch ($action) { + "update" { + if (-Not (Test-Path $autostartDir)) { + Write-Error "Папка автозапуска не найдена: $autostartDir" + break + } + + # Получаем все ярлыки из папки + $shortcuts = Get-ChildItem -Path $autostartDir -Filter *.lnk + + # Создаем/обновляем задачи по ярлыкам + foreach ($sc in $shortcuts) { + Update-Tasks $sc + } + + # Удаляем задачи, которых нет в папке + $existingTasks = Get-ManagedTasks + foreach ($t in $existingTasks) { + $nameWithoutPrefix = $t.TaskName.Substring($taskPrefix.Length) + if (-Not ($shortcuts.BaseName -contains $nameWithoutPrefix)) { + Unregister-ScheduledTask -TaskName $t.TaskName -Confirm:$false + } + } + } + "remove" { + Remove-AllTasks (Get-ManagedTasks) + } + default { + Write-Error "Неизвестное действие: $action. Используйте 'update' или 'remove'." + } +} diff --git a/src/vars.ps1 b/src/vars.ps1 index 8dc7051..2ca4b1a 100644 --- a/src/vars.ps1 +++ b/src/vars.ps1 @@ -2,6 +2,7 @@ # Modules $appsDataManager = "$PSScriptRoot\modules\apps-data-manager.ps1" +$autostartManager = "$PSScriptRoot\modules\autostart-manager.ps1" # Package Manager Installers $winget = "$PSScriptRoot\winget.ps1" @@ -20,6 +21,7 @@ $appsAll = "$data\apps.csv" # GLOBAL $storage = "$env:USERPROFILE\Storage" +$autostartDir = "C:\Winos\Scenaries\Autorun" # $userName = "oqyude" # $diskLabel = "S:"