...
Code Block |
---|
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $headers.Add("x-api-key", "{APIKEY}") $headers.Add("Content-Type", "application/json") # Ensure the computer group is created in the UI with at least one device # Replace 'GROUPNAME'{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://{BASEURL}/gytpolapi/v2.0/add_to_group" -Method 'POST' -Headers $headers -Body $body $response | ConvertTo-Json } |
...