For debugging, to find out where your error is coming from, I suggest debugging using ISE or PowerGUI.
You can also get the transcript for you script using the Start-Transcript cmdlet, which will write all the console activity to a file. So you can put expressions like Write-Host "Doing XYZ" and they will appear in the protocol log.
If you catch exceptions with a try catch or use a trap, you can write the row number and the exception column as follows:
$ErrorActionPreference = 'Stop' trap { Write-Host "Error on line $($_.InvocationInfo.ScriptLineNumber)" exit 1 } try { Get-Item DoesntExist } catch { Write-Host ("Error occurred on line: " + $_.InvocationInfo.ScriptLineNumber) }
$_.InvocationInfo contains other information about whether an error occurred.
By setting $ErrorActionPreference = "Stop" , you will make sure that any error starts the trap{} block, which in this case writes the line where the script is located.
Andy arismendi
source share