parts/InvokeApi.ps1


<#
.SYNOPSIS
Invokes the Secret Server Rest API, internally used by functions only
#>

[Cmdletbinding()]
param(
    # Secret Server REST API URL
    [Parameter(Mandatory,ValueFromPipelineByPropertyName)]
    [Alias('Url')]
    [uri]
    $Uri,

    # Valid Access Token issued by Secret Server
    [Parameter(ValueFromPipelineByPropertyName)]
    [Alias('PAT')]
    [string]
    $PersonalAccessToken,

    # Method used for the web request, supported by Secret Server
    [Parameter(ValueFromPipelineByPropertyName)]
    [ValidateSet('GET','DELETE', 'PATCH', 'POST', 'PUT')]
    [string]
    $Method,

    # Specifies the body of the request.
    [Parameter(ValueFromPipelineByPropertyName)]
    [Object]
    $Body,

    # Specifies the file path to write the content.
    [Parameter(ValueFromPipelineByPropertyName)]
    [string]
    $OutFile,

    # Specifies the content type of the web request.
    # If this parameter is omitted and the request method is POST, Invoke-RestMethod sets the content type to application/x-www-form-urlencoded. Otherwise, the content type is not specified in the call.
    [string]
    $ContentType = 'application/json',

    # Header of the web request. Enter a hash table or dictionary.
    [System.Collections.IDictionary]
    [Alias('Header')]
    $Headers,

    # Indicates using the credentials of the current user to send the web request (winauth).
    [Alias('UseDefaultCredential')]
    [switch]
    $UseDefaultCredentials,

    # Specifies that the cmdlet uses a proxy server for the request, rather than connecting directly to the Internet resource. Enter the URI of a network proxy server.
    [uri]
    $Proxy,

    # Specifies a user account that has permission to use the proxy server that is specified by the Proxy parameter. The default is the current user.
    # Type a user name, such as "User01" or "Domain01\User01", or enter a PSCredential object, such as one generated by the Get-Credential cmdlet.
    # This parameter is valid only when the Proxy parameter is also used in the command. You cannot use the ProxyCredential and ProxyUseDefaultCredentials parameters in the same command.
    [PSCredential]
    [Management.Automation.CredentialAttribute()]
    $ProxyCredential,

    # Indicates that the cmdlet uses the credentials of the current user to access the proxy server that is specified by the Proxy parameter.
    # This parameter is valid only when the Proxy parameter is also used in the command. You cannot use the ProxyCredential and ProxyUseDefaultCredentials parameters in the same command.
    [switch]
    $ProxyUseDefaultCredentials
)
process {
    #region Prepare Parameters
    $irmSplat = @{ } + $PSBoundParameters    # First, copy PSBoundParameters and remove the parameters that aren't Invoke-RestMethod's
    $irmSplat.Remove('PersonalAccessToken') # * -PersonalAccessToken
    $irmSplat.Remove('PSTypeName') # * -PSTypeName
    $irmSplat.Remove('Property') # *-Property
    $irmSplat.Remove('RemoveProperty') # *-RemoveProperty
    $irmSplat.Remove('ExpandProperty') # *-ExpandProperty
    if ($PersonalAccessToken) {
        # If there was a personal access token, set the authorization header
        if ($Headers) {
            # (make sure not to step on other headers).
            $irmSplat.Headers.Authorization = "Bearer $PersonalAccessToken"
        } else {
            $irmSplat.Headers = @{
                Authorization = "Bearer $PersonalAccessToken"
            }
        }
    }
    if (-not $irmSplat.ContentType) {
        # If no content type was passed
        $irmSplat.ContentType = $ContentType # set it to the default.
    }
    #endregion Prepare Parameters

    #region Call Invoke-RestMethod
    Invoke-RestMethod @irmSplat
    #endregion Call Invoke-RestMethod
}