Functions/Disconnect-TCPWriter.ps1

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
Function Disconnect-TCPWriter
{
    <#
        .SYNOPSIS
        Disconnects/Closes a TCPWriter Object
 
        .DESCRIPTION
        Internal function.
         
        Disconnects/closes an open TCPWriter.
 
        .EXAMPLE
        Disconnect-TCPWriter -TCPWriter $Writer
        Closes the writer $Writer.
 
        .OUTPUTS
        None
    #>

    
    [CmdletBinding()]
    [OutputType($null)]
    param
    (
        # TcpWriter object
        [Parameter(Mandatory   = $true,
                   HelpMessage = 'TcpWriter object')]
        [ValidateNotNullOrEmpty()]
        [System.IO.StreamWriter]
        $TcpWriter
    )

    Try 
    {
        $TcpStream = $TcpWriter.BaseStream
                
        If ($TcpWriter)
        {
            Write-Verbose -Message 'Cleaning up the TCP writer object'
            $TcpWriter.Close()
        }

        If ($TcpStream)
        {
            Write-Verbose -Message 'Cleaning up the TCP stream object'
            $TcpStream.Dispose()
        }
    }
    Catch 
    {
        Throw $_
    }
}