PowerShell script errors और Task Scheduler में exit code
आपकी PowerShell script लाल errors print करती है, पर Task Scheduler 0x0 दिखाता है; या script ने जो भी लौटाया हो, हर failure 0x1 के रूप में दिखता है। दोनों इस बात से आते हैं कि powershell.exe script के errors को exit code में कैसे बदलता है। नियम जान लेने के बाद असली नतीजा report करना आसान है।
PowerShell exit code कैसे तय करता है
powershell.exe (Windows PowerShell 5.1) और pwsh (PowerShell 7) के लिए Microsoft का documentation script शुरू करने के दो तरीके बताता है, और दोनों अलग तरह से काम करते हैं:
-File script.ps1: exit code वह संख्या है जो आपexitको देते हैं। अगर script सामान्य रूप से ख़त्म होती है, तो यह 0 है। अगर कोई terminating error (throw, या-ErrorAction Stopके साथ चलाया गया cmdlet) पकड़ा नहीं जाता, तो यह 1 है।-Command "…": exit code 0 या 1 होता है, इस आधार पर कि आख़िरी command सफल हुई या नहीं ($?), जब तक आपexitcall न करें। कोई भी दूसरा code, उदाहरण के लिए आपके चलाए program का 3, दबकर 1 बन जाता है। उसे बचाए रखने के लिए Microsoft अंत मेंexit $LASTEXITCODEलिखने का सुझाव देता है।
जाल तीसरी तरह का error है। ज़्यादातर cmdlet failures non-terminating होती हैं: गायब file पर Copy-Item, ग़लत query के साथ Invoke-Sqlcmd या Write-Error एक लाल message print करते हैं और script आगे चलती रहती है। -File के साथ, ऐसे दस errors वाली script भी 0 के साथ exit करती है, और Task Scheduler 0x0 दिखाता है।
Native programs एक और नियम जोड़ते हैं: जब आपकी script कोई .exe चलाती है, तो उसका exit code $LASTEXITCODE में होता है, और fail होने वाला program script को तब तक नहीं रोकता जब तक आप उसे जाँचें नहीं।
Script को ठीक से fail करवाइए
Script के शुरू में हर error को terminating बना दीजिए, और native programs को साफ़ तौर पर जाँचिए:
$ErrorActionPreference = 'Stop' # every cmdlet error now stops the script (exit code 1 with -File)
& C:\tools\export.exe --day today
if ($LASTEXITCODE -ne 0) { exit $LASTEXITCODE }
इसे -Command से नहीं, -File से शुरू कीजिए, ताकि exit को दिया गया code बचा रहे।
नतीजा TaskBeacon को report कीजिए
अब exit code ईमानदार है, पर किसी को फिर भी Task Scheduler देखना पड़ता है। नीचे का wrapper कोई भी program चलाता है, powershell.exe -File your-script.ps1 सहित, और उसका exit code TaskBeacon को भेजता है: /0 OK है, बाकी कुछ भी failure, output की आख़िरी 20 lines के साथ।
# 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 tooApp में, आपके job का Code tab आपको यही code देता है, your-job-id की जगह job के अपने address के साथ, और जिन हिस्सों की ज़रूरत नहीं उनका tick आप हटा सकते हैं।
PowerShell script के लिए task के arguments ये बनते हैं: -NoProfile -ExecutionPolicy Bypass -File C:\jobs\taskbeacon-run.ps1 powershell.exe -NoProfile -File C:\jobs\nightly.ps1। Wrapper उसी exit code के साथ ख़त्म होता है, इसलिए Task Scheduler और TaskBeacon एक ही बात कहते हैं।
कुछ scripts बदली नहीं जा सकतीं और 0 के साथ exit करते हुए errors print करती हैं। उनके लिए finishing ping के साथ output भेजिए और एक output rule जोड़िए, जैसे Fail if the output contains (अगर output में हो तो Fail) ERROR, Exception; देखें Output में शब्द।
आपको क्या मिलता है
- Failed (विफल) exit code और output के अंत के साथ, email, Slack, Teams या webhook से।
- Missed (छूटा) जब task तय समय और grace तक नहीं चला, जो कोई exit code आपको नहीं बता सकता।
- Late (देर से) जब script अपनी सामान्य अवधि के काफ़ी बाद भी चल रही हो, उदाहरण के लिए
Read-Hostजैसे prompt या ऐसी confirmation पर अटकी हो जिसका कोई जवाब नहीं दे सकता।
TaskBeacon के बिना
ऊपर की तरह $ErrorActionPreference = 'Stop', -File और साफ़ exit codes इस्तेमाल कीजिए, body को try { … } catch { …; exit 1 } में लपेटिए, और catch block से किसी mail library या Teams Workflows webhook के ज़रिए ख़ुद को message भेजिए। इससे script के अंदर के errors पकड़े जाते हैं। जो runs कभी शुरू नहीं होते, और जो scripts अटक जाती हैं, उनके लिए फिर भी machine के बाहर कुछ चाहिए जो signal की उम्मीद करे; देखें ऐसा scheduled job जो नहीं चला।
क्या आपने Claude या ChatGPT को TaskBeacon से जोड़ा है? उससे पूछिए "कल रात क्या fail हुआ, और क्यों?" और वह आपके jobs के आधार पर जवाब देगा। यह कैसे काम करता है।
संबंधित
डॉक्स में: PowerShell (Task Scheduler) · Ping में exit code · Output में शब्द।
- Task Scheduler Last Run Result 0x0, लेकिन script fail हुईLast Run Result 0x0 का मतलब सिर्फ़ इतना है कि जिस program को चलाया गया उसने 0 लौटाया।
- Task Scheduler अब email नहीं भेज सकता: task fail होने पर alert पाएँBuilt-in e-mail action हट चुका है; event से trigger होने वाले tasks उन runs को नहीं पकड़ते जो कभी हुए ही नहीं।
- Database Mail के बिना SQL Server Agent job failure alertsAgent के emails के लिए Database Mail, एक operator और restart चाहिए, और जो job चला ही नहीं उसके बारे में वे कुछ नहीं बताते।
TaskBeacon से शुरुआत मुफ़्त है; अभी हर feature सबके लिए खुला है और credit card की ज़रूरत नहीं। एक job बनाइए, code paste कीजिए, और पहला ping job के page पर दिख जाएगा।
मुफ़्त शुरू करें लाइव demo देखेंस्रोत
- Microsoft Learn: about_PowerShell_exe (Windows PowerShell 5.1, -File और -Command के exit codes) (2024-07-23)
- Microsoft Learn: about_Pwsh (PowerShell 7.5) (2026-01-18)
- Microsoft Learn: about_Automatic_Variables ($LASTEXITCODE) (2026-04-02)
- Microsoft Q&A: Getting PowerShell return code 2147942401 (27 दिसंबर 2022, पुराना: माँग का संकेत)
इस page को इसके स्रोतों से 9 नवंबर 2026 को मिलाकर जाँचा गया। दूसरे products के menu और limits बदलते रहते हैं; अगर यहाँ कुछ अब मेल नहीं खाता, तो support@taskbeacon.io पर लिखें।