Errors occur. Things fail. As an author of a script, you need to be able to deal with these hiccups.
What Do I Need To Watch For?
PowerShell has different types of errors (I explain some of that here.) Terminating errors can ruin a script’s day. Say you are trying to gather information or update settings to a large number of servers. What happens when you hit an error on server number three, or number 33, or number 333? You’ll know you hit an error on that item,
- What about the remaining servers?
- Do you need to re-run the script?
- What about the servers it completed on? Can you run it there again?
It Depends
How much any of this matters really depends on your script and what you are trying to accomplish, but you really do need to take errors into account, especially if you are handing your script off to someone else.
What can I do?
Take advantage of the error handling syntax that PowerShell provides. Wrap potentially troublesome code in a try/catch or try/catch/finally block (check out the help for about_Try_Catch_Finally). It can be as simple as
param ( [parameter()] [string[]] $ComputerName )
process { if ($ComputerName.count -gt 0) { try { Get-Process -ComputerName $ComputerName -Name powershell } catch { Write-Error “Error trying to connect and retrieve process from $ComputerName” } } }