Public/Get-GW2Characters.ps1

<#
.SYNOPSIS
Retrieves character information from the Guild Wars 2 API.
 
.DESCRIPTION
Calls the Guild Wars 2 API v2/characters endpoint.
- If no parameters are provided, returns a list of character names.
- If one or more names are provided via -Names, returns details for those characters.
- If -All is specified, retrieves all characters (note: this may be resource intensive).
- Uses schema version 2019-12-19T00:00:00.000Z to include buildtabs and equipmenttabs.
 
.PARAMETER APIKey
The Guild Wars 2 API key (string) to authenticate the request. Requires 'characters' scope (and 'builds'/'inventories' etc dependent on depth).
 
.PARAMETER Names
Optional. A list of character names to retrieve. Percent encoding is handled automatically.
 
.PARAMETER All
Optional. If set, retrieves all characters.
 
.EXAMPLE
Get-GW2Characters -APIKey 'key'
Returns a list of character names.
 
.EXAMPLE
Get-GW2Characters -APIKey 'key' -Names "Character Name"
Returns details for "Character Name".
 
.EXAMPLE
Get-GW2Characters -APIKey 'key' -Names @("Character Name","Character Name 2")
Returns details for "Character Name" and "Character Name 2".
 
.EXAMPLE
Get-GW2Characters -APIKey 'key' -All
Returns details for all characters on the account.
 
.NOTES
- Requires network access to api.guildwars2.com.
#>

function Get-GW2Characters {
    param (
        [Parameter(Mandatory = $true)]
        [string]$APIKey,
        
        [Parameter(Mandatory = $false)]
        [string[]]$Names,

        [Parameter(Mandatory = $false)]
        [switch]$All
    )
    
    $headers = @{
        "Authorization" = "Bearer $APIKey"
    }

    $url = "https://api.guildwars2.com/v2/characters"

    if ($All) {
        $url = $url + "?ids=all"
    }
    elseif ($Names) {
        $encodedNames = $Names | ForEach-Object { [System.Web.HttpUtility]::UrlEncode($_) }
        $idString = $encodedNames -join ','
        $url = $url + "?ids=$idString"
    }
    $response = Invoke-RestMethod -Uri $url -Method Get -Headers $headers
    
    return $response
}