Private/Start-PARClientProcess.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Function Start-PARClientProcess {

    <#
    .SYNOPSIS
    Starts PARClient process

    .DESCRIPTION
    Designed to receive PARClient process object from Invoke-PARClient.

    Returns Object containing ExitCode, StdOut & StdErr

    .PARAMETER Process
    System.Diagnostics.Process object containing PARClient parameters

    .EXAMPLE
    Start-PARClientProcess -Process $Process

    Invokes the Start method on the $Process object

    .NOTES
        AUTHOR: Pete Maan

    #>


    [CmdLetBinding(SupportsShouldProcess)]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSShouldProcess", "", Justification = "ShouldProcess handling is in Invoke-PARClient")]
    param(

        [Parameter(
            Mandatory = $True,
            ValueFromPipelineByPropertyName = $True
        )]
        [System.Diagnostics.Process]$Process
    )

    Begin {

    }

    Process {

        #Start Process
        $Process.start() | Out-Null

        #Read Output Stream First
        $StdOut = $Process.StandardOutput.ReadToEnd()
        $StdErr = $Process.StandardError.ReadToEnd()

        #If you wait for the process to exit before reading StandardOutput
        #the process can block trying to write to it, so the process never ends.
        $Process.WaitForExit()

        [PSCustomObject] @{

            "ExitCode" = $Process.ExitCode
            "StdOut"   = $StdOut
            "StdErr"   = $StdErr

        }

    }

    End {

        $Process.Dispose()

    }

}