Skip to content

Instantly share code, notes, and snippets.

@Dalmirog-zz
Created August 26, 2015 21:09
Show Gist options
  • Select an option

  • Save Dalmirog-zz/9167bd9ca7617088623a to your computer and use it in GitHub Desktop.

Select an option

Save Dalmirog-zz/9167bd9ca7617088623a to your computer and use it in GitHub Desktop.
Removes specific Variable Sets or Script modules from all/some projects in Octopus. Octoposh Module is required
Import-Module Octoposh #You can get Octoposh from http://dalmirog.github.io/OctoPosh
$VariableSetName = "" #variable set NAME, not ID
$ScriptModuleName = "Test" #Script module NAME, not ID
$ProjectList = ""#leave empty if you want to check all the project in your Octopus instance. Else add names between double quotes separated by a comma
$VariableSet = Get-OctopusVariableSet -LibrarySetName $VariableSetName -ResourceOnly
$ScriptModule = Get-OctopusVariableSet -LibrarySetName $ScriptModuleName -ResourceOnly
$c = New-OctopusConnection
If([string]::IsNullOrEmpty($ProjectList)){
$projects = Get-OctopusProject -ResourceOnly
}
else{
$projects = Get-OctopusProject -ProjectName $ProjectList -ResourceOnly
}
Foreach($project in $projects){
$update = $false
Write-Output "Checking project: [$($project.name)]"
If($project.IncludedLibraryVariableSetIds.Contains($VariableSet.OwnerId)){
Write-Output "--Project [$($project.name)] contains Variable Set [$VariableSetName]. Removing it..."
If($project.IncludedLibraryVariableSetIds.Remove($VariableSet.OwnerId)){
$update = $true
}
}
else{
Write-Output "--Project [$($project.name)] does not contain Variable Set [$VariableSetName]."
}
if($project.IncludedLibraryVariableSetIds.Contains($ScriptModule.OwnerId)){
Write-Output "--Project [$($project.name)] contains ScriptModule [$ScriptModuleName]. Removing it..."
If($project.IncludedLibraryVariableSetIds.Remove($ScriptModule.OwnerId)){
$update = $true
}
}
else{
Write-Output "--Project [$($project.name)] does not contain the Script Module [$ScriptModuleName]."
}
If ($update -eq $true){
Write-Output "--Saving changes on Project [$($project.name)]"
If(Update-OctopusResource -Resource $project -Force){
Write-Output "--Changes on Project [$($project.name)] saved to the database"
}
else{
write-output "--Something went wrong saving project [$($project.name)]. Please try to make the changes manually from the web UI"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment