Public/Invoke-SfApi.ps1

<#
    .SYNOPSIS
    Invokes a Salesforce API endpoint for the currently configured org.
 
    .DESCRIPTION
    Invokes a Salesforce API endpoint for the currently configured org.
 
    .INPUTS
    None. You cannot pipe objects to Invoke-SfApi.
 
    .OUTPUTS
    Returns as PSCustomObject with the format of the API being called. (Depends on use)
 
    .PARAMETER Path
    The path portion beyond "/services/data" to use.
 
    .PARAMETER Version
    The version of the API to use. The default is 48.0
 
    .PARAMETER Method
    The method to call. The default is "Get"
 
    .EXAMPLE
    PS> $patientIds = (Invoke-SfApi "/query?q=SELECT+Id+FROM+phecc__Patient__c").records | Select Id
 
    .LINK
    Set-FileConfig
 
    .NOTES
    Assumes config is initialized for org access.
#>

function Invoke-SfApi {
    param([String]$Path, [String]$Version = "48.0", [String]$Method = "Get")
    $orgUrl = $script:__sfAuth.instance_url
    $headers = SfHeaders $script:__sfAuth
    $services = Invoke-RestMethod -Uri "$($orgUrl)/services/data" -Method Get
    $dataUrl = ($services | Where-Object { $_.version -eq $version }).url
    Write-Debug $dataUrl
    $dataFullUrl = "$($orgUrl)$($dataUrl)"
    $url = "$($dataFullUrl)$($path)"
    Write-Debug $url
    Invoke-RestMethod -Uri $url -Method $Method -Headers $headers
}