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. Get,Post,Delete,Put. The default is "Get"
 
    .PARAMETER Body
    The body to pass to the method
 
    .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", [ValidateSet('Get','Post', 'Delete', 'Put')]$Method = "Get", [String]$Body)
    $orgUrl = $script:__sfAuth.instance_url
    $headers = Get-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)"
    $headers.Add("Content-Type", "application/json")
    Write-Debug "Invoke-RestMethod -Uri $($url) -Method $($Method)"
    Write-Debug "Headers: $($headers)"
    Write-Debug "Body: $($body)"
    Invoke-RestMethod -Uri $url -Method $Method -Headers $headers -Body $Body
}