Bulk Edit Machines to change the Machine Policy

How can we edit the existing/registered machines to use another machine policy than the default one?
We have 150+ machines and I am sure there is a way to edit them in bulk instead of one by one.

Thanks

Hi Sercan,

Thanks for getting in touch.

While there is no way to bulk edit machines via the UI, you can easily achieve this using the API via the Octopus Client.

After backing up your database, you could run a script like this in PowerShell:

# You can get this dll from your Octopus Server/Tentacle installation directory or from
# https://www.nuget.org/packages/Octopus.Client/
Add-Type -Path 'C:\Development\OctopusDeploy\source\Octopus.Client\bin\Octopus.Client.dll' 

$apikey = 'API-1234' # Get this from your profile
$octopusURI = 'http://your-octopus-server' # Your Octopus Server address
$endpoint = New-Object Octopus.Client.OctopusServerEndpoint $octopusURI,$apikey
$repository = New-Object Octopus.Client.OctopusRepository $endpoint

$destinationMachinePolicyName = "Default Machine Policy" # Change this to whichever machine policy you wish to use.

# Get the ID of the machine policy we're trying to set our machines to.
$destinationMachinePolicy = $repository.MachinePolicies.FindByName($destinationMachinePolicyName)
if (!$destinationMachinePolicy) {
    Write-Host("Failed to find machine policy with name $($destinationMachinePolicyName). Please set a valid machine policy name.")
    Exit
}

# Find all machines.
$machines = $repository.Machines.FindAll()

# Update each machine's machine policy.
Foreach ($machine in $machines)
{
    # Only update machines that currently have a machine policy assigned.
    if ($machine.MachinePolicyId) {
        $machine.MachinePolicyId = $destinationMachinePolicy.Id
        $repository.Machines.Modify($machine)
        Write-Host("Updated machine policy for $($machine.Name) to $($destinationMachinePolicyName).")
    }
}

Hope this helps

Cheers
Mark