3

My Powershell script is throwing an exception, but not giving me the line number where the exception is thrown:

The process cannot access the file 'C:/path/to/foo.txt' because it is being used by another process.
+ CategoryInfo          : NotSpecified: (:) [Set-Content], IOException
+ FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.SetContentCommand
+ PSComputerName        : localhost

I would think the best practice here is to refactor the script to use try/catch blocks to narrow down exactly where the Set-Content exception is being thrown - but in this case, due to the size/scope of the script, I am trying to avoid that option.

Instead, I'm hoping for a bit of a top-down solution - for instance, being able to call the script with added verbosity, in such a way that Powershell prints the line numbers in the stack traces (pretty sure Python does this by default).

Does Powershell 3.0 natively support this?

EDIT: The accepted answer to this question involves the try/catch approach, which I have mentioned is not my preferred approach. Again, is there some way to get PowerShell to report line numbers when exceptions are raised without mandatory try/catches? Would I have to wrap the entire script in a generic try/catch? Would I have to invoke the script with some --verbose flag? Does Powershell 3.0 support anything like that?

alex
  • 6,818
  • 9
  • 52
  • 103
  • Could you add the script causing the error? – spicy.dll Jun 13 '19 at 21:14
  • 1
    There should be `At D:\PShell\SO\56588150.ps1:21 char:1` line (or alike). You can check the property `$Error[0].ScriptStackTrace`. – JosefZ Jun 13 '19 at 22:15
  • Alternatively, `$Error[0].InvocationInfo.ScriptLineNumber` – Drew Jun 13 '19 at 23:26
  • Possible duplicate of [How to get the line number of error in PowerShell](https://stackoverflow.com/questions/17226718/how-to-get-the-line-number-of-error-in-powershell) – Moerwald Jun 14 '19 at 04:51
  • @Moerwald This was not a duplicate of that question - the accepted answer involves the try/catch block approach (`$_.Exception` is called in the context of a catch block). – alex Jun 14 '19 at 12:50
  • @JosefZ Nope - I included the full exception. – alex Jun 14 '19 at 12:52

1 Answers1

3

Set-PsDebug is going to print every command one the console, while a script is running.

Set-PSDebug -Trace 2; foreach ($i in 1..3) {$i}
DEBUG:    1+ Set-PsDebug -Trace 2; foreach ($i in 1..3) {$i}
DEBUG:    1+ Set-PsDebug -Trace 2; foreach ($i in 1..3) {$i}
1
DEBUG:    1+ Set-PsDebug -Trace 2; foreach ($i in 1..3) {$i}
2
DEBUG:    1+ Set-PsDebug -Trace 2; foreach ($i in 1..3) {$i}
3
JosefZ
  • 28,460
  • 5
  • 44
  • 83
Moerwald
  • 10,448
  • 9
  • 43
  • 83
  • Thanks Moerwald - this is exactly the kind of thing what I'm looking for. For the sake of those would find [the official docs](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/set-psdebug?view=powershell-3.0) bewildering - to use this, you would just throw `Set-PSDebug -Trace 1` somewhere in the context of the script itself. This will show every line executed, along with a message like `DEBUG: 1739+` - very ugly but it works. – alex Jun 14 '19 at 13:40