_original/Instalar-WorkloadManager.ps1

# ============================================================
# Instalar-WorkloadManager.ps1
# Autor: Dante / SafeAutentic
#
# Instalador del WorkloadManager
# Ejecutar UNA SOLA VEZ como Administrador
# Ambos archivos deben estar en la misma carpeta
# ============================================================

# Verificar que se ejecuta como administrador
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")) {
    Write-Host "ERROR: Ejecuta este script como Administrador." -ForegroundColor Red
    exit 1
}

$scriptDestino = "$env:ProgramData\WorkloadManager\WorkloadManager.ps1"
$carpeta       = Split-Path $scriptDestino

# 1. Crear carpeta destino
if (-not (Test-Path $carpeta)) {
    New-Item -ItemType Directory -Path $carpeta -Force | Out-Null
}

# 2. Copiar script principal
$origen = Join-Path $PSScriptRoot "WorkloadManager.ps1"
if (Test-Path $origen) {
    Copy-Item $origen $scriptDestino -Force
    Write-Host "Script copiado a $scriptDestino" -ForegroundColor Cyan
} else {
    Write-Host "ERROR: No se encontro WorkloadManager.ps1 en $PSScriptRoot" -ForegroundColor Red
    Write-Host "Asegurate de que ambos archivos esten en la misma carpeta." -ForegroundColor Yellow
    exit 1
}

# 3. Registrar fuente en Event Log
if (-not [System.Diagnostics.EventLog]::SourceExists("WorkloadManager")) {
    New-EventLog -LogName Application -Source "WorkloadManager" -ErrorAction SilentlyContinue
}

# 4. Limpiar tareas programadas anteriores
Unregister-ScheduledTask -TaskPath "\SafeAutentic\" -TaskName "WorkloadManager_Init" -Confirm:$false -ErrorAction SilentlyContinue
Unregister-ScheduledTask -TaskPath "\SafeAutentic\" -TaskName "WorkloadManager_Run" -Confirm:$false -ErrorAction SilentlyContinue

# 5. Limpiar entradas Run anteriores
Remove-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" -Name "WorkloadManager" -ErrorAction SilentlyContinue
Remove-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" -Name "WorkloadManager" -ErrorAction SilentlyContinue

# 6. Registrar inicio automatico en HKCU con auto-elevacion
$cmd = 'powershell -NonInteractive -WindowStyle Hidden -ExecutionPolicy Bypass -Command "Start-Process powershell -ArgumentList \"-NonInteractive -WindowStyle Hidden -ExecutionPolicy Bypass -File `\"C:\ProgramData\WorkloadManager\WorkloadManager.ps1`\"\" -Verb RunAs"'
Set-ItemProperty -Path "HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Run" -Name "WorkloadManager" -Value $cmd
Write-Host "Inicio automatico registrado en HKCU\Run" -ForegroundColor Cyan

# 7. Desactivar Recall via registro
$regPath = "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsAI"
if (-not (Test-Path $regPath)) { New-Item -Path $regPath -Force | Out-Null }
Set-ItemProperty -Path $regPath -Name "DisableAIDataAnalysis" -Value 1 -Type DWord -Force
Set-ItemProperty -Path $regPath -Name "DisableRecall"         -Value 1 -Type DWord -Force
Set-ItemProperty -Path $regPath -Name "AllowRecallEnablement" -Value 0 -Type DWord -Force

$svcRecall = Get-Service -Name "ScreenshotIndexerService" -ErrorAction SilentlyContinue
if ($svcRecall) {
    Stop-Service $svcRecall -Force -ErrorAction SilentlyContinue
    Set-Service  $svcRecall -StartupType Disabled -ErrorAction SilentlyContinue
}

Get-ScheduledTask -TaskPath "\Microsoft\Windows\WindowsAI\" -ErrorAction SilentlyContinue |
    Where-Object { $_.TaskName -match "Recall|Snapshot|ScreenshotIndex" } |
    Disable-ScheduledTask -ErrorAction SilentlyContinue

Write-Host "Recall desactivado via registro" -ForegroundColor Cyan

# 8. Lanzar el script ahora mismo sin esperar reinicio
Start-Process powershell -ArgumentList "-NonInteractive -WindowStyle Hidden -ExecutionPolicy Bypass -File `"$scriptDestino`"" -Verb RunAs

Write-Host ""
Write-Host "============================================" -ForegroundColor Green
Write-Host " Instalacion completada correctamente" -ForegroundColor Green
Write-Host "============================================" -ForegroundColor Green
Write-Host ""
Write-Host " Script activo ahora mismo en background" -ForegroundColor White
Write-Host " Se iniciara automaticamente con Windows" -ForegroundColor White
Write-Host " NOTA: Al iniciar Windows aparecera un prompt" -ForegroundColor Yellow
Write-Host " de UAC - haz clic en Si para continuar" -ForegroundColor Yellow
Write-Host " Recall desactivado" -ForegroundColor White
Write-Host ""
Write-Host " Para ver actividad en tiempo real:" -ForegroundColor Yellow
Write-Host " Get-EventLog -LogName Application -Source WorkloadManager -Newest 20 | Select-Object TimeGenerated, Message" -ForegroundColor Yellow
Write-Host ""