Edit Machine Using REST API

Hi,

I’m trying to edit the TenantedDeploymentParticipation parameter of my machines via Powershell through the REST API. I’m able to get the json object of a machine and change the parameter within the object. However, when I try to call PUT to update, I keep getting 400 errors.

PS C:\Users\15SOF> $machine
Id : Machines-724
Name : Server01
Thumbprint : 0384B258021C16819A779370DD7706E3D7AE7B9B
Uri :
IsDisabled : False
EnvironmentIds : {Environments-1}
Roles : {app-server}
MachinePolicyId : MachinePolicies-1
TenantedDeploymentParticipation : TenantedOrUntenanted

PS C:\Users\15SOF> Invoke-WebRequest $OctopusURL/api/machines/$machineId -Method Put -Headers $header -Body $machine
Invoke-WebRequest : The remote server returned an error: (400) Bad Request.
At line:1 char:1

  • Invoke-WebRequest $OctopusURL/api/machines/$machineId -Method Put -H …
  •   + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
     eption
      + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
    
    

Any help or suggestions are greatly appreciated!

Thanks!

Hi Austin,

Thanks for getting in touch! I suspect that the 400 validation error is perhaps due to the PUT command not including the whole body. I’ve done some testing and was able to accomplish this with the script below. :slight_smile:

# set up variables
$apikey = ""
$octopusURL = ""
$machineName = ""

# change tenanted participation
$machines = Invoke-RestMethod -uri ($octopusURL, "/api/machines/all?apikey=", $apikey -join "")
$machine = $machines | ? { $_.Name -eq $machineName} 
$machine.TenantedDeploymentParticipation = ("TenantedOrUntenanted")
$putTo = ($octopusURL, "/api/machines/", $machine.Id , "?apikey=", $apikey -join "")
$putresult = Invoke-RestMethod -uri $putTo -Method PUT -Body ($machine |convertto-json -depth 5)

I hope this helps! Don’t hesitate to reach out if you have any further questions moving forward!

Best regards,

Kenny

Worked like a charm! Thanks!

Hi Austin,

That’s great to hear! Thanks for letting me know, and don’t hesitate to reach out if you come across any questions in the future. :slight_smile:

Best regards,

Kenny

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.