Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions REST/PowerShell/Targets/UpdateMachinePolicy.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
$ErrorActionPreference = "Stop";

# Define working variables
$octopusURL = "https://mysite.octopus.app"
$octopusAPIKey = "API-MYAPIKEY"
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
$spaceName = "default"
$machineNames = @("machine1", "machine2", "machine3")
$machinePolicyName = "mypolicyname"

# Get space
$space = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/spaces/all" -Headers $header) | Where-Object {$_.Name -eq $spaceName}

# Get all machines
$allMachines = Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/machines/all" -Headers $header

# Get specified machine policy
$machinePolicy = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/machinepolicies/all" -Headers $header) | Where-Object { $_.Name -eq $machinePolicyName }

# Update each machine
foreach ($machineName in $machineNames) {
$machine = $allMachines | Where-Object {$_.Name -eq $machineName}

if ($null -eq $machine) {
Write-Warning "Machine not found: $machineName — skipping"
continue
}

Write-Host "Updating machine policy for: $machineName"
$machine.MachinePolicyId = $machinePolicy.Id
Invoke-RestMethod -Method Put -Uri "$octopusURL/api/$($space.Id)/machines/$($machine.Id)" -Body ($machine | ConvertTo-Json -Depth 10) -Headers $header
}