PowerShell Requires Chilkat v11.0.0+
PowerShell
Schwab: List authorized accounts and select one
See more Schwab Examples
Lists masked account numbers. Run once to see their endings, then enter the last four digits of the intended account. A unique match saves Schwab’s hashValue in a separate secret for the account-specific examples. If two accounts share an ending, use the full-account input from your account picker. A failed match leaves the previous selection in place.
Use the access token saved by desktop authorization. Use the same Chilkat Secrets store as the OAuth example.
Chilkat PowerShell Downloads
Add-Type -Path "C:\chilkat\ChilkatDotNet47-x64\ChilkatDotNet47.dll"
$success = $false
# Assumes Chilkat is unlocked. See Global Unlock Sample.
$success = $false
$vault = New-Object Chilkat.Secrets
$vault.Location = "local_manager"
$vault.AutoSplit = $true
$entry = New-Object Chilkat.JsonObject
$entry.UpdateString("appName","SchwabTrader")
$entry.UpdateString("service","OAuth2")
$entry.UpdateString("username","personal-desktop")
$tokens = New-Object Chilkat.JsonObject
$success = $vault.GetSecretJson($entry,$tokens)
if ($success -ne $true) {
$("Could not read the token record. Check the store and complete desktop authorization first.")
exit
}
$accessToken = $tokens.StringOf("access_token")
if ($accessToken -eq "") {
$("Run the Schwab authorization or refresh example to save an access token.")
exit
}
$http = New-Object Chilkat.Http
$http.RequireSslCertVerify = $true
$http.AuthToken = $accessToken
# Supply one of these from your account picker. Leave both empty to list only.
$selectedEnding = ""
$selectedFullNumber = ""
$resp = New-Object Chilkat.HttpResponse
$success = $http.HttpNoBody("GET","https://api.schwabapi.com/trader/v1/accounts/accountNumbers",$resp)
if (($success -ne $true) -or ($resp.StatusCode -ne 200)) {
if ($resp.StatusCode -eq 0) {
$("No HTTP response. Check connectivity.")
}
else {
$("Schwab returned HTTP " + $resp.StatusCode)
if ($resp.StatusCode -eq 401) {
$("Refresh the access token or repeat desktop authorization.")
}
}
exit
}
$accounts = New-Object Chilkat.JsonArray
$success = $accounts.Load($resp.BodyStr)
if ($success -ne $true) {
$("Schwab returned unexpected JSON.")
exit
}
$row = New-Object Chilkat.JsonObject
$number = New-Object Chilkat.StringBuilder
$matches = 0
$i = 0
$selectedHash = ""
while (($i -lt $accounts.Size)) {
$accounts.ObjectAt2($i,$row)
$accountNumber = $row.StringOf("accountNumber")
$number.SetString($accountNumber)
if ($number.Length -ge 4) {
$start = $number.Length - 4
$ending = $number.GetRange($start,4,$false)
$("Account ending in " + $ending)
$matched = $false
if ($selectedFullNumber -ne "") {
if ($accountNumber -eq $selectedFullNumber) {
$matched = $true
}
}
else {
if ($ending -eq $selectedEnding) {
$matched = $true
}
}
if ($matched -eq $true) {
$matches = $matches + 1
$selectedHash = $row.StringOf("hashValue")
}
}
$i = $i + 1
}
if (($matches -ne 1) -or ($selectedHash -eq "")) {
$("Select exactly one account. For duplicate endings use the full-account input.")
exit
}
$entry.UpdateString("service","SelectedAccount")
$selection = New-Object Chilkat.JsonObject
$selection.UpdateString("hashValue",$selectedHash)
$success = $vault.UpdateSecretJson($entry,$selection)
if ($success -ne $true) {
$("Could not save the selected account.")
exit
}
$("Selected account saved.")