Functions/Write-OutputMessage.ps1
<#
.SYNOPSIS This function writes a message to an output stream and returns it with a number of newlines appended. #> function Write-OutputMessage { [CmdletBinding(PositionalBinding=$true, DefaultParameterSetName="Information")] [OutputType([String])] param ( # The message to write to output. [Parameter(Mandatory=$true, ParameterSetName="Information", Position=0)] [Parameter(Mandatory=$true, ParameterSetName="Warning", Position=0)] [Parameter(Mandatory=$true, ParameterSetName="Error", Position=0)] [Parameter(Mandatory=$true, ParameterSetName="Stream", Position=0)] [ValidateNotNullOrEmpty()] [String]$message, # The number of newlines to add to the end of the message when it is returned. [Parameter(Mandatory=$false, Position=1)] [ValidateNotNull()] [Int32]$appendNewLines, # Indicate that the message should be written to the information stream. [Parameter(Mandatory=$true, ParameterSetName="Information")] [Switch]$toInformation, # Indicate that the message should be written to the warning stream. [Parameter(Mandatory=$true, ParameterSetName="Warning")] [Switch]$toWarning, # Indicate that the message should be written to the error stream. [Parameter(Mandatory=$true, ParameterSetName="Error")] [Switch]$toError, # Select the stream where the messages will be directed. [Parameter(Mandatory=$true, ParameterSetName="Stream")] [ValidateSet("Information", "Warning", "Error", "None")] [String]$outputStream, # Select whether the message should be returned from the function. [Parameter(Mandatory=$false)] [ValidateNotNull()] [Switch]$returnMessage = [Switch]::Present ) # Write the message to output $switchVariable = if ($PSCmdlet.ParameterSetName -ne "Stream") { $PSCmdlet.ParameterSetName } else { $outputStream } switch ($switchVariable) { Information { Write-Information $message } Warning { Write-Warning $message } Error { Write-Error $message } None { # Do nothing } } # Return the message with new lines added $stringToAppend = "" if ($appendNewLines -gt 0) { $stringToAppend = "`r`n" * $appendNewLines } if ($returnMessage) { return ($message + $stringToAppend) } } |