-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_task.py
More file actions
67 lines (57 loc) · 2.43 KB
/
create_task.py
File metadata and controls
67 lines (57 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import ctypes
import os
import subprocess
import sys
from pathlib import Path
def create_scheduled_task():
"""Crea una tarea programada para ejecutar MediaSync-Daemon.py al inicio de sesión"""
try:
# Obtener rutas absolutas
script_dir = Path(__file__).parent.absolute()
script_path = script_dir / "MediaSync-Daemon.py"
# Verificar que el script existe
if not script_path.exists():
raise FileNotFoundError(f"No se encuentra el script: {script_path}")
# Obtener la ruta absoluta de python.exe
python_path = sys.executable
# Crear el comando PowerShell que se ejecutará
ps_command = f'''
$action = New-ScheduledTaskAction -Execute "{python_path}" -Argument '"{script_path}"'
$trigger = New-ScheduledTaskTrigger -AtLogOn
$settings = New-ScheduledTaskSettingsSet -StartWhenAvailable
Register-ScheduledTask -TaskName "MediaSync-Daemon.py" `
-Action $action `
-Trigger $trigger `
-Settings $settings `
-Description "Ejecuta VLC al iniciar sesión" `
-User "$env:USERNAME" `
-Force
Write-Output "Tarea programada creada con éxito."
'''
# Ejecutar PowerShell con privilegios elevados
result = subprocess.run([
"powershell.exe",
"-ExecutionPolicy", "Bypass",
"-Command", ps_command
], capture_output=True, text=True)
# Verificar resultado
if result.returncode == 0:
print("Tarea 'MediaSync-Daemon.py' creada exitosamente.")
else:
print(f"ERROR al crear la tarea: {result.stderr}")
except Exception as e:
print(f"ERROR: No se pudo crear la tarea programada: {str(e)}")
return False
return True
if __name__ == "__main__":
# Verificar si se está ejecutando con privilegios de administrador
if os.name == 'nt' and not ctypes.windll.shell32.IsUserAnAdmin():
print("Este script requiere privilegios de administrador.")
# Re-ejecutar el script con privilegios elevados
ctypes.windll.shell32.ShellExecuteW(
None, "runas", sys.executable, __file__, None, 1
)
else:
create_scheduled_task()
# Pausa para leer la salida
input("\nPresiona ENTER para salir...")