How can I throw my own error AND catch its location in a script?
i have large script in using try/catch/finally. in catch block have following:
catch { write-output "$(wts):" write-output "$(wts): --------------------------------------------------" write-output "$(wts): -- script processing cancelled" write-output "$(wts): --------------------------------------------------" write-output "$(wts):" write-output "$(wts): error in $($_.invocationinfo.scriptname)." write-output "$(wts):" write-output "$(wts): --------------------------------------------------" write-output "$(wts): -- error information" write-output "$(wts): --------------------------------------------------" write-output "$(wts):" write-output "$(wts): line number: $($_.invocationinfo.scriptlinenumber)" write-output "$(wts): offset: $($_.invocationinfo.offsetinline)" write-output "$(wts): command: $($_.invocationinfo.mycommand)" write-output "$(wts): line: $($_.invocationinfo.line)" write-output "$(wts): error details: $($_)" write-output "$(wts):" write-output "$(wts): --------------------------------------------------" write-output "$(wts): -- error information" write-output "$(wts): --------------------------------------------------" write-output "$(wts):" } within body of script tried using write-error when encountered explicit error wanted handle. however, when pass write-error cmdlet error referencing location of script, not, line within script:
04/13/2012 08:39:00: -------------------------------------------------- 04/13/2012 08:39:00: -- script processing cancelled 04/13/2012 08:39:00: -------------------------------------------------- 04/13/2012 08:39:00: 04/13/2012 08:39:00: error in . 04/13/2012 08:39:00: 04/13/2012 08:39:00: -------------------------------------------------- 04/13/2012 08:39:00: -- error information 04/13/2012 08:39:00: -------------------------------------------------- 04/13/2012 08:39:00: 04/13/2012 08:39:00: line number: 1 04/13/2012 08:39:00: offset: 2 04/13/2012 08:39:00: command: script.ps1 04/13/2012 08:39:00: line: . .\script.ps1 -environment dev 04/13/2012 08:39:00: error details: script permissions website (somewebsite) not set. cancelling. 04/13/2012 08:39:00: 04/13/2012 08:39:00: -------------------------------------------------- 04/13/2012 08:39:00: -- error information 04/13/2012 08:39:00: --------------------------------------------------
this should indicate command (like get-webconfiguration property), line (line: 243) , offset (99). it seems using write-error adds new error , breaks out of invocationinfo object scoped script. instead, breaks, changes scope shell , returns information on command used run script. tried using $error.add("my comment.") had same result. using throw "my comment." produced same result well.
if there powershell solution, i'd know is. in case, i've used same technique used in vb, maintain "step" numbers in code. in powershell, increment variable $step periodically, after each logical segment of code (or after statements possibly raise error). in catch block (or trap block) i can log value of $step. requires more lines of code, , more work if code revised, but it helps to troubleshoot long scripts.
richard mueller - mvp directory services
Windows Server > Windows PowerShell
Comments
Post a Comment