In 2025, the below works for checking if run as runbook:
$env:AZUREPS_HOST_ENVIRONMENT -eq "AzureAutomation"