Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("x-api-key", "{x-api-key}")
$headers.Add("Content-Type", "application/json")

# Ensure the computer group is created in the UI with at least one device
# Replace {GROUP} with the actual group name, i.e SERVERS
$groupName = "{GROUP}"

# Read computer names from CSV file without headers
$computers = Get-Content -Path "c:\path\to\computers.csv" | Select-Object -Skip 1

# Loop through each computer and add it to the group
foreach ($line in $computers) {
    # Split the line by commas and take the first part as the computer name
    $computerName = $line.Split(',')[0].Trim()

    # Create the body for the API request
    $body = @{
        groupName = $groupName
        computerName = $computerName
    } | ConvertTo-Json
    
    # Send the API request
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
    
    $response = Invoke-RestMethod -Uri "https://{BASE-URL}/gytpolapi/v2.0/add_to_group" -Method 'POST' -Headers $headers -Body $body
    $response | ConvertTo-Json
    
    # Optional: Add a 2-second pause between each iteration. Remove the comment if needed.
    # Start-Sleep -Seconds 2
}

Note: Change BASE-URL to your base URL. Change x-api-key to your API key.

...