Public/Invoke-SfQuery.ps1

<#
    .SYNOPSIS
    Execute a SOQL statement in the configured Salesforce org

    .DESCRIPTION
    Execute a SOQL statement in the configured Salesforce org

    .INPUTS
    None. You cannot pipe objects to Invoke-SfQuery.

    .OUTPUTS
    An array of PSCustomObjects that are the result of the SOQL query.

    .PARAMETER Query
    The SOQL statement to execute

    .EXAMPLE
    PS> $AccountNames = Invoke-SfQuery "SELECT Name FROM Account" | Select Name

    .LINK
    Set-Config

    .NOTES
    Assumes config is initialized for org access.
#>

function Invoke-SfQuery {

    [CmdletBinding()]
    [OutputType([PSCustomObject[]])]
    param(
        [Parameter(Mandatory = $true, Position = 0, ValueFromPipeline)]
        [ValidateNotNullOrEmpty()]
        [String]$Query
    )
    begin {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Function started"
    }

    end {
        Write-Verbose "[$($MyInvocation.MyCommand.Name)] Complete"
    }

    process {
        Write-Debug "[$($MyInvocation.MyCommand.Name)] PSBoundParameters: $($PSBoundParameters | Out-String)"

        $q = $Query -replace " ", "+"
        $query = "/query?q=$($q)"

        $records = @()
        do {
            $response = Invoke-SfApi $query
            $records += $response.records
            if ($response.nextRecordsUrl) {
                $query = "/query/$($response.nextRecordsUrl -replace '.*/')"
            }
        } while ($response.done -eq $false)
        Write-Output $records
    }
}