# Custom Field You can read and set Ninja [[NinjaOne Custom Fields|custom fields]] inside of scripts. ## Read Custom Field ```PowerShell Ninja-Property-Get fieldName ``` ### Example: Get Printix ID #### Windows ```PowerShell $PrintixTenantId = Ninja-Property-Get printixTenantId ``` #### MacOS ```bash # Ninja Custom Fields on MacOS ninjaCLI="$NINJA_DATA_PATH/ninjarmm-cli" # Get the organization custom field information. printixTenantId=$("$ninjaCLI" "get" "printixTenantId") printixTenantDomain=$("$ninjaCLI" "get" "printixTenantDomain") # Short circuit if the Ninja Custom Fields are not set if [ -z "$printixTenantId" ] || [ -z "$printixTenantDomain" ]; then echo "$(date) | WARN: Ninja Custom Fields not set. Exiting." exit 0 ``` ## Write Custom Field ```PowerShell Ninja-Property-Set fieldName ``` ### Example: Print Timestamp ```PowerShell # Set timestamp for when the password was last updated (ISO DateTime format in UTC) $timestamp = (Get-Date).ToUniversalTime().ToString("yyyy-MM-ddTHH:mm:ss") Ninja-Property-Set localAdminPasswordLastUpdated $timestamp Write-Host "Date stored in NinjaOne custom field 'localAdminPasswordLastUpdated': $timestamp UTC" -ForegroundColor Green ``` # Script Variables > When a script is executed and includes script variables (dynamic script forms), the NinjaOne agent adds them as environment variables only for the lifetime of the script's execution. If the system already has an existing environment variable being sent from the script, the script fails and sends an activity log clarifying what script failed and why. > \- [Automation Library: Using Variables in Scripts | NinjaOne Dojo](https://ninjarmm.zendesk.com/hc/en-us/articles/17783013460621) ## Using Variables in a Script > NinjaOne technicians can add their created script variables to the script editor by clicking in an empty space on the script editor and then pressing `CTRL + Space` on the keyboard. The script variable selector displays as a small pop-up; hover your mouse pointer over the variables to see a description. > \- [Automation Library: Using Variables in Scripts | NinjaOne Dojo](https://ninjarmm.zendesk.com/hc/en-us/articles/17783013460621) ### Example: Reading Product Key from Script Variable ```PowerShell # Replace with your actual ESU product key $ESU_MAK = $env:esuActivationKey $ESU_Year = $env:esuYear # ESU Activation IDs $ActivationIDs = @{ "Year 1 (2025-2026)" = "f520e45e-7413-4a34-a497-d2765967d094" "Year 2 (2026-2027)" = "1043add5-23b1-4afb-9a0f-64343c8f3f8d" "Year 3 (2027-2028)" = "83d49986-add3-41d7-ba33-87c7bfb5c0fb" } $ActivationID = $ActivationIDs[$ESU_Year] Write-Output "Installing client-specific ESU MAK key $ESU_MAK" cscript.exe /b %windir%\system32\slmgr.vbs /ipk $ESU_MAK Write-Output "Activating generic ESU MAK key for Year $ESU_Year ($ActivationID)..." cscript.exe /b %windir%\system32\slmgr.vbs /ato $ActivationID ```