Is there a canned way to log the current line in Powershell with a Write-Host? -


i remember c++ macros output current line number. same code, preprocessor replace macro current line. want in powershell can tell meant when tfs 2015 tells me there error on line 6, line 6 comment. see , powershell "thinks" line 6 differ.

first, define following aliases:

function get-scriptlinenumber { return $myinvocation.scriptlinenumber } function get-scriptname { return $myinvocation.scriptname }  new-item alias:__line__ -value get-scriptlinenumber new-item alias:__file__ -value get-scriptname  

to test them, save code below in script file (let's call "test.ps1") , call powershell prompt:

function hello                                                              #1 {                                                                           #2     param (                                                                 #3             [parameter(mandatory = $true)] [string] $receiver               #4     )                                                                       #5                                                                             #6     $i = 0                                                                  #7                                                                             #8     # hello                                                             #9     # comment                                                       #10     # , yet 1                                                   #11                                                                             #12     write-host "line:" $(__line__) "file:" $(__file__) "hello $receiver"    #13 }                                                                           #14  hello "world" 

when you'll run it, you'll following result:

line: 13 file: d:\temp\test.ps1 hello world 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -