Receive-Output – A Write-* Wrapper and Log Writer

This is a wrapper for your Write-* cmdlets. Send the message, type and file log path, and it will write out the message with the proper write-* cmdlet and log the message to the specified file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<#
.Synopsis
   This is a wrapper for your Write-* cmdlets. Send the message, type and file log path, and it will write out the message with the proper write-* cmdlet and log the message to the specified file.
.DESCRIPTION
   A cmdlet / function to wrap all your write-* cmdlets
.EXAMPLE
   Receive-Output -Message "This is a test" -Type Default -Log "C:\temp\log.txt"
.EXAMPLE
   Receive-Output -Message "This is might be a test..." -Type Warning -Log "C:\temp\log.txt"
.EXAMPLE
   Receive-Output -Message "This is NOT a test!" -Type Error -Log "C:\temp\log.txt"
.EXAMPLE
   Receive-Output -M "This example uses alias parameters" -T Warning -L "C:\temp\log.txt"
.EXAMPLE
   Receive-Output -M "This example uses write-host with colors!" -T Host -L "C:\temp\log.txt" -C Magenta
#>

function Receive-Output
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([int])]
    Param
    (
        # Enter the message, use quotes as needed
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [Alias("M")]
        [string]
        $Message,

        # Enter the message level
        [Parameter(Mandatory=$true,
        Position=1)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [ValidateSet("Default", "Warning", "Error", "Debug", "Verbose", "Host")]
        [Alias("T")]
        [String]
        $Type = "Default",

        # Enter the log file path or variable containing path
        [Parameter(Mandatory=$true,
        Position=2)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]        
        [Alias("L")]
        [String]
        $Log,

        # Enter the color for host writes
        [Parameter(
        Position=3)]    
        [Alias("C")]
        [ConsoleColor[]]
        $Colorz = [System.ConsoleColor]::White
    )

    Switch ($Type) {    
        Default  {$out = {Write-Output $Message}}
        Debug  {$out = {Write-Debug $Message -Debug}}
        Error  {$out = {Write-Error $Message}}
        Warning  {$out = {Write-Warning $Message}}            
        Verbose  {$out = {Write-Verbose $Message -Verbose}}
        Host {$out = {Write-Host $Message -ForegroundColor $Colorz}}  
    }
    # Add timestamp and write out the message to the log
    $logMessage = "$(Get-Date -Format "yyyy-MM-dd HH:mm:ss") : $Message"
    Add-Content -Value $logMessage -Path $Log  
    # Write out the message using the proper cmdlet
    Return & $out    
}

Leave a Reply

Your email address will not be published. Required fields are marked *