Private/Invoke-PARClient.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 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
Function Invoke-PARClient { <# .SYNOPSIS Defines specified PARClient command and arguments .DESCRIPTION Defines a PARClient process object with arguments required for specific command. .PARAMETER PARClient The Path to PARClient.exe. Defaults to value of $Script:PAR.ClientPath, which is set during module import or via Set-PARConfiguration. .PARAMETER Server The name or address of the Vault server to target .PARAMETER Password SecureString of password used for PARClient operations .PARAMETER Credential A credential object containing the password used for PARClient operations .PARAMETER PassFile The path to a "password" file created by PARClient.exe, containing the encrypted password value used for remote operations via PARClient .PARAMETER CommandParameters The PARClient command to execute .PARAMETER PAROptions Additional command parameters. By default specifies /Q /C and /StateFileName. StateFileName is set to a file named after the process ID of the script, and with the local temp directory path .PARAMETER RemainingArgs A catch all parameter, accepts any remaining values from pipeline. Intended to suppress errors when piping in an object. .EXAMPLE Invoke-PARClient -Server EPV1 -Password $SecureString -CommandParameters "GetParm Vault DebugLevel" Invokes the GetParm action against the Vault on EPV1 and returns the DebugLevel parameter value. .NOTES AUTHOR: Pete Maan #> [CmdLetBinding(SupportsShouldProcess)] param( [Parameter( Mandatory = $False, ValueFromPipelineByPropertyName = $True )] [string]$PARClient = $Script:PAR.ClientPath, [Parameter( Mandatory = $False, ValueFromPipelineByPropertyName = $True )] [int]$Port = $Script:PAR.Port, [Parameter( Mandatory = $True, ValueFromPipelineByPropertyName = $True )] [string]$Server, [Parameter( Mandatory = $True, ValueFromPipelineByPropertyName = $True, ParameterSetName = "Password" )] [securestring]$Password, [Parameter( Mandatory = $True, ValueFromPipelineByPropertyName = $True, ParameterSetName = "Credential" )] [pscredential]$Credential, [Parameter( Mandatory = $True, ValueFromPipelineByPropertyName = $True, ParameterSetName = "PassFile" )] [ValidateScript( {Test-Path $_})] [string]$PassFile, [Parameter( Mandatory = $True, ValueFromPipelineByPropertyName = $True )] [string]$CommandParameters, [Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $True )] [string]$PAROptions = "/StateFileName $(Join-Path $env:temp "$PID.tmp") /Q /C", [Parameter(Mandatory = $False, ValueFromPipelineByPropertyName = $False, ValueFromRemainingArguments = $true )] $RemainingArgs ) Begin { Try { Get-Variable -Name PAR -ErrorAction Stop if($PAR.PSObject.Properties.Name -notcontains "ClientPath") { Write-Error "Heads Up!" -ErrorAction Stop } } Catch {throw "PARClient.exe not found `nRun Set-PARConfiguration to set path to PARClient"} #Create process $Process = New-Object System.Diagnostics.Process } Process { Switch($PSCmdlet.ParameterSetName) { "Credential" { $ClearTextPassword = $($Credential.GetNetworkCredential().Password) $PARCommand = "$Server/$ClearTextPassword"; break } "Password" { $ClearTextPassword = ConvertTo-InsecureString -SecureString $Password $PARCommand = "$Server/$ClearTextPassword"; break } "PassFile" {$PARCommand = "$Server /UsePassFile $PassFile"; break} } If($Port -gt 0) { $PARCommand = "$PARCommand /Port $Port" } $CommandParameters = $CommandParameters -replace ('^', '"') $CommandParameters = $CommandParameters -replace ('$', '"') if ($PSCmdlet.ShouldProcess($Server, "$CommandParameters")) { Write-Debug "Command Arguments: $PARCommand $PAROptions $CommandParameters" #Assign process parameters $Process.StartInfo.WorkingDirectory = "$(Split-Path $PARClient -Parent)" $Process.StartInfo.Filename = $PARClient $Process.StartInfo.Arguments = "$PARCommand $PAROptions $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-PARClientProcess -Process $Process $Result | Add-Member -MemberType NoteProperty -Name Server -Value $Server if($Result.StdOut -match '((?:^[A-Z]{5}[0-9]{3}[A-Z])|(?:ERROR \(\d+\)))(?::)? (.+)$') { #PARCL002S Authentication failure. Write-Error -Message $Matches[2] -ErrorId $Matches[1] } ElseIf($Result.StdOut -match '(.+) \((.+: \d)\)') { #Cannot get parameter DisableExceptionHandling. (Reason: 7) Write-Error -Message $Matches[1] -ErrorId $Matches[2] } Else {$Result} } } End { $Process.Dispose() } } |