You can write scripts for your site to retrieve information or execute a function. Drush will bootstrap Drupal before running the script, meaning you will have access to the API to load content or trigger actions.
drush scr path/to/script.php
drush topic docs:script
<?php
use Drush\Drush;
if (empty($extra)) {
echo "Which host?\n";
echo "Ex: -- lando \n";
return;
}
$host = array_pop($extra);
foreach (Drush::aliasManager()->getMultiple("@$host") as $site) {
$this->output()->writeln('Updating ' . $site->name());
$updb = Drush::drush($site, 'updb', [], [], []);
$updb_exit = $updb->run();
$this->output()->writeln($updb->getOutput());
$this->output()->writeln($updb->getErrorOutput());
if ($updb_exit > 0) {
$cr = Drush::drush($site, 'cr', [], [], []);
$cr->run();
}
}