This repository was archived by the owner on Jun 19, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcess-Queue.ps1
More file actions
72 lines (52 loc) · 1.58 KB
/
Copy pathProcess-Queue.ps1
File metadata and controls
72 lines (52 loc) · 1.58 KB
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
72
param(
[string]$QueueFile = "C:\Scripts\CommandQueue\CommandQueue.json"
)
$LockFile = [System.IO.Path]::ChangeExtension($QueueFile, ".lock")
# Prevent concurrent runs
if (Test-Path $LockFile) {
exit
}
New-Item $LockFile -ItemType File -Force | Out-Null
try {
if (!(Test-Path $QueueFile)) {
'{"jobs":[]}' | Out-File $QueueFile -Encoding utf8
}
$json = Get-Content $QueueFile -Raw
if ([string]::IsNullOrWhiteSpace($json)) {
$queue = [PSCustomObject]@{ jobs = @() }
}
else {
$queue = $json | ConvertFrom-Json
}
if ($queue -eq $null) {
$queue = [PSCustomObject]@{ jobs = @() }
}
$now = Get-Date
$remainingJobs = @()
foreach ($job in $queue.jobs) {
$runTime = [datetime]$job.RunTime
if ($runTime -le $now) {
try {
Write-Host "Running job $($job.Id)"
$encoded = [Convert]::ToBase64String(
[System.Text.Encoding]::Unicode.GetBytes($job.Command)
)
Start-Process powershell.exe `
-ArgumentList "-NoProfile -ExecutionPolicy Bypass -EncodedCommand $encoded" `
-WindowStyle Normal
}
catch {
Write-Host "Failed job $($job.Id): $_"
}
}
else {
$remainingJobs += $job
}
}
[PSCustomObject]@{ jobs = @($remainingJobs) } |
ConvertTo-Json -Depth 5 |
Out-File $QueueFile -Encoding utf8
}
finally {
Remove-Item $LockFile -Force -ErrorAction SilentlyContinue
}