Private/Invoke-AIMClient.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
Function Invoke-AIMClient {

    <#
    .SYNOPSIS
    Defines specified CLIPasswordSDK command and arguments

    .DESCRIPTION
    Defines a CLIPasswordSDK process object with arguments required for specific command.

    .PARAMETER ClientPath
    The Path to CLIPasswordSDK.exe.
    Defaults to value of $Script:AIM.ClientPath, which is set during module import or via Set-AIMConfiguration.

    .PARAMETER CommandParameters
    The CLIPasswordSDK command to execute

    .PARAMETER PAROptions
    Additional command parameters.

    .PARAMETER RemainingArgs
    A catch all parameter, accepts any remaining values from pipeline.
    Intended to suppress errors when piping in an object.

    .EXAMPLE
    Invoke-AIMClient -CommandParameters "/p AppDescs.AppID=TestApp /p RequiredProps=UserName,Address /p Query="Safe=TestSafe;Folder=Root;UserName=TestUser1" /o PassProps.UserName,PassProps.Address,Password,PasswordChangeInProcess""

    Invokes the GetPassword action using the provided arguments.

    .NOTES
        AUTHOR: Pete Maan

    #>


    [CmdLetBinding(SupportsShouldProcess)]
    [Diagnostics.CodeAnalysis.SuppressMessageAttribute("PSReviewUnusedParameter", "RemainingArgs", Justification = "Intentionally Unused Parameter")]
    param(

        [Parameter(
            Mandatory = $False,
            ValueFromPipelineByPropertyName = $True
        )]
        [string]$ClientPath = $Script:AIM.ClientPath,

        [Parameter(
            Mandatory = $False,
            ValueFromPipelineByPropertyName = $True
        )]
        [string]$Command = "GetPassword",

        [Parameter(
            Mandatory = $True,
            ValueFromPipelineByPropertyName = $True
        )]
        [string]$CommandParameters,

        [Parameter(Mandatory = $False,
            ValueFromPipelineByPropertyName = $True
        )]
        [string]$Options,

        [Parameter(Mandatory = $False,
            ValueFromPipelineByPropertyName = $False,
            ValueFromRemainingArguments = $true
        )]
        $RemainingArgs
    )

    Begin {

        Try {

            Get-Variable -Name AIM -ErrorAction Stop

            #Check we have the path to the required client executable
            if($AIM.PSObject.Properties.Name -notcontains "ClientPath") {

                Write-Error "Heads Up!" -ErrorAction Stop

            }

        } Catch {throw "CLIPasswordSDK.exe not found `nRun Set-AIMConfiguration to set path to CLIPasswordSDK"}

        #Create process
        $Process = New-Object System.Diagnostics.Process

    }

    Process {

        if ($PSCmdlet.ShouldProcess($ClientPath, "$CommandParameters")) {

            Write-Debug "Command Arguments: $Command $Options $CommandParameters"

            #Assign process parameters

            $Process.StartInfo.WorkingDirectory = "$(Split-Path $ClientPath -Parent)"
            $Process.StartInfo.Filename = $ClientPath
            $Process.StartInfo.Arguments = "$Command $Options $CommandParameters"
            $Process.StartInfo.RedirectStandardOutput = $True
            $Process.StartInfo.RedirectStandardError = $True
            $Process.StartInfo.UseShellExecute = $False
            $Process.StartInfo.CreateNoWindow = $True
            $Process.StartInfo.WindowStyle = "hidden"

            #Start Process
            $Result = Start-AIMClientProcess -Process $Process -ErrorAction Stop

            #Return Error or Result
            if($Result.StdErr -match '((?:^[A-Z]{5}[0-9]{3}[A-Z])|(?:ERROR \(\d+\)))(?::)? (.+)$') {

                #APPAP008E Problem occurred while trying to use user in the Vault
                Write-Debug "ErrorId: $($Matches[1])"
                Write-Debug "Message: $($Matches[2])"
                Write-Error -Message $Matches[2] -ErrorId $Matches[1]

            } Else {$Result}
        }

    }

    End {

        $Process.Dispose()

    }

}