Skip to content

Instantly share code, notes, and snippets.

@Dalmirog-zz
Last active September 6, 2016 15:23
Show Gist options
  • Save Dalmirog-zz/21f131d8d7e055c54ff1c0614ab6da25 to your computer and use it in GitHub Desktop.
Save Dalmirog-zz/21f131d8d7e055c54ff1c0614ab6da25 to your computer and use it in GitHub Desktop.
get machine names using the roles of the current step
<#
This script should run from:
- A script step
- That's executed on the Octopus Server
- With a Window size of 1
It'll create an output variable called "MachineNames" Which will have the names of the machines (in octopus, so not the same as $env:computername).
To learn more about the usage of output variables read http://octopusdeploy.com/blog/fun-with-output-variables
#>
##CONFIG##
$OctopusAPIkey = "" #API Key to authenticate in Octopus.
##PROCESS##
$Roles = ($OctopusParameters['Octopus.Action[Get Machines].TargetRoles']).split(',')
$OctopusURL = $OctopusParameters['Octopus.Web.BaseUrl']
$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey }
$MachineIDs = @()
Foreach ($Role in $Roles){
If(!([string]::IsNullOrEmpty($OctopusParameters["Octopus.Environment.MachinesInRole[$Role]"])))
{
$MachineIDs += ($OctopusParameters["Octopus.Environment.MachinesInRole[$Role]"]).Split(',')
}
}
$machineNamesArray = @()
foreach ($Id in $MachineIDs){
$MachineNamesArray += ((Invoke-WebRequest $OctopusURL/api/machines/$id -Headers $header -Method Get).content | ConvertFrom-Json | select -ExpandProperty Name)
}
$MachineNamesString = $machineNamesArray -join ","
#Creating the Output variable
Set-OctopusVariable -name "MachineNames" -value $MachineNamesString
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment