PowerShell中捕獲異常時(shí)的行號(hào)和列號(hào)的方法

字號(hào):


    這篇文章主要介紹了PowerShell中捕獲異常時(shí)的行號(hào)和列號(hào)的方法,使用$_.InvocationInfo變量實(shí)現(xiàn)獲取,需要的朋友可以參考下。
    如果自己寫函數(shù)動(dòng)態(tài)地去執(zhí)行一些腳本塊或者腳本文件,那么捕獲到異常時(shí)行號(hào)和列號(hào)信息非常有用。例如腳本文件:
    代碼如下:
    trap{
    $info=$_.InvocationInfo
    "在第{0}行,第{1}列捕獲到異常:{2}"-f$info.ScriptLineNumber,$info.OffsetInLine,$_.Exception.Message
    continue
    }
    get-date
    1gb/2kb
    Get-Service|foreach{1/0}
    執(zhí)行結(jié)果為:
    代碼如下:
    Thursday,June26,20144:41:41PM
    524288
    在第9行,第15列捕獲到異常:Attemptedtodividebyzero.