Public/Invoke-SfExecuteApex.ps1

<#
    .SYNOPSIS
    Execute anonoymous apex in the configured Salesforce org
 
    .DESCRIPTION
    Execute anonoymous apex in the configured Salesforce org
 
    .INPUTS
    None. You cannot pipe objects to Invoke-SfExecuteApex.
 
    .OUTPUTS
    None. Throws error if the apex either fails to compile or the result is not 'success'
 
    .PARAMETER Apex
    The anonymous apex to execute.
 
    .EXAMPLE
    PS> Invoke-SfExecuteApex "phecc.TriggerHandlers.setEnabled(false, new List<String> {'ObservationBD' });"
 
    .LINK
    Set-FileConfig
 
    .NOTES
    Assumes config is initialized for org access.
#>

function Invoke-SfExecuteApex {
    param([String]$Apex)
    $encodedURL = [System.Web.HttpUtility]::UrlEncode($Apex)
    $result = (Invoke-SfApi "/tooling/executeAnonymous/?anonymousBody=$($encodedURL)")
    if ($result.compiled -ne 'True') {
        throw "Apex compile failed: $($result.compileProblem)-$($result.exceptionMessage)"
    }
    if ($result.success -ne 'True') {
        throw "Apex execution failed: $($result.exceptionMessage)-$($result.exceptionMessage)"
    }
}