# Install-AssetCollectionSchedule.ps1 # Creates a Windows scheduled task to run asset collection 4 times daily in the background param( [string]$ScriptPath = "S:\DT\adata\script\Update-PC-CompleteAsset-Silent.bat", [string]$TaskName = "GE Asset Collection", [string]$TaskDescription = "Automated PC asset collection for GE shopfloor systems - runs silently 4 times daily" ) Write-Host "===================================== " -ForegroundColor Cyan Write-Host "GE Asset Collection - Schedule Installer" -ForegroundColor Cyan Write-Host "===================================== " -ForegroundColor Cyan Write-Host "" # Check if running as administrator if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")) { Write-Host "[ERROR] This script must be run as Administrator to create scheduled tasks" -ForegroundColor Red Write-Host "Please right-click and 'Run as Administrator'" -ForegroundColor Yellow exit 1 } # Verify the script file exists if (-not (Test-Path $ScriptPath)) { Write-Host "[ERROR] Asset collection script not found at: $ScriptPath" -ForegroundColor Red Write-Host "Please ensure the script is deployed to the correct location" -ForegroundColor Yellow exit 1 } Write-Host "Installing scheduled task for automated asset collection..." -ForegroundColor Green Write-Host " Script Path: $ScriptPath" -ForegroundColor Gray Write-Host " Task Name: $TaskName" -ForegroundColor Gray Write-Host " Frequency: 4 times daily (every 6 hours)" -ForegroundColor Gray Write-Host "" try { # Remove existing task if it exists $existingTask = Get-ScheduledTask -TaskName $TaskName -ErrorAction SilentlyContinue if ($existingTask) { Write-Host "Removing existing scheduled task..." -ForegroundColor Yellow Unregister-ScheduledTask -TaskName $TaskName -Confirm:$false } # Define the action - run the batch file completely hidden $action = New-ScheduledTaskAction -Execute "cmd.exe" -Argument "/c `"$ScriptPath`"" -WorkingDirectory "S:\DT\adata\script" # Define multiple triggers for 4 times daily (6:00 AM, 12:00 PM, 6:00 PM, 12:00 AM) $trigger1 = New-ScheduledTaskTrigger -Daily -At "06:00" $trigger2 = New-ScheduledTaskTrigger -Daily -At "12:00" $trigger3 = New-ScheduledTaskTrigger -Daily -At "18:00" $trigger4 = New-ScheduledTaskTrigger -Daily -At "00:00" # Define settings - run in background, don't disturb users $settings = New-ScheduledTaskSettingsSet ` -AllowStartIfOnBatteries ` -DontStopIfGoingOnBatteries ` -StartWhenAvailable ` -RunOnlyIfNetworkAvailable ` -DontStopOnIdleEnd ` -Hidden ` -Priority 4 ` -ExecutionTimeLimit (New-TimeSpan -Hours 2) ` -RestartCount 3 ` -RestartInterval (New-TimeSpan -Minutes 10) # Run as SYSTEM account for maximum permissions and no user interaction $principal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount -RunLevel Highest # Create the scheduled task $task = New-ScheduledTask -Action $action -Trigger @($trigger1, $trigger2, $trigger3, $trigger4) -Settings $settings -Principal $principal -Description $TaskDescription # Register the task Register-ScheduledTask -TaskName $TaskName -InputObject $task -Force Write-Host "[SUCCESS] Scheduled task created successfully!" -ForegroundColor Green Write-Host "" Write-Host "Task Details:" -ForegroundColor Cyan Write-Host " Name: $TaskName" -ForegroundColor Gray Write-Host " Schedule: 4 times daily at 6:00 AM, 12:00 PM, 6:00 PM, 12:00 AM" -ForegroundColor Gray Write-Host " Account: SYSTEM (runs in background)" -ForegroundColor Gray Write-Host " Hidden: Yes (no user interruption)" -ForegroundColor Gray Write-Host " Network Required: Yes" -ForegroundColor Gray Write-Host " Max Runtime: 2 hours" -ForegroundColor Gray Write-Host " Auto Restart: 3 attempts if failed" -ForegroundColor Gray Write-Host "" # Test the task by running it once Write-Host "Testing scheduled task..." -ForegroundColor Yellow Start-ScheduledTask -TaskName $TaskName Start-Sleep -Seconds 3 $taskInfo = Get-ScheduledTaskInfo -TaskName $TaskName Write-Host " Last Run: $($taskInfo.LastRunTime)" -ForegroundColor Gray Write-Host " Last Result: $($taskInfo.LastTaskResult)" -ForegroundColor Gray Write-Host " Next Run: $($taskInfo.NextRunTime)" -ForegroundColor Gray Write-Host "" Write-Host "[INFO] Task installation complete!" -ForegroundColor Green Write-Host "The asset collection will now run automatically 4 times daily in the background." -ForegroundColor Green Write-Host "Users will not see any windows or be interrupted during execution." -ForegroundColor Green } catch { Write-Host "[ERROR] Failed to create scheduled task: $($_.Exception.Message)" -ForegroundColor Red Write-Host "Please check Windows Event Viewer for additional details" -ForegroundColor Yellow exit 1 } Write-Host "" Write-Host "===================================== " -ForegroundColor Cyan Write-Host "Installation Complete" -ForegroundColor Cyan Write-Host "===================================== " -ForegroundColor Cyan