Hi Team,
I am using REST API to add role on target machine. However, I am not able to remove using same function.
PowerShell Code
# Define working variables
$octopusURL = ""
$octopusAPIKey = ""
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
$spaceName = "default"
$machineName = ""
$targetRole = "AppServer-3"
$targetRemoveRole = "AppServer-2"
try
{
# Get space
$space = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/spaces/all" -Headers $header) | Where-Object {$_.Name -eq $spaceName}
# Get machine
$machine = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/machines/all" -Headers $header) | Where-Object {$_.Name -eq $machineName}
# Add target role
$machine.roles += ($targetRole) # It is working
$machine.Roles.Remove($targetRemoveRole) # It is working
Invoke-RestMethod -Method Put -Uri "$octopusURL/api/$($space.Id)/machines/$($machine.Id)" -Body ($machine | ConvertTo-Json -Depth 10) -Headers $header
}
catch
{
Write-Host $_.Exception.Message
}
Error
Exception calling "Remove" with "1" argument(s): "Collection was of a fixed size."