PowerShell (Task Scheduler)
किसी भी program के चारों ओर एक PowerShell wrapper: यह program के exit code से रिपोर्ट करता है। Program का output एक temporary फ़ाइल में रखा जाता है; failure पर उसकी आखिरी 20 lines alert के साथ जाती हैं।
Code
यह वह code है जो Code टैब बनाता है (start और errors पर निशान लगाकर)। App में इसमें पहले से आपके job का अपना पता होता है।
# Save as C:\jobs\taskbeacon-run.ps1
# Task Scheduler > Action: Start a program
# Program: powershell.exe
# Arguments: -NoProfile -ExecutionPolicy Bypass -File C:\jobs\taskbeacon-run.ps1 "C:\path\to\program.exe" "arg1" "arg2"
# Runs the program, then reports success or failure from its exit code.
# A .ps1 script as the program is run with powershell.exe -NoProfile -ExecutionPolicy Bypass -File script.ps1
# (an execution policy set by Group Policy still wins over Bypass).
$tb = "https://taskbeacon.io/ping/your-job-id"
$program, $programArgs = $args
# a .ps1 file is not a program Windows can start on its own ("not a valid Win32 application")
if ($program -match '\.ps1$') {
$programArgs = @('-NoProfile', '-ExecutionPolicy', 'Bypass', '-File', $program) + @($programArgs | Where-Object { $null -ne $_ })
$program = 'powershell.exe'
}
function Send-TaskBeacon($path, $text) {
$send = @{ UseBasicParsing = $true; TimeoutSec = 10; Uri = ($tb + $path) }
if ($text) { $send.Method = 'Post'; $send.ContentType = 'text/plain; charset=utf-8'; $send.Body = [Text.Encoding]::UTF8.GetBytes($text) }
try { Invoke-WebRequest @send | Out-Null }
catch { Write-Host "TaskBeacon ping failed: $_" }
}
Send-TaskBeacon "/start"
# keep arguments that contain spaces together
$argLine = ($programArgs | ForEach-Object { if ($_ -eq '' -or $_ -match '[\s"]') { '"' + ($_ -replace '"', '\"') + '"' } else { $_ } }) -join ' '
# Windows PowerShell 5.1 refuses an empty -ArgumentList: pass it only when there are arguments
$run = @{ FilePath = $program; Wait = $true; PassThru = $true; NoNewWindow = $true; ErrorAction = 'Stop' }
if ($argLine) { $run.ArgumentList = $argLine }
# the program's output goes to temporary files: shown below, and its last 20 lines go with a failure
$out = [IO.Path]::GetTempFileName(); $err = [IO.Path]::GetTempFileName()
$run.RedirectStandardOutput = $out; $run.RedirectStandardError = $err
$code = 1
$note = ''
try {
$p = Start-Process @run
if ($null -ne $p.ExitCode) { $code = $p.ExitCode }
} catch { $note = "Could not start ${program}: $_"; Write-Host $note }
Get-Content -LiteralPath $out, $err -ErrorAction SilentlyContinue
$tail = ''
if ($code -ne 0) {
$lines = @(Get-Content -LiteralPath $out, $err -Tail 20 -ErrorAction SilentlyContinue) + @($note)
$tail = ($lines | Where-Object { $_ } | ForEach-Object { if ($_.Length -gt 300) { $_.Substring(0, 300) } else { $_ } }) -join "`n"
}
# the exit code goes at the end of the address: /0 = OK, any other number = failed
Send-TaskBeacon "/$code" $tail
Remove-Item -LiteralPath $out, $err -ErrorAction SilentlyContinue
exit $code # not 0 when it failed, so Task Scheduler shows the failure tooयह कैसे काम करता है
Program शुरू होने पर wrapper /start ping करता है और खत्म होने पर वह address, जिसके अंत में उसका exit code होता है: /0 OK है, कोई और नंबर failure, जो output की आखिरी 20 lines के साथ भेजा जाता है। यह row count नहीं भेजता।
किसी हिस्से को छोड़ने के लिए Code टैब पर विकल्पों का निशान हटाएँ; कोई output न भेजकर सिर्फ़ error या exit code भेजने के लिए Send output lines (output लाइनें भेजें) का निशान हटाएँ। देखें क्या रिपोर्ट किया जा सकता है।
Command line से task बनाएँ
ऊपर की script को C:\jobs\taskbeacon-run.ps1 नाम से save करें, फिर एक task बनाएँ जो उसे आपके program के साथ चलाए (एक line, Command Prompt में):
schtasks /Create /TN "Nightly export" /SC DAILY /ST 02:00 /TR "powershell.exe -NoProfile -ExecutionPolicy Bypass -File C:\jobs\taskbeacon-run.ps1 C:\jobs\export.exe"
Wrapper program शुरू करता है, उसका इंतज़ार करता है, और पते में exit code के साथ result भेजता है (/0 यानी OK, /1 या कोई और संख्या यानी failure), fail होने पर output की आख़िरी 20 lines के साथ। यह उसी exit code के साथ ख़त्म होता है, इसलिए Task Scheduler का Last Run Result TaskBeacon से मेल खाता है।
Job के रूप में PowerShell script. Wrapper को program के रूप में .ps1 file दें; वह उसे ख़ुद powershell.exe -NoProfile -ExecutionPolicy Bypass -File script.ps1 से चलाता है (Windows अपने आप .ps1 शुरू नहीं कर सकता: %1 is not a valid Win32 application):
schtasks /Create /TN "Nightly cleanup" /SC DAILY /ST 03:00 /TR "powershell.exe -NoProfile -ExecutionPolicy Bypass -File C:\jobs\taskbeacon-run.ps1 C:\jobs\cleanup.ps1"
किसी मौजूदा task को wrap करने के लिए उसे Task Scheduler में खोलें → Actions → Edit: Program powershell.exe, Arguments वैसे जैसे script के ऊपर comment में हैं, अंत में आपके program का path और arguments।
Exit code झूठ क्यों बोलता है
Task Scheduler केवल उस program का exit code जानता है जिसे उसने शुरू किया। तीन चीज़ें उसे धोखा देती हैं:
- ऐसा wrapper जो उसे छिपा देता है।
.batfile अपने आख़िरी command के code के साथ ख़त्म होती है, इसलिए fail होने वाले program के बाद सफलcopyहोने पर0x0दिखता है। - ऐसा program जो हमेशा 0 लौटाता है। Batch mode में Stata और कई in-house tools error के बाद भी 0 के साथ बंद होते हैं। TaskBeacon पर उस language का अपना code इस्तेमाल करें (Stata, R, Python …), जो अंदर से error की सूचना देता है।
- कोई run हुआ ही नहीं। जब PC बंद हो, sleep में हो, या task का password expire हो गया हो, तो न कोई exit code होता है न कोई सूचना। TaskBeacon job को “Missed” (छूटा) दिखाता है।
Last Run Result के आम मान: 0x0 सफल, 0x1 program ने 1 लौटाया (अक्सर एक सामान्य error), 0x41301 task अभी चल रहा है, 0x41303 task अभी तक नहीं चला।
कोई संख्या भेजें
Wrapper केवल exit code देखता है। Row count भेजने के लिए अपने program की अपनी language वाला code इस्तेमाल करें, या program से ही ?rows= के साथ ping करें।
आम समस्याएँ
- हाथ से चलाने पर चलता है, schedule पर नहीं। Run whether user is logged on or not पर tick करें, और mapped drive letters की जगह UNC paths इस्तेमाल करें।
- "running scripts is disabled on this system". Arguments में
-ExecutionPolicy Bypassरहने दें; यह केवल इसी run पर लागू होता है। Group Policy से सेट execution policy इससे ऊपर रहती है:Get-ExecutionPolicy -Listसे जाँचें, और अगर MachinePolicy या UserPolicy सेट है, तो IT से पूछें। - "%1 is not a valid Win32 application". एक
.ps1को program के रूप में शुरू किया गया। Wrapper इस्तेमाल करें (वह .ps1 संभाल लेता है), या उसेpowershell.exe -NoProfile -ExecutionPolicy Bypass -File script.ps1के रूप में शुरू करें। - Spaces वाला path. उसे task के arguments में quotes में रखें:
"C:\Program Files\Tool\tool.exe"।
उपयोग के उदाहरण: Task Scheduler Last Run Result 0x0, लेकिन script fail हुई · Task Scheduler अब email नहीं भेज सकता: task fail होने पर alert पाएँ · PowerShell script errors और Task Scheduler में exit code। और सवाल: FAQ.