autostart module maybe?
This commit is contained in:
@@ -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
|
||||
+39
@@ -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
|
||||
@@ -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'."
|
||||
}
|
||||
}
|
||||
@@ -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:"
|
||||
|
||||
Reference in New Issue
Block a user