Getting Atera Custom Fields in Scripts

On a Reddit post recently someone asked how they might get the value from an Atera Custom Field on a customer from within a script on an agent. It's actually pretty easy using the PSAtera module. A great use case for this is installing Sophos Anti-Virus on an endpoint, one of my latest projects at Cage Data. To make the installation process easy, I've added a custom field to customers in Atera called "Sophos Key" and put the Sophos installer onto a SharePoint site so I can download the installer in my script. Here's what the script will look like to get the Sophos Key and install the software:

$AteraAPIKey = 'MY ATERA API KEY'
$FieldName = 'Sophos Key'
$SophosURI = 'https://SOME SHAREPOINT PUBLIC URL FOR/SophosSetup.exe'

# Install and load the right version of Atera
if (!(Get-Module -ListAvailable PSAtera)) {
	Install-Module -Name PSAtera -MinimumVersion 1.3.1 -Force
}
Import-Module -Name PSAtera -MinimumVersion 1.3.1

Set-AteraAPIKey -APIKey $AteraAPIKey

# Get the agent information for the PC that's running the script
$agent = Get-AteraAgent

# Get the value from the Customer endpoint
$customValue = Get-AteraCustomValue -ObjectType Customer -ObjectId $agent.CustomerID -FieldName $FieldName

# Download Sophos Installer to temp path
$SophosInstaller = Join-Path -Path $env:TEMP -ChildPath "SophosSetup.exe"
Invoke-WebRequest -Uri $SophosURI -OutFile $SophosInstaller

& $SophosInstaller --customertoken="$($customValue.ValueAsString)" --epiinstallerserver="api-cloudstation-us-east-2.prod.hydra.sophos.com" --products="all" --quiet

# Get the status of the Sophos Install
do {
	Get-Process -Name "*SophosSetup.exe*"
    Start-Sleep -Seconds 10
} while (Get-Process -Name "*SophosSetup.exe*" -ErrorAction SilentlyContinue)

# After install is over, get the status of the Sophos Services
Get-Service -Name "*Sophos*"

To make the script work for you, just change the variable values in the first 3 lines of the script.