Creating a PowerShell Function to display error messages

Share this:

When writing your code using PowerShell, and based on the complexity of the script we need to create output to troubleshoot/debug the code.

We can always write-host “xxx” and see what is going on during the execution of the script, also the PowerShell ISE we can debug from the editor using F9 (Toggle Breakpoint) as well.

Solution


In this Tutorial, we will create a function that display the information based on the severity, we just need to pass the text and the severity where 0=normal, 1=warning, and 2=critical.

We control the display of the messages using a Global variable called $Global:vDebug, if we turn that to off, we automatically silence any message using the function.

Here is the Function, global variable and a basic example

$Global:vDebug = $True

Function APDebug ($param_ErrorMessage,$param_type)
{
     Switch ($param_type)
     {
         0 {$vColor = “Cyan”; $vPreText = “-> “}
         1 {$vColor = “Yellow”; $vPreText = “.:. WARNING –> “}
         2 {$vcolor = “red”; $vPreText = “.:. ERROR –> “}
     }

    If ($Global:vDebug)
     {
         Write-Host
         Write-Host -Foregroundcolor $vColor $vPretext $param_ErrorMessage
         Write-Host
     }
     return;
}

APDebug “Warning message” 1

We can test running the following command lines to see the results.

image

If we change the vDebug variable to false, automatically we don’t see output for the function. Which can be helpful during troubleshooting where you want to place your debug messages across the code and before deleting your code, we can disable the debug, see if everything is fine.

Using a function also helps when releasing that code to production, we just need to search and remove the lines used by the function, instead of evaluating write-host lines to see if they are debug or actual output from the script.

image

Written by Anderson Patricio

Anderson Patricio

Anderson Patricio is a Canadian MVP in Cloud and Datacenter Management, and Office Server and Services, besides the Microsoft Award he also holds a Solutions Master (MCSM) in Exchange and several other certifications. Anderson has been contributing to the Microsoft Community with articles, tutorials, blog posts, twitter, forums and book reviews. He is a regular contributor here at ITPROCentral.com, MSExchange.org, Techgenix.com and Anderson Patricio.org (Portuguese).

Related Post

Managing features at Network Adapter level using P... Using PowerShell to manage features at network adapter level, the tasks that we are going over on this Tutorial may be required when managing a larger...
How to remove the several Organization Units that ... In some situation an entire structure of Organization Units must be deleted, and by default new OUs are automatic defined to protect them against dele...
Getting the number of mailboxes per database in yo... How to create a single line PowerShell to retrieve the number of mailboxes per mailbox database in your organization. I was asked about that number in...
Automating the process to join a server to a domai... In this Tutorial we are going to optimize a simple task that in larger environments may take some time from the service desk/operations team which is ...