PowerShell – Various Annoyances

Terminating errors vs. Non-terminating errors.
This will “fail” (a.k.a. worked as designed) and skip over the catch block because it is a non-terminating error.

try
{
Import-Module BogusModule
}
catch
{
Write-Warning "BogusModule could not be loaded."
Write-Output $Error[0]
}

This will work:

try
{
Import-Module BogusModule -ErrorAction 'Stop'
}
catch
{
Write-Warning "BogusModule could not be loaded."
Write-Output $Error[0]
}

Reference function:
http://stackoverflow.com/questions/10498433/how-to-correctly-ignore-import-module-errors-in-powershell

Annoyance:
PowerShell deciding how it will format (“rolling” and “unrolling” objects) an item on output.
An intermediary function for CSV as an example:
http://learn-powershell.net/2014/01/24/avoiding-system-object-or-similar-output-when-using-export-csv/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s