-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreateElevatedTasks.ps1
71 lines (60 loc) · 2.64 KB
/
createElevatedTasks.ps1
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
68
69
70
71
Import-Module ".\functions.psm1"
CheckPwshInstallation
# Check for administrative privileges
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "Requesting administrative privileges..."
Start-Process -Verb RunAs -FilePath 'pwsh' -ArgumentList "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`""
exit
}
Write-Host "============================================================================"
Write-Host "= This script will remove all 'elevated-' Scheduled tasks! ="
Write-Host "============================================================================"
Write-Host "Checking for 'elevated-' tasks..."
$tasksDir = ".\conf\task-scheduler"
# Get all XML files in the current directory that start with 'elevated-'
$xmlFiles = Get-ChildItem -Path $tasksDir -Filter 'elevated-*.xml'
# Get existing scheduled tasks
$existingTasks = Get-ScheduledTask | Where-Object { $_.TaskName -like 'elevated-*' }
# Get existing task base names
$existingBaseNames = $existingTasks.TaskName
# Check if all tasks already exist
$allTasksExist = $true
foreach ($file in $xmlFiles) {
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
if ($existingBaseNames -notcontains $baseName) {
$allTasksExist = $false
break
}
}
function availableTask {
if ($existingBaseNames.Count -gt 0) {
Write-Host "Existing tasks:"
foreach ($task in $existingBaseNames) {
Write-Host $task
}
}
}
$xmlFilesCurrated = Get-ChildItem -Path $tasksDir -Filter 'elevated-*.xml' | ForEach-Object { $_.BaseName -replace '^elevated-' }
$expectedCount = $xmlFilesCurrated.Count
$expectedFiles = $xmlFilesCurrated -join ', '
Write-Host "$expectedCount XML found: $expectedFiles"
availableTask
Write-Host "All tasks exist? $allTasksExist"
if ($allTasksExist) {
Write-Host "All scheduled tasks already exist" -ForegroundColor Yellow
} else {
# Create tasks for those that don't exist
foreach ($file in $xmlFiles) {
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($file.Name)
if ($existingBaseNames -notcontains $baseName) {
Write-Host "Creating scheduled task for $baseName" -ForegroundColor Blue
schtasks.exe /CREATE /XML $file.FullName /TN "$baseName" > $Null
if ($LASTEXITCODE -eq 0) {
Write-Host "$baseName task created!" -ForegroundColor Green
} else {
Write-Host "Failed to create scheduled task for $baseName" -ForegroundColor Red
}
}
}
}
pause