PowerShell of the line and column numbers when an exception is caught in the _powershell

Source: Internet
Author: User

If you write your own function dynamically to execute some script blocks or script files, it is useful to catch the exception line number and column number information. For example, script files:

Copy Code code as follows:

trap{
$info =$_. Invocationinfo
"In line {0}, the {1} column caught an exception: {2}"-F $info. Scriptlinenumber, $info. Offsetinline, $_. Exception.Message
Continue
}

Get-date
1gb/2kb
Get-service | foreach {1/0}

The results of the execution are:
Copy Code code as follows:

Thursday, June, 2014 4:41:41 PM
524288
In line 9th, column 15th catches the exception: attempted to divide by zero.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.