104 lines
3.8 KiB
PowerShell
104 lines
3.8 KiB
PowerShell
#Requires -Version 3.0
|
|
#Requires -RunAsAdministrator
|
|
<#
|
|
.SYNOPSIS
|
|
Creates a scheduled task to run the user activity tracking script at logon.
|
|
.DESCRIPTION
|
|
This script creates a Windows Task Scheduler task that launches the
|
|
report.ps1 script whenever a user logs into the system. It must be
|
|
run with administrative privileges.
|
|
.NOTES
|
|
File Name : schedule_task.ps1
|
|
Author : IT Department
|
|
Version : 1.4
|
|
.EXAMPLE
|
|
.\schedule_task.ps1 -ScriptPath "C:\Scripts\report.ps1"
|
|
|
|
# To avoid security warnings, you can unblock the files first:
|
|
# Unblock-File -Path schedule_task.ps1
|
|
# Unblock-File -Path report.ps1
|
|
#>
|
|
|
|
param (
|
|
[Parameter(Mandatory=$true)]
|
|
[string]$ScriptPath
|
|
)
|
|
|
|
# Begin by unblocking the files to prevent security warnings
|
|
Write-Host "Attempting to unblock script files..."
|
|
try {
|
|
Unblock-File -Path $ScriptPath -ErrorAction SilentlyContinue
|
|
Unblock-File -Path $MyInvocation.MyCommand.Path -ErrorAction SilentlyContinue
|
|
Write-Host "Files unblocked successfully."
|
|
} catch {
|
|
Write-Host "Unable to unblock files. If you see security warnings, you may need to run the following manually:"
|
|
Write-Host "Unblock-File -Path $ScriptPath"
|
|
}
|
|
|
|
# Verify the script exists
|
|
if (-not (Test-Path $ScriptPath)) {
|
|
Write-Error "Script not found at path: $ScriptPath"
|
|
exit 1
|
|
}
|
|
|
|
# Make sure the path is absolute
|
|
$ScriptPath = (Resolve-Path $ScriptPath).Path
|
|
|
|
# Task settings
|
|
$taskName = "UserActivityTracking"
|
|
$taskDescription = "Monitors user activity and reports to central server"
|
|
|
|
# Check if the task already exists and remove it
|
|
$existingTask = Get-ScheduledTask -TaskName $taskName -ErrorAction SilentlyContinue
|
|
if ($existingTask) {
|
|
Write-Host "Task '$taskName' already exists. Removing it..."
|
|
schtasks /Delete /TN $taskName /F
|
|
}
|
|
|
|
# Create the PowerShell command to be executed
|
|
$powershellCmd = "-NoProfile -ExecutionPolicy Bypass -WindowStyle Hidden -File `"$ScriptPath`""
|
|
|
|
# Use schtasks.exe command to create the task (more reliable than PowerShell cmdlets)
|
|
try {
|
|
$createCmd = "schtasks /Create /TN $taskName /TR `"powershell.exe $powershellCmd`" /SC ONLOGON /F /RU INTERACTIVE /IT /DELAY 0000:30"
|
|
Write-Host "Creating scheduled task with command: $createCmd"
|
|
Invoke-Expression $createCmd
|
|
|
|
# Verify task was created
|
|
$taskExists = schtasks /Query /TN $taskName 2>$null
|
|
if ($LASTEXITCODE -eq 0) {
|
|
Write-Host "Task '$taskName' has been created successfully."
|
|
Write-Host "The activity tracking script will now run at each user logon."
|
|
Write-Host "Script path: $ScriptPath"
|
|
} else {
|
|
Write-Error "Task creation verification failed. The task may not have been created properly."
|
|
}
|
|
} catch {
|
|
Write-Error "Failed to create scheduled task: $_"
|
|
exit 1
|
|
}
|
|
|
|
# Copy config.env next to script file if it exists
|
|
$scriptDir = Split-Path -Parent $ScriptPath
|
|
$configEnvSource = Join-Path (Get-Location) "config.env"
|
|
$configEnvDest = Join-Path $scriptDir "config.env"
|
|
|
|
if (Test-Path $configEnvSource) {
|
|
# Check if source and destination are different
|
|
if ((Resolve-Path $configEnvSource).Path -ne (Resolve-Path $configEnvDest -ErrorAction SilentlyContinue).Path) {
|
|
try {
|
|
Copy-Item -Path $configEnvSource -Destination $configEnvDest -Force
|
|
Write-Host "Copied config.env to script directory: $configEnvDest"
|
|
}
|
|
catch {
|
|
Write-Warning "Failed to copy config.env to script directory: $_"
|
|
}
|
|
} else {
|
|
Write-Host "Config.env is already in the script directory."
|
|
}
|
|
}
|
|
else {
|
|
Write-Warning "config.env not found in current directory. Make sure to manually configure the script."
|
|
}
|
|
|
|
Write-Host "`nIMPORTANT: To manually verify the task was created correctly, open Task Scheduler`n(taskschd.msc) and check for the 'UserActivityTracking' task." |