Private/Start-AIMClientProcess.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
70
71
Function Start-AIMClientProcess {

    <#
    .SYNOPSIS
    Starts AIM CLI process

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

    Returns Object containing ExitCode, StdOut & StdErr

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

    .EXAMPLE
    Start-AIMClientProcess -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-AIMClient")]
    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()

        Write-Debug "Exit Code: $($Process.ExitCode)"

        [PSCustomObject] @{

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

        }

    }

    End {

        $Process.Dispose()

    }

}