Last active
August 29, 2018 21:45
-
-
Save inammathe/81418a00dd9bea4b31c3edd78a54ade5 to your computer and use it in GitHub Desktop.
Copies from one Octopus Deploy project variables to another
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function Copy-OctopusVariableSet | |
{ | |
<# | |
.SYNOPSIS | |
Copies from one Octopus Deploy project variables to another | |
.DESCRIPTION | |
Uses the Octopus rest api to get all variables from one project to replace all variables in another. | |
Super handy if you need to get a new project going with almost identical variables that don't yet exist in a project template or library set | |
.EXAMPLE | |
PS C:\> Copy-OctopusVariableSet ` | |
-OctopusUrl 'https://octopus.contoso.com' ` | |
-OctopusAPIKey 'API-N6DHEJYITJWTCAHOAZ2KSUJE' ` | |
-CopyFromId 'variableset-Projects-01' ` | |
-CopyToId 'variableset-Projects-02' | |
Copies all variables from variableset-Projects-01 and replaces those in variableset-Projects-02 | |
.EXAMPLE | |
PS C:\> @('variableset-Projects-02', 'variableset-Projects-03', 'variableset-Projects-04') | Copy-OctopusVariableSet ` | |
-OctopusUrl 'https://octopus.contoso.com' ` | |
-OctopusAPIKey 'API-N6DHEJYITJWTCAHOAZ2KSUJE' ` | |
-CopyFromId 'variableset-Projects-01' ` | |
Copies all variables from variableset-Projects-01 and replaces those in 'variableset-Projects-02', 'variableset-Projects-03', 'variableset-Projects-04' | |
#> | |
[cmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')] | |
param | |
( | |
# your Octopus URL e.g. https://octopus.contoso.com | |
[Parameter(Mandatory)] | |
[string] | |
$OctopusUrl, | |
# your personal or service account API key e.g. API-N6DHEJYITJWTCAHOAZ2KSUJE | |
[Parameter(Mandatory)] | |
[string] | |
$OctopusAPIKey, | |
# variable set ID to copy from e.g. variableset-Projects-01 | |
[Parameter(Mandatory)] | |
[string] | |
$CopyFromId, | |
# variable set ID to copy to e.g. variableset-Projects-02 | |
[Parameter(Mandatory, ValueFromPipeline, ValueFromPipelineByPropertyName)] | |
[string[]] | |
$CopyToId, | |
# maximum depth in the returned variableset object to convert to JSON | |
[Parameter(Mandatory=$false)] | |
[int] | |
$Depth = 99 | |
) | |
Begin{ | |
$reqHeaders = @{ "X-Octopus-ApiKey" = $OctopusAPIKey } | |
# Get the CopyFrom project variable set object | |
$CopyFromProjectVarSet = Invoke-RestMethod "$OctopusURL/api/variables/$CopyFromId" -Headers $reqHeaders -Method GET | |
} | |
Process | |
{ | |
if ($pscmdlet.ShouldProcess($CopyToId, "Copies all project variables from '$OctopusURL/api/variables/$CopyFromId' to replace all variables in")) | |
{ | |
foreach ($id in $CopyToId) { | |
# Get the CopyTo project variable set object | |
$CopyToProjectVarSet = Invoke-RestMethod "$OctopusURL/api/variables/$id" -Headers $reqHeaders -Method GET | |
# Copy the variables from the CopyFrom project variable set object | |
$CopyToProjectVarSet.Variables = $CopyFromProjectVarSet.Variables | |
# Update the resource with the new variables | |
Invoke-RestMethod "$OctopusURL/api/variables/$id" -Headers $reqHeaders -Method PUT -Body ($CopyToProjectVarSet | ConvertTo-JSON -Depth $Depth) -ContentType "application/json" | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment