Move Multiple Computers Across Your Organization to a Target Group

This API script will move multiple computers - regardless of what group they’re currently in - to a group you specify.

You’ll need to set the following before running the script:

$apiKey = 'YOUR_API_KEY' - in your console, go to Settings->API and select the API key. Note that the API key is per admin user, so you and another admin in your console will have different API keys.

$orgID = 'YOUR_ORG_ID' - put in your Org ID which can be found by looking at the URL on the dashboard and selecting the value after the “?o=”: [https://console.automox.com/dashboard?o=1234]. In this example the Org ID is 1234.

$targetGID = 'GROUP_ID#_TO_MOVE_TO' - put your Group ID you want the devices moved to, which can be found by viewing the group page that lists devices in that particular group and grabbing the value after “gid=” in the URL.

$toMove - Put your list of device names you want to be moved in quotes separated by a comma.

You can also modify $logpath if you want to change the location where the logs are saved. Keep in mind the script will overwrite a previously generated file if it exists.

# Replace the variables below with your Org ID, API key, server group to move to, & system names
$orgID = 'YOUR_ORG_ID'
$apiKey = 'YOUR_API_KEY'
$targetGID = 'GROUP_ID#_TO_MOVE_TO'

# List of computers to move to target group
$toMove = @("Computer1","Computer2","Computer3")

#modify log path as desired
$logPath = 'C:\Temp\'

###########################

$page = 0
$limit = 500
$servers = @()

#Easier to maintain, especially if multiple organizations, or for repurposing for different API Tables
$apiInstance = 'https://console.automox.com/api/'
$apiTable = 'servers'

#initialize empty arrays to store server IDs to be moved
$failMove = @()
$successMove = @()

while($true) {

    $orgAndKey = "?o=$orgID&api_key=$apiKey&l=$limit&p=$page"

    #put components together
    $uri = $apiInstance + $apiTable + $orgAndKey

    #Get the json body of the Web Request
    $resp = (Invoke-WebRequest -Method GET -Uri $uri -UseBasicParsing).Content | ConvertFrom-Json | Select-Object results

    $servers += $resp.results
    $page += 1

    if($resp.results.count -lt $limit) {
        break
    }
}

############################# OPERATIONAL Section #############################

foreach ($move in $toMove) {

    # Check each server and conditionally take action
    foreach ($server in $servers) {
        
        # Pull out wanted details
        $serverID = $server.id
        $serverName = $server.name
        $serverGID = $server.server_group_id
    
        if (($serverGID -ne $targetGID) -and ($move -eq $serverName)) {

            #$toMove += @{"ServerName"=$serverName;"ServerID"=$serverID}
            $moveURI = $apiInstance + $apiTable + '/' + $serverID + $orgAndKey
        
            # Attempt to move and track failures
            try { 
                $headers = @{
                    "Authorization" = "Bearer $apiKey"
                    "Content-Type" = "application/json"
                }
                $body = @"
                {
                    "server_group_id": $targetGID
                }
"@

                $moveResponse = Invoke-WebRequest -Method Put -Uri $moveURI -Headers $headers -Body $body
                Write-Output "Successfully Moved Server: $serverName"
                $successMove += @{"ServerName"=$serverName;"ServerID"=$serverID}
            }
            catch { 
                $failMove += @{"ServerName"=$serverName;"ServerID"=$serverID}
                Write-Output "Failed to Move Server: $serverName" 
            }
    
        }

        # Output logging into json files for later review/manipulation
        $successMove | ConvertTo-Json | Out-File $logPath\Move_Success.json
        $failMove | ConvertTo-Json | Out-File $logPath\Move_Failed.json
    }    
}

You can also use this version to import all of the computer names from a .txt file and move them to a designated group. When you create the text file, put each computer on its’ own line.

# Replace the variables below with your Org ID, API key, server group to move to, & system names
$orgID = 'YOUR_ORG_ID'
$apiKey = 'YOUR_API_KEY'
$targetGID = '12345'
$file = 'C:\Temp\Move_Devices.txt'

#modify log path as desired
$logPath = 'C:\Temp\'

###########################

$page = 0
$limit = 500
$servers = @()
$toMove = @()

# Read servers from txt file and put into array
$serverList = Get-Content $file
ForEach ($line in $serverList) {
    $toMove += $line
}

#Easier to maintain, especially if multiple organizations, or for repurposing for different API Tables
$apiInstance = 'https://console.automox.com/api/'
$apiTable = 'servers'

#initialize empty arrays to store server IDs to be moved
$failMove = @()
$successMove = @()

while($true) {

    $orgAndKey = "?o=$orgID&api_key=$apiKey&l=$limit&p=$page"

    #put components together
    $uri = $apiInstance + $apiTable + $orgAndKey

    #Get the json body of the Web Request
    $resp = (Invoke-WebRequest -Method GET -Uri $uri -UseBasicParsing).Content | ConvertFrom-Json | Select-Object results

    $servers += $resp.results
    $page += 1

    if($resp.results.count -lt $limit) {
        break
    }
}

############################# OPERATIONAL Section #############################

foreach ($move in $toMove) {

    # Check each server and conditionally take action
    foreach ($server in $servers) {
        
        # Pull out wanted details
        $serverID = $server.id
        $serverName = $server.name
        $serverGID = $server.server_group_id
    
        if (($serverGID -ne $targetGID) -and ($move -eq $serverName)) {

            #$toMove += @{"ServerName"=$serverName;"ServerID"=$serverID}
            $moveURI = $apiInstance + $apiTable + '/' + $serverID + $orgAndKey
        
            # Attempt to move and track failures
            try { 
                $headers = @{
                    "Authorization" = "Bearer $apiKey"
                    "Content-Type" = "application/json"
                }
                $body = @"
                {
                    "server_group_id": $targetGID
                }
"@

                $moveResponse = Invoke-WebRequest -Method Put -Uri $moveURI -Headers $headers -Body $body
                Write-Output "Successfully Moved Server: $serverName"
                $successMove += @{"ServerName"=$serverName;"ServerID"=$serverID}
            }
            catch { 
                $failMove += @{"ServerName"=$serverName;"ServerID"=$serverID}
                Write-Output "Failed to Move Server: $serverName" 
            }
    
        }

        # Output logging into json files for later review/manipulation
        $successMove | ConvertTo-Json | Out-File $logPath\Move_Success.json
        $failMove | ConvertTo-Json | Out-File $logPath\Move_Failed.json
    }    
}