- # Install-SmartSleep.ps1 - Run as Administrator
- # Create Scripts directory
- $scriptPath = "C:\Scripts"
- if (-not (Test-Path $scriptPath)) {
- New-Item -ItemType Directory -Path $scriptPath -Force
- Write-Host "Created directory: $scriptPath" -ForegroundColor Green
- }
- # Check if main script exists
- $mainScript = "$scriptPath\SmartSleep.ps1"
- if (-not (Test-Path $mainScript)) {
- Write-Host "ERROR: Please save SmartSleep.ps1 to C:\Scripts\ first!" -ForegroundColor Red
- exit 1
- }
- # Set execution policy for scripts
- try {
- Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope LocalMachine -Force
- Write-Host "Set execution policy to RemoteSigned" -ForegroundColor Green
- }
- catch {
- Write-Host "Warning: Could not set execution policy. Run as Administrator." -ForegroundColor Yellow
- }
- # Remove existing task if present
- $existingTask = Get-ScheduledTask -TaskName "SmartSleepMonitor" -ErrorAction SilentlyContinue
- if ($existingTask) {
- Unregister-ScheduledTask -TaskName "SmartSleepMonitor" -Confirm:$false
- Write-Host "Removed existing scheduled task" -ForegroundColor Yellow
- }
- # Create scheduled task
- $taskAction = New-ScheduledTaskAction -Execute "powershell.exe" `
- -Argument "-WindowStyle Hidden -NoProfile -ExecutionPolicy Bypass -File `"$mainScript`""
- $taskTrigger = New-ScheduledTaskTrigger -AtStartup
- $taskSettings = New-ScheduledTaskSettingsSet `
- -AllowStartIfOnBatteries `
- -DontStopIfGoingOnBatteries `
- -StartWhenAvailable `
- -RestartCount 3 `
- -RestartInterval (New-TimeSpan -Minutes 1) `
- -ExecutionTimeLimit (New-TimeSpan -Hours 0)
- $taskPrincipal = New-ScheduledTaskPrincipal `
- -UserId "SYSTEM" `
- -LogonType ServiceAccount `
- -RunLevel Highest
- $task = New-ScheduledTask `
- -Action $taskAction `
- -Trigger $taskTrigger `
- -Settings $taskSettings `
- -Principal $taskPrincipal `
- -Description "Monitors system idle time and forces sleep when games are idle but not during media playback"
- # Register the task
- Register-ScheduledTask `
- -TaskName "SmartSleepMonitor" `
- -InputObject $task `
- -Force
- Write-Host "`nScheduled task created successfully!" -ForegroundColor Green
- # Start the task immediately
- Start-ScheduledTask -TaskName "SmartSleepMonitor"
- Write-Host "Smart Sleep Monitor is now running" -ForegroundColor Green
- # Create desktop shortcut for logs
- $WshShell = New-Object -comObject WScript.Shell
- $Shortcut = $WshShell.CreateShortcut("$env:USERPROFILE\Desktop\SmartSleep Log.lnk")
- $Shortcut.TargetPath = "notepad.exe"
- $Shortcut.Arguments = "$scriptPath\SmartSleep.log"
- $Shortcut.Save()
- Write-Host "`nCreated desktop shortcut to view logs" -ForegroundColor Cyan
- # Display status
- Write-Host "`n=== Installation Complete ===" -ForegroundColor Green
- Write-Host "The Smart Sleep Monitor is now:"
- Write-Host " ✓ Installed as a system service" -ForegroundColor Green
- Write-Host " ✓ Running in the background" -ForegroundColor Green
- Write-Host " ✓ Will start automatically on boot" -ForegroundColor Green
- Write-Host "`nLog file: $scriptPath\SmartSleep.log"
- Write-Host "To uninstall, run: Unregister-ScheduledTask -TaskName 'SmartSleepMonitor'" -ForegroundColor Yellow
