Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@RichieBzzzt
Created September 14, 2017 14:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichieBzzzt/381a21b177199b25ed0018b7c6fa545f to your computer and use it in GitHub Desktop.
Save RichieBzzzt/381a21b177199b25ed0018b7c6fa545f to your computer and use it in GitHub Desktop.
$OctopusURL = "" #Octopus URL
$APIKey = "" #Octopus APIKey
$projectname = "" #Name of the project
$nuGetPath = ""
$pkdex = Import-Csv ""
if (-not (Test-Path $NugetPath)) {
Write-Error "Cannot find nuget at path $NugetPath\nuget.exe"
throw
}
$NugetExe = "$NugetPath\nuget.exe"
$nugetInstallOctopusClient = "&$NugetExe install Octopus.Client -ExcludeVersion -OutputDirectory $psscriptRoot"
Write-Output $nugetInstallOctopusClient
Invoke-Expression $nugetInstallOctopusClient
$octopusClientFolder = "$psscriptRoot\Octopus.Client\lib\net45"
if (-not (Test-Path $octopusClientFolder)) {
Throw "It appears that the nuget install hasn't worked for Octopus.Client, check output above to see whats going on"
}
$nugetInstallNewtonsoftJson = "&$NugetExe install Newtonsoft.Json -ExcludeVersion -OutputDirectory $psscriptRoot"
Write-Output $nugetInstallNewtonsoftJson
Invoke-Expression $nugetInstallNewtonsoftJson
$netwonSoftClientFolder = "$psscriptRoot\Newtonsoft.Json\lib\net45"
if (-not (Test-Path $netwonSoftClientFolder)) {
Throw "It appears that the nuget install hasn't worked for NetwonSoft.Json, check output above to see whats going on"
}
Add-Type -Path "$netwonSoftClientFolder\Newtonsoft.Json.dll"
Add-Type -Path "$octopusClientFolder\Octopus.Client.dll"
$endpoint = new-object Octopus.Client.OctopusServerEndpoint ($OctopusURL, $APIKey)
$c = new-object Octopus.Client.OctopusRepository $endpoint
Write-Output "Locating project $projectname. This may take some time..."
$project = $c.Projects.FindByName($projectname)
if ($null -eq $project.Name) {
Write-Error "Project $projectname not found!"
throw
}
Write-Output "Found project $($project.Name)"
$variableSet = $c.VariableSets.Get($project.links.variables)
foreach ($pkmn in $pkdex) {
if ($variableSet.Variables.Name.Contains($pkmn.Name) -eq $true) {
Write-Output "Variable$($pkmn.Name) exists. Checking if value matches..."
$variable = $variableSet.Variables | Where-Object {$_.Name -eq $pkmn.Name}
if ($variable.Value -eq $pkmn.Value) {
Write-Output "Value of $($pkmn.Name) matches..."
}
else {
Write-Output "Variable $($pkmn.Name) exists but value different. Updating to match value stored in csv..."
$variable.Value = $($pkmn.Value)
}
}
else {
$variable = New-Object Octopus.Client.Model.VariableResource
$variable.name = $($pkmn.Name)
$variable.Value = $($pkmn.Value)
$variable.IsSensitive = $false
$variableSet.Variables.Add($variable)
Write-Output "Adding var $($variable.name) to variable set. "
}
try {
if ($pkmn.EnvironmentScope) {
Write-Output "$($variable.name) has environment scoping. Updating to match value stored in csv. This may take some time..."
$environments = $pkmn.EnvironmentScope -Split ","
$octopusEnvironment = $c.Environments.FindByNames($environments)
$variable.Scope.Add([Octopus.Client.Model.Scopefield]::Environment, (New-Object Octopus.Client.Model.ScopeValue($octopusEnvironment.Id)))
}
}
catch {
Out-Null
}
}
$variableSet = $c.VariableSets.Modify($variableSet)
We can make this file beautiful and searchable if this error is corrected: Any value after quoted field isn't allowed in line 5.
Name, Value, EnvironmentScope
myFirstVar, 5, Dev
mySecondVar,4,"Dev, Test"
myThirdVar, 5, PreProd
myFourthVar, 10, "Dev, PreProd"
myFifthVar, 34, "Test, Dev"
mySixthhVar, 34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment