Simple API Instructions required :)

BEKAHENI
BEKAHENI Member Posts: 15 ✭✭
edited August 14 in Scripting and automations

Hi All,

Thought I would turn my attention to API's ! and getting thoroughly frustrated at my failure !!

Would any one be kind enough to provide a simple example on how I get it to work !

What I would like is this :-

Create a custom Field in say the Agents area, lets call it "Field1"

Then I would like a script that uses the API to then populate that field with for example sake the hostname (just using that as an example which can be customised to a future needs later)

This script then attached to a threshold etc to be executed.

Is this even possible ? (yes I know this is available if I upgrade to super users and get the "Script based custom fields" functionality)

Would that script then update all the custom fields of all Agent's called "Field1"

Many Thanks in Advance Nick

Tagged:

Comments

  • COOLNETAU
    COOLNETAU Member Posts: 58 ✭✭
    edited August 15
    # Import the PSAtera module
    Import-Module PSAtera
    
    # Define the Atera API Key
    $APIKey = "INSERT API KEY"
    
    # Connect to the Atera API
    Connect-AteraAPI -APIKey $APIKey
    
    # Define the customer custom field variable
    $CustomerCustomField = Get-AteraCustomerCustomField -Name "DNS1"
    
    # Check if the customer custom field is not null or empty
    if (![string]::IsNullOrEmpty($CustomerCustomField.Value)) {
    
        # Get the agent custom field
        $AgentCustomField = Get-AteraAgentCustomField -Name "DNS1"
    
        # Check if the agent custom field is empty
        if ([string]::IsNullOrEmpty($AgentCustomField.Value)) {
    
            # Populate the agent custom field with the contents of the customer custom field
            Set-AteraAgentCustomField -Name "DNS1" -Value $CustomerCustomField.Value
        }
    }
    else {
        # Output a message indicating that the customer custom field is empty or does not exist
        Write-Output "The customer custom field DNS1 is empty or does not exist."
    }
    
    # Disconnect from the Atera API
    Disconnect-AteraAPI
    

  • COOLNETAU
    COOLNETAU Member Posts: 58 ✭✭

    this grabs the DNS1 field from the customer and populates the agend field called DNS1

  • gilgi
    gilgi Administrator, Moderator, Internal Posts: 294 admin

    nice @COOLNETAU !

    Does that work for you @BEKAHENI ?

  • BEKAHENI
    BEKAHENI Member Posts: 15 ✭✭

    Thanks Coolnetau,

    I will follow that this afternoon and report back.

    Will be a good template to use for anythign else we want to populate :)

    Cheers

  • BEKAHENI
    BEKAHENI Member Posts: 15 ✭✭

    Hi All,

    Ok tried running it (inserting my own API) but I get the error "The term 'Connect-AteraAPI' is not recognized as the name of a cmdlet" when runnign it in powershell (As admin) on my own machine.

    I have created the Custom field and named it DNS1

    Thanks In advance

    Nick

  • COOLNETAU
    COOLNETAU Member Posts: 58 ✭✭

    You must install the PSAtera module

    • that in itself is a whole another can of worms lol