From a7c082afec0984c1f73e0273e0e906acf80cc745 Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Tue, 23 May 2023 13:18:23 +0100 Subject: [PATCH 01/20] Update CreateProject.ps1 Updated with: - Prompted parameters - Informational Try/Catch - Existing Project name awareness --- REST/PowerShell/Projects/CreateProject.ps1 | 97 +++++++++++++++++----- 1 file changed, 74 insertions(+), 23 deletions(-) diff --git a/REST/PowerShell/Projects/CreateProject.ps1 b/REST/PowerShell/Projects/CreateProject.ps1 index 25d1ab17..43c33ef0 100644 --- a/REST/PowerShell/Projects/CreateProject.ps1 +++ b/REST/PowerShell/Projects/CreateProject.ps1 @@ -1,31 +1,82 @@ +# ================================================================================================================== +# This script creates a standard Project on the specified Octopus Server. +# To make the new Project Git-enabled (config-as-code), navigate to the new Project URL > Settings > Version Control +# ================================================================================================================== + $ErrorActionPreference = "Stop"; -# Define working variables -$octopusURL = "https://youroctourl" -$octopusAPIKey = "API-YOURAPIKEY" -$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey } -$spaceName = "default" -$projectName = "MyProject" -$projectDescription = "MyDescription" -$projectGroupName = "Default project group" -$lifecycleName = "Default lifecycle" +# ====== BYPASS PROMPTS? ====== +$BypassPrompts = $true # Set to $true if you wish to predefine your parameters + -# Get space -$space = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/spaces/all" -Headers $header) | Where-Object {$_.Name -eq $spaceName} +# ====== PARAMETERS ====== +If ($BypassPrompts -eq $true) { + + # === Predefined Parameters (Optional) === + $OctopusURL = "http://YOUR_OCTOPUS_URL.bla" + $OctopusAPIKey = "API-XXXXXXXXXXXXXXXXXX" + $SpaceId = "Spaces-XX" + $ProjectName = "My Project" + $ProjectDescription = "My Description" + $ProjectGroupName = "Default Project Group" + $LifecycleName = "Default Lifecycle" +} -# Get project group -$projectGroup = (Invoke-RestMethod -Method Get "$octopusURL/api/$($space.Id)/projectgroups/all" -Headers $header) | Where-Object {$_.Name -eq $projectGroupName} +If ($BypassPrompts -eq $false) { + + # === Prompted Parameters === + $OctopusAPIKey = (Read-Host "Enter your Octopus API key (example: `"API-XXXXXXXXXXXXXXXXXX`")").trim('"') + $OctopusURL = (Read-Host "Enter your Octopus Instance URL with no trailing slash (i.e. `"http://YOUR_OCTOPUS_URL.bla`")").trim('"') + $SpaceId = (Read-Host "Enter the SpaceId where the Library Variable Set resides (example: `"Spaces-1`")").trim('"') + $ProjectName = (Read-Host "Enter a name for your new Project (example: `"My Project`")").trim('"') + $ProjectDescription = (Read-Host "Enter a description for your new Project (example: `"My Description`")").trim('"') + $ProjectGroupName = (Read-Host "Enter the name of an existing Project Group in `"$($SpaceId)`" for your new Project (example: `"Default Project Group`")").trim('"') + $LifecycleName = (Read-Host "Enter the name of an existing Lifecycle in `"$($SpaceId)`" for your new Project (example: `"Default Lifecycle`")").trim('"') +} -# Get Lifecycle -$lifeCycle = (Invoke-RestMethod -Method Get "$octopusURL/api/$($space.Id)/lifecycles/all" -Headers $header) | Where-Object {$_.Name -eq $lifecycleName} +$Header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey } -# Create project json payload -$jsonPayload = @{ - Name = $projectName - Description = $projectDescription - ProjectGroupId = $projectGroup.Id - LifeCycleId = $lifeCycle.Id + +# ====== SCRIPT BODY ====== +# Try to GET the ProjectGroupId for $ProjectGroupName +Try { + $ProjectGroup = (Invoke-RestMethod -Method Get "$($OctopusURL)/api/$($SpaceId)/ProjectGroups/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectGroupName} + If (!$ProjectGroup) {throw} +} +Catch { + Write-Warning "Unable to find a ProjectGroupId for the Project Group Name `"$($ProjectGroupName)`" via `"$($OctopusURL)/api/$($SpaceId)/ProjectGroups/all`"" + Write-Warning "Check your parameters (Octopus API key, URL, SpaceId, etc.), ensure your API key has sufficient permissions, and the Octopus Server is accessible from this machine." + break } -# Create project -Invoke-RestMethod -Method Post -Uri "$octopusURL/api/$($space.Id)/projects" -Body ($jsonPayload | ConvertTo-Json -Depth 10) -Headers $header \ No newline at end of file +# Try to GET the LifecycleId for $LifecycleName +Try { + $Lifecycle = (Invoke-RestMethod -Method Get "$($OctopusURL)/api/$($SpaceId)/Lifecycles/all" -Headers $Header) | Where-Object {$_.Name -eq $LifecycleName} + If (!$Lifecycle) {throw} +} +Catch { + Write-Warning "Unable to find a LifecycleId for the Lifecycle Name `"$($LifecycleName)`" via `"$($OctopusURL)/api/$($SpaceId)/Lifecycles/all`"" + Write-Warning "Check your parameters (Octopus API key, URL, SpaceId, etc.), ensure your API key has sufficient permissions, and the Octopus Server is accessible from this machine." + break +} + +# Create Json payload for new Project creation +$JsonPayload = @{ + Name = $ProjectName + Description = $ProjectDescription + ProjectGroupId = $ProjectGroup.Id + LifecycleId = $Lifecycle.Id +} + +# Create Project using $JsonPayload +Try { + $CheckProjName = (Invoke-RestMethod -Method Get -Uri "$($OctopusURL)/api/$($SpaceId)/projects/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectName} + If ($CheckProjName) {throw} + Else { + $NewProject = Invoke-RestMethod -Method Post -Uri "$($OctopusURL)/api/$($SpaceId)/projects" -Body ($JsonPayload | ConvertTo-Json -Depth 10) -Headers $Header + Write-Host "You may view your new Project at: $($OctopusURL)$($NewProject.Links.Self)" + } +} +Catch { + Write-Warning "A Project with the name `"$($ProjectName)`" already exists in `"$($SpaceId)`". Please choose a Project Name that does not exist in this Space." +} From d9059fb559ec73b0bce0e6887d4ec8dceb0706b7 Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Tue, 23 May 2023 14:19:31 +0100 Subject: [PATCH 02/20] Update CreateProject.ps1 --- REST/PowerShell/Projects/CreateProject.ps1 | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/REST/PowerShell/Projects/CreateProject.ps1 b/REST/PowerShell/Projects/CreateProject.ps1 index 43c33ef0..e36bfd32 100644 --- a/REST/PowerShell/Projects/CreateProject.ps1 +++ b/REST/PowerShell/Projects/CreateProject.ps1 @@ -40,7 +40,7 @@ $Header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey } # ====== SCRIPT BODY ====== # Try to GET the ProjectGroupId for $ProjectGroupName Try { - $ProjectGroup = (Invoke-RestMethod -Method Get "$($OctopusURL)/api/$($SpaceId)/ProjectGroups/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectGroupName} + $ProjectGroup = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)/ProjectGroups/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectGroupName} If (!$ProjectGroup) {throw} } Catch { @@ -51,7 +51,7 @@ Catch { # Try to GET the LifecycleId for $LifecycleName Try { - $Lifecycle = (Invoke-RestMethod -Method Get "$($OctopusURL)/api/$($SpaceId)/Lifecycles/all" -Headers $Header) | Where-Object {$_.Name -eq $LifecycleName} + $Lifecycle = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)/Lifecycles/all" -Headers $Header) | Where-Object {$_.Name -eq $LifecycleName} If (!$Lifecycle) {throw} } Catch { @@ -70,10 +70,10 @@ $JsonPayload = @{ # Create Project using $JsonPayload Try { - $CheckProjName = (Invoke-RestMethod -Method Get -Uri "$($OctopusURL)/api/$($SpaceId)/projects/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectName} + $CheckProjName = (Invoke-RestMethod -Method GET -Uri "$($OctopusURL)/api/$($SpaceId)/projects/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectName} If ($CheckProjName) {throw} Else { - $NewProject = Invoke-RestMethod -Method Post -Uri "$($OctopusURL)/api/$($SpaceId)/projects" -Body ($JsonPayload | ConvertTo-Json -Depth 10) -Headers $Header + $NewProject = Invoke-RestMethod -Method POST -Uri "$($OctopusURL)/api/$($SpaceId)/projects" -Body ($JsonPayload | ConvertTo-Json -Depth 10) -Headers $Header Write-Host "You may view your new Project at: $($OctopusURL)$($NewProject.Links.Self)" } } From 4738e08ad6757ede035e11e2e95a1d74f583eaf8 Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Tue, 23 May 2023 14:53:42 +0100 Subject: [PATCH 03/20] Update CreateProject.ps1 --- REST/PowerShell/Projects/CreateProject.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/REST/PowerShell/Projects/CreateProject.ps1 b/REST/PowerShell/Projects/CreateProject.ps1 index e36bfd32..7921aeb1 100644 --- a/REST/PowerShell/Projects/CreateProject.ps1 +++ b/REST/PowerShell/Projects/CreateProject.ps1 @@ -6,7 +6,7 @@ $ErrorActionPreference = "Stop"; # ====== BYPASS PROMPTS? ====== -$BypassPrompts = $true # Set to $true if you wish to predefine your parameters +$BypassPrompts = $false # Set to $true if you wish to predefine your parameters # ====== PARAMETERS ====== From fad42d9ac312c906c92c2a770552e25dd59f2574 Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Tue, 23 May 2023 16:40:11 +0100 Subject: [PATCH 04/20] Update and rename DisableProject.ps1 to DisableOrEnableProject.ps1 Updated with: - Prompted parameters - Informational Try/Catch - Current Project enabled/disabled status awareness --- .../Projects/DisableOrEnableProject.ps1 | 83 +++++++++++++++++++ REST/PowerShell/Projects/DisableProject.ps1 | 23 ----- 2 files changed, 83 insertions(+), 23 deletions(-) create mode 100644 REST/PowerShell/Projects/DisableOrEnableProject.ps1 delete mode 100644 REST/PowerShell/Projects/DisableProject.ps1 diff --git a/REST/PowerShell/Projects/DisableOrEnableProject.ps1 b/REST/PowerShell/Projects/DisableOrEnableProject.ps1 new file mode 100644 index 00000000..f5b269e0 --- /dev/null +++ b/REST/PowerShell/Projects/DisableOrEnableProject.ps1 @@ -0,0 +1,83 @@ +# ================================================================================================================== +# This script disables or enables a Project on the specified Octopus Server. +# ================================================================================================================== + +$ErrorActionPreference = "Stop"; + +# ====== BYPASS PROMPTS? ====== +$BypassPrompts = $false # Set to $true if you wish to predefine your parameters + + +# ====== PARAMETERS ====== +If ($BypassPrompts -eq $true) { + + # === Predefined Parameters (Optional) === + $OctopusURL = "http://YOUR_OCTOPUS_URL.bla" + $OctopusAPIKey = "API-XXXXXXXXXXXXXXXXXX" + $SpaceId = "Spaces-XX" + $ProjectName = "My Project" + $ProjectEnabled = $false +} + +If ($BypassPrompts -eq $false) { + + # === Prompted Parameters === + $OctopusAPIKey = (Read-Host "Enter your Octopus API key (example: `"API-XXXXXXXXXXXXXXXXXX`")").trim('"') + $OctopusURL = (Read-Host "Enter your Octopus Instance URL with no trailing slash (i.e. `"http://YOUR_OCTOPUS_URL.bla`")").trim('"') + $SpaceId = (Read-Host "Enter the SpaceId where the Library Variable Set resides (example: `"Spaces-1`")").trim('"') + $ProjectName = (Read-Host "Enter a name for your new Project (example: `"My Project`")").trim('"') + $ProjectStatusPrompt = (Read-Host "Type `"D`" to Disable or `"E`" to Enable `"$($ProjectName)`"").trim('"') + While (($ProjectStatusPrompt -ne "D") -and ($ProjectStatusPrompt -ne "E")) { + $ProjectStatusPrompt = (Read-Host "Type `"D`" to Disable or `"E`" to Enable `"$($ProjectName)`"").trim('"') + } + If ($ProjectStatusPrompt -eq "D") {$ProjectEnabled = $false} + If ($ProjectStatusPrompt -eq "E") {$ProjectEnabled = $true} +} + +$Header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey } + + +# ====== SCRIPT BODY ====== +# Try to GET the ProjectId for $ProjectName +Try { + $Project = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)/Projects/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectName} + If (!$Project) {throw} +} +Catch { + Write-Warning "Unable to find a ProjectId for the Project Name `"$($ProjectName)`" via `"$($OctopusURL)/api/$($SpaceId)/Projects/all`"" + Write-Warning "Check your parameters (Octopus API key, URL, SpaceId, etc.), ensure your API key has sufficient permissions, and the Octopus Server is accessible from this machine." + break +} + +# Check if Project is already Disabled/Enabled +If ($Project.IsDisabled -eq !$ProjectEnabled) { + If ($Project.IsDisabled) { + Write-Host "`"$($ProjectName)`" ($($Project.Id)) already disabled! No action required!" + break + } + If (!$Project.IsDisabled) { + Write-Host "`"$($ProjectName)`" ($($Project.Id)) already enabled! No action required!" + break + } +} + +# Disable/Enable Project +$Project.IsDisabled = !$ProjectEnabled +If ($ProjectEnabled -eq $false) { + Write-Host "Disabling `"$($ProjectName)`" ($($Project.Id))" +} +If ($ProjectEnabled -eq $true) { + Write-Host "Enabling `"$($ProjectName)`" ($($Project.Id))" +} +# Save Project changes +Try { + $SaveProject = Invoke-RestMethod -Method PUT -Uri "$($OctopusURL)/api/$($SpaceId)/Projects/$($Project.Id)" -Headers $Header -Body ($Project | ConvertTo-Json -Depth 10) +} +Catch { + Write-Warning "Something went wrong when attempting a PUT via `"$($OctopusURL)/api/$($SpaceId)/Projects/$($Project.Id)`"." +} + +$ProjectCheck = Invoke-RestMethod -Method GET -Uri "$($OctopusURL)/api/$($SpaceId)/Projects/$($Project.Id)" -Headers $Header +If ($ProjectCheck.IsDisabled -eq !$ProjectEnabled) { + Write-Host "Success!" +} diff --git a/REST/PowerShell/Projects/DisableProject.ps1 b/REST/PowerShell/Projects/DisableProject.ps1 deleted file mode 100644 index c0405e4d..00000000 --- a/REST/PowerShell/Projects/DisableProject.ps1 +++ /dev/null @@ -1,23 +0,0 @@ -$ErrorActionPreference = "Stop"; - -# Define working variables -$octopusURL = "https://your.octopus.app" -$octopusAPIKey = "API-YOURAPIKEY" -$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey } -$spaceName = "Default" -$projectName = "MyProject" -$projectEnabled = $False - -# Get space -$spaces = Invoke-RestMethod -Uri "$octopusURL/api/spaces?partialName=$([uri]::EscapeDataString($spaceName))&skip=0&take=100" -Headers $header -$space = $spaces.Items | Where-Object { $_.Name -eq $spaceName } - -# Get project -$projects = Invoke-RestMethod -Uri "$octopusURL/api/$($space.Id)/projects?partialName=$([uri]::EscapeDataString($projectName))&skip=0&take=100" -Headers $header -$project = $projects.Items | Where-Object { $_.Name -eq $projectName } - -# Enable/Disable project -$project.IsDisabled = !$projectEnabled - -# Save project changes -Invoke-RestMethod -Method Put -Uri "$octopusURL/api/$($space.Id)/projects/$($project.Id)" -Headers $header -Body ($project | ConvertTo-Json -Depth 10) \ No newline at end of file From 00f905d0ab16e5c52df6a1559c75d707a4bd7468 Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Tue, 23 May 2023 16:44:56 +0100 Subject: [PATCH 05/20] Update DisableOrEnableProject.ps1 --- REST/PowerShell/Projects/DisableOrEnableProject.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/REST/PowerShell/Projects/DisableOrEnableProject.ps1 b/REST/PowerShell/Projects/DisableOrEnableProject.ps1 index f5b269e0..40e4a81a 100644 --- a/REST/PowerShell/Projects/DisableOrEnableProject.ps1 +++ b/REST/PowerShell/Projects/DisableOrEnableProject.ps1 @@ -9,7 +9,7 @@ $BypassPrompts = $false # Set to $true if you wish to predefine your parameters # ====== PARAMETERS ====== -If ($BypassPrompts -eq $true) { +If ($BypassPrompts) { # === Predefined Parameters (Optional) === $OctopusURL = "http://YOUR_OCTOPUS_URL.bla" @@ -19,7 +19,7 @@ If ($BypassPrompts -eq $true) { $ProjectEnabled = $false } -If ($BypassPrompts -eq $false) { +If (!$BypassPrompts) { # === Prompted Parameters === $OctopusAPIKey = (Read-Host "Enter your Octopus API key (example: `"API-XXXXXXXXXXXXXXXXXX`")").trim('"') From 6185fe7de40df7f64beed26d921767463f59615c Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Tue, 23 May 2023 16:45:52 +0100 Subject: [PATCH 06/20] Update CreateProject.ps1 --- REST/PowerShell/Projects/CreateProject.ps1 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/REST/PowerShell/Projects/CreateProject.ps1 b/REST/PowerShell/Projects/CreateProject.ps1 index 7921aeb1..7ee5f00a 100644 --- a/REST/PowerShell/Projects/CreateProject.ps1 +++ b/REST/PowerShell/Projects/CreateProject.ps1 @@ -6,11 +6,11 @@ $ErrorActionPreference = "Stop"; # ====== BYPASS PROMPTS? ====== -$BypassPrompts = $false # Set to $true if you wish to predefine your parameters +$BypassPrompts = $true # Set to $true if you wish to predefine your parameters # ====== PARAMETERS ====== -If ($BypassPrompts -eq $true) { +If ($BypassPrompts) { # === Predefined Parameters (Optional) === $OctopusURL = "http://YOUR_OCTOPUS_URL.bla" @@ -22,7 +22,7 @@ If ($BypassPrompts -eq $true) { $LifecycleName = "Default Lifecycle" } -If ($BypassPrompts -eq $false) { +If (!$BypassPrompts) { # === Prompted Parameters === $OctopusAPIKey = (Read-Host "Enter your Octopus API key (example: `"API-XXXXXXXXXXXXXXXXXX`")").trim('"') From 43d99f21c087fd5d24c3d59253239d07f323a9d1 Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Tue, 23 May 2023 17:41:02 +0100 Subject: [PATCH 07/20] Update DeleteProjectByName.ps1 Updated with: - Prompted parameters - Informational Try/Catch - Confirmation before Project deletion when prompts are enabled --- .../Projects/DeleteProjectByName.ps1 | 79 ++++++++++++++++--- 1 file changed, 66 insertions(+), 13 deletions(-) diff --git a/REST/PowerShell/Projects/DeleteProjectByName.ps1 b/REST/PowerShell/Projects/DeleteProjectByName.ps1 index 26ec21d3..2c240a18 100644 --- a/REST/PowerShell/Projects/DeleteProjectByName.ps1 +++ b/REST/PowerShell/Projects/DeleteProjectByName.ps1 @@ -1,17 +1,70 @@ -$ErrorActionPreference = "Stop"; +# ================================================================================================================== +# This script deletes a Project on the specified Octopus Server. +# ================================================================================================================== -# Define working variables -$octopusURL = "https://youroctourl" -$octopusAPIKey = "API-YOURAPIKEY" -$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey } -$spaceName = "Default" -$projectName = "MyProject" +$ErrorActionPreference = "Stop"; -# Get space -$space = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/spaces/all" -Headers $header) | Where-Object {$_.Name -eq $spaceName} +# ====== BYPASS PROMPTS? ====== +$BypassPrompts = $false # Set to $true if you wish to predefine your parameters -# Get project -$project = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/projects/all" -Headers $header) | Where-Object {$_.Name -eq $projectName} -# Delete project -Invoke-RestMethod -Method Delete -Uri "$octopusURL/api/$($space.Id)/projects/$($project.Id)" -Headers $header \ No newline at end of file +# ====== PARAMETERS ====== +If ($BypassPrompts) { + + # === Predefined Parameters (Optional) === + $OctopusURL = "http://YOUR_OCTOPUS_URL.bla" + $OctopusAPIKey = "API-XXXXXXXXXXXXXXXXXX" + $SpaceId = "Spaces-XX" + $ProjectName = "My Project" +} + +If (!$BypassPrompts) { + + # === Prompted Parameters === + $OctopusAPIKey = (Read-Host "Enter your Octopus API key (example: `"API-XXXXXXXXXXXXXXXXXX`")").trim('"') + $OctopusURL = (Read-Host "Enter your Octopus Instance URL with no trailing slash (i.e. `"http://YOUR_OCTOPUS_URL.bla`")").trim('"') + $SpaceId = (Read-Host "Enter the SpaceId where the Library Variable Set resides (example: `"Spaces-1`")").trim('"') + $ProjectName = (Read-Host "Enter a name for your new Project (example: `"My Project`")").trim('"') +} + +$Header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey } + +# ====== SCRIPT BODY ====== +# Try to GET the ProjectId for $ProjectName +Try { + $Project = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)/Projects/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectName} + If (!$Project) {throw} +} +Catch { + Write-Warning "Unable to find a ProjectId for the Project Name `"$($ProjectName)`" via `"$($OctopusURL)/api/$($SpaceId)/Projects/all`"" + Write-Warning "Check your parameters (Octopus API key, URL, SpaceId, etc.), ensure your API key has sufficient permissions, and the Octopus Server is accessible from this machine." + break +} + +# Confirmation (ignored if $BypassPrompts = $true) +If (!$BypassPrompts) { + Write-Host "==================================================================================================================================" + $Confirm = (Read-Host "Are you sure you want to DELETE the Project `"$ProjectName`" ($($Project.Id))? This cannot be undone. (Type Y to continue or N to quit)").trim('"') + While (($Confirm -ne "N") -and ($Confirm -ne "Y")) { + $Confirm = (Read-Host "Are you sure you want to DELETE the Project `"$ProjectName`" ($($Project.Id))? This cannot be undone. (Type Y to continue or N to quit)").trim('"') + } + If ($Confirm -eq "N") { + Write-Warning "Aborted. No changes were made." + break + } +} + +# Delete Project +Invoke-RestMethod -Method DEL "$($OctopusURL)/api/$($SpaceId)/Projects/$($Project.Id)" -Headers $Header +Try { + $DeleteCheck = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)/Projects/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectName} + If (!$DeleteCheck) { + Write-Host "The Project named `"$($ProjectName)`" ($($Project.Id)) was DELETED." + } + If ($DeleteCheck) {throw} +} +Catch { + Write-Warning "Unable to DELETE the Project `"$($ProjectName)`" via `"$($OctopusURL)/api/$($SpaceId)/Projects/$($Project.Id)`"" + Write-Warning "Check your parameters (Octopus API key, URL, SpaceId, etc.), ensure your API key has sufficient permissions, and the Octopus Server is accessible from this machine." + break +} From bdc772b469e5389459fa2401972b79dbd17b2cc3 Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Wed, 24 May 2023 18:02:57 +0100 Subject: [PATCH 08/20] Update RenameProject.ps1 Updated with: - Prompted parameters - Informational Try/Catch - Existing Project name awareness - Slug support - Special character detection and optional removal --- REST/PowerShell/Projects/RenameProject.ps1 | 108 +++++++++++++++++---- 1 file changed, 91 insertions(+), 17 deletions(-) diff --git a/REST/PowerShell/Projects/RenameProject.ps1 b/REST/PowerShell/Projects/RenameProject.ps1 index 03e6fdbb..9b5f13a3 100644 --- a/REST/PowerShell/Projects/RenameProject.ps1 +++ b/REST/PowerShell/Projects/RenameProject.ps1 @@ -1,24 +1,98 @@ +# ================================================================================================================== +# This script renames a Project (and optionally a Project's slug) on the specified Octopus Server. +# ================================================================================================================== $ErrorActionPreference = "Stop"; -# Define working variables -$octopusURL = "https://your.octopus.app" -$octopusAPIKey = "API-YOURAPIKEY" -$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey } -$spaceName = "Default" -$projectName = "MyProject" -$newProjectName = "MyNewProjectName" +# ====== BYPASS PROMPTS? ====== +$BypassPrompts = $false # Set to $true if you wish to predefine your parameters -# Get space -$spaces = Invoke-RestMethod -Uri "$octopusURL/api/spaces?partialName=$([uri]::EscapeDataString($spaceName))&skip=0&take=100" -Headers $header -$space = $spaces.Items | Where-Object { $_.Name -eq $spaceName } -# Get project -$projects = Invoke-RestMethod -Uri "$octopusURL/api/$($space.Id)/projects?partialName=$([uri]::EscapeDataString($projectName))&skip=0&take=100" -Headers $header -$project = $projects.Items | Where-Object { $_.Name -eq $projectName } +# ====== PARAMETERS ====== +If ($BypassPrompts) { + + # === Predefined Parameters (Optional) === + $OctopusURL = "http://YOUR_OCTOPUS_URL.bla" + $OctopusAPIKey = "API-XXXXXXXXXXXXXXXXXX" + $SpaceId = "Spaces-XX" + $ProjectName = "My Project" + $NewProjectName = "My New Project Name" + $ChangeProjectSlug = $true +} -# Set project new name -$project.Name = $newProjectName +If (!$BypassPrompts) { + + # === Prompted Parameters === + $OctopusAPIKey = (Read-Host "Enter your Octopus API key (example: `"API-XXXXXXXXXXXXXXXXXX`")").trim('"') + $OctopusURL = (Read-Host "Enter your Octopus Instance URL with no trailing slash (i.e. `"http://YOUR_OCTOPUS_URL.bla`")").trim('"') + $SpaceId = (Read-Host "Enter the SpaceId where the Library Variable Set resides (example: `"Spaces-1`")").trim('"') + $ProjectName = (Read-Host "Enter the current Project name (example: `"My Project`")").trim('"') + $NewProjectName = (Read-Host "Enter the new Project name (example: `"My New Project Name`")").trim('"') + $ChangeProjectSlugPrompt = (Read-Host "Would you like to change the Project's Slug to match the new Project Name? (Type `"Y`" for yes or `"N`" for no)").trim('"') + While (($ChangeProjectSlugPrompt -ne "Y") -and ($ChangeProjectSlugPrompt -ne "N")) { + $ChangeProjectSlugPrompt = (Read-Host "Would you like to change the Project's Slug to match the new Project Name? (Type Y for yes or N for no)").trim('"') + } + If ($ChangeProjectSlugPrompt -eq "Y") {$ChangeProjectSlug = $true} + If ($ChangeProjectSlugPrompt -eq "N") {$ChangeProjectSlug = $false} +} -# Save project changes -Invoke-RestMethod -Method Put -Uri "$octopusURL/api/$($space.Id)/projects/$($project.Id)" -Headers $header -Body ($project | ConvertTo-Json -Depth 10) \ No newline at end of file +$Header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey } + +# Special character check +$CheckSpecialChar = $NewProjectName | Select-String '[^ !@#$%^&();`~,.+=\-\w]' -AllMatches | ForEach-Object { $_.Matches.Value } +If ($CheckSpecialChar) { + $PrintSpecialChars = $CheckSpecialChar -join ' ' + Write-Warning "The following special characters were detected and may cause this action to fail:" + Write-Host "$($PrintSpecialChars)" + If (!$BypassPrompts) { + Write-Host "" + $Confirm = (Read-Host "Type `"Y`" to continue as is, `"R`" to remove these characters and continue or `"N`" to quit)").trim('"') + While (($Confirm -ne "N") -and ($Confirm -ne "Y") -and ($Confirm -ne "R")) { + $Confirm = (Read-Host "Type `"Y`" to continue as is, `"R`" to remove these characters and continue or `"N`" to quit)").trim('"') + } + If ($Confirm -eq "N") { + Write-Warning "Aborted. No changes were made." + break + } + If ($Confirm -eq "R") { + Write-Host "Requested new Project name: $($NewProjectName)" + $NewProjectName = $NewProjectName -replace '[^ !@#$%^&();`~,.+=\-\w]','' + Write-Host ">Adjusted new Project name: $($NewProjectName)" + } + } +} + +# Try to GET the ProjectId for $ProjectName +Try { + $Project = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)/Projects/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectName} + If (!$Project) {throw} +} +Catch { + Write-Warning "Unable to find a ProjectId for the Project Name `"$($ProjectName)`" via `"$($OctopusURL)/api/$($SpaceId)/Projects/all`"" + Write-Warning "Check your parameters (Octopus API key, URL, SpaceId, etc.), ensure your API key has sufficient permissions, and the Octopus Server is accessible from this machine." + break +} + +# Confirm no Projects already exist in $SpaceId with $NewProjectName +$ProjectNameCheck = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)/Projects/all" -Headers $Header) | Where-Object {$_.Name -eq $NewProjectName} +If ($ProjectNameCheck) { + Write-Warning "A Project named `"$($NewProjectName)`" ($($ProjectNameCheck.Id)) already exists. Please try a different new Project name." + break +} + +# Set new Project name (and slug via $ChangeProjectSlug) +$Project.Name = $NewProjectName +If ($ChangeProjectSlug) { + $SlugConvert = $NewProjectName.ToLower().Replace('_','-') -replace '\s','-' + $SlugArray = $SlugConvert | Select-String '(\w+)' -AllMatches | ForEach-Object { $_.Matches.Value } + $Slug = $SlugArray -join '-' + $Project.Slug = $Slug +} + +# Save Project changes +$SaveProject = Invoke-RestMethod -Method PUT "$($OctopusURL)/api/$($SpaceId)/Projects/$($Project.Id)" -Headers $header -Body ($Project | ConvertTo-Json -Depth 10) + +$ProjectCheck = Invoke-RestMethod -Method GET -Uri "$($OctopusURL)/api/$($SpaceId)/Projects/$($Project.Id)" -Headers $Header +If ($ProjectCheck.Name -eq $NewProjectName) { + Write-Host "Project `"$ProjectName`" ($($Project.Id)) is now named `"$NewProjectName`" with the slug `"$($ProjectCheck.Slug)`"" +} From b6f449dae77c31c1ab71b653d4cf1e688216a300 Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Wed, 24 May 2023 18:04:30 +0100 Subject: [PATCH 09/20] Update RenameProject.ps1 --- REST/PowerShell/Projects/RenameProject.ps1 | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/REST/PowerShell/Projects/RenameProject.ps1 b/REST/PowerShell/Projects/RenameProject.ps1 index 9b5f13a3..1e93cbfe 100644 --- a/REST/PowerShell/Projects/RenameProject.ps1 +++ b/REST/PowerShell/Projects/RenameProject.ps1 @@ -12,11 +12,11 @@ $BypassPrompts = $false # Set to $true if you wish to predefine your parameters If ($BypassPrompts) { # === Predefined Parameters (Optional) === - $OctopusURL = "http://YOUR_OCTOPUS_URL.bla" - $OctopusAPIKey = "API-XXXXXXXXXXXXXXXXXX" - $SpaceId = "Spaces-XX" - $ProjectName = "My Project" - $NewProjectName = "My New Project Name" + $OctopusURL = "http://YOUR_OCTOPUS_URL.bla" + $OctopusAPIKey = "API-XXXXXXXXXXXXXXXXXX" + $SpaceId = "Spaces-XX" + $ProjectName = "My Project" + $NewProjectName = "My New Project Name" $ChangeProjectSlug = $true } From 725857a9affc815aa183696fc3e5203d4a433486 Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Wed, 24 May 2023 18:06:17 +0100 Subject: [PATCH 10/20] Update RenameProject.ps1 From de1bcabf9b93fe5cbc632ddac42de3a46bd0361a Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Thu, 25 May 2023 09:54:08 +0100 Subject: [PATCH 11/20] Update RenameProject.ps1 fixed tabs/spaces From 1ee8fc282cf5d72f2b95cbe686002344afba5a99 Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Thu, 25 May 2023 09:55:39 +0100 Subject: [PATCH 12/20] Update RenameProject.ps1 fixed tabs/spaces From 5fd0cb72a9826a3f9740e86290f9051ca771bd82 Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Thu, 25 May 2023 09:56:17 +0100 Subject: [PATCH 13/20] Update RenameProject.ps1 --- REST/PowerShell/Projects/RenameProject.ps1 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/REST/PowerShell/Projects/RenameProject.ps1 b/REST/PowerShell/Projects/RenameProject.ps1 index 1e93cbfe..454364cf 100644 --- a/REST/PowerShell/Projects/RenameProject.ps1 +++ b/REST/PowerShell/Projects/RenameProject.ps1 @@ -1,6 +1,6 @@ -# ================================================================================================================== +# ============================================================================================================= # This script renames a Project (and optionally a Project's slug) on the specified Octopus Server. -# ================================================================================================================== +# ============================================================================================================= $ErrorActionPreference = "Stop"; From b968a544a1815a590f4833eaa544701904c367b9 Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Thu, 25 May 2023 09:57:39 +0100 Subject: [PATCH 14/20] Update RenameProject.ps1 --- REST/PowerShell/Projects/RenameProject.ps1 | 99 +--------------------- 1 file changed, 1 insertion(+), 98 deletions(-) diff --git a/REST/PowerShell/Projects/RenameProject.ps1 b/REST/PowerShell/Projects/RenameProject.ps1 index 454364cf..5b4f8bb6 100644 --- a/REST/PowerShell/Projects/RenameProject.ps1 +++ b/REST/PowerShell/Projects/RenameProject.ps1 @@ -1,98 +1 @@ -# ============================================================================================================= -# This script renames a Project (and optionally a Project's slug) on the specified Octopus Server. -# ============================================================================================================= - -$ErrorActionPreference = "Stop"; - -# ====== BYPASS PROMPTS? ====== -$BypassPrompts = $false # Set to $true if you wish to predefine your parameters - - -# ====== PARAMETERS ====== -If ($BypassPrompts) { - - # === Predefined Parameters (Optional) === - $OctopusURL = "http://YOUR_OCTOPUS_URL.bla" - $OctopusAPIKey = "API-XXXXXXXXXXXXXXXXXX" - $SpaceId = "Spaces-XX" - $ProjectName = "My Project" - $NewProjectName = "My New Project Name" - $ChangeProjectSlug = $true -} - -If (!$BypassPrompts) { - - # === Prompted Parameters === - $OctopusAPIKey = (Read-Host "Enter your Octopus API key (example: `"API-XXXXXXXXXXXXXXXXXX`")").trim('"') - $OctopusURL = (Read-Host "Enter your Octopus Instance URL with no trailing slash (i.e. `"http://YOUR_OCTOPUS_URL.bla`")").trim('"') - $SpaceId = (Read-Host "Enter the SpaceId where the Library Variable Set resides (example: `"Spaces-1`")").trim('"') - $ProjectName = (Read-Host "Enter the current Project name (example: `"My Project`")").trim('"') - $NewProjectName = (Read-Host "Enter the new Project name (example: `"My New Project Name`")").trim('"') - $ChangeProjectSlugPrompt = (Read-Host "Would you like to change the Project's Slug to match the new Project Name? (Type `"Y`" for yes or `"N`" for no)").trim('"') - While (($ChangeProjectSlugPrompt -ne "Y") -and ($ChangeProjectSlugPrompt -ne "N")) { - $ChangeProjectSlugPrompt = (Read-Host "Would you like to change the Project's Slug to match the new Project Name? (Type Y for yes or N for no)").trim('"') - } - If ($ChangeProjectSlugPrompt -eq "Y") {$ChangeProjectSlug = $true} - If ($ChangeProjectSlugPrompt -eq "N") {$ChangeProjectSlug = $false} -} - -$Header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey } - -# Special character check -$CheckSpecialChar = $NewProjectName | Select-String '[^ !@#$%^&();`~,.+=\-\w]' -AllMatches | ForEach-Object { $_.Matches.Value } -If ($CheckSpecialChar) { - $PrintSpecialChars = $CheckSpecialChar -join ' ' - Write-Warning "The following special characters were detected and may cause this action to fail:" - Write-Host "$($PrintSpecialChars)" - If (!$BypassPrompts) { - Write-Host "" - $Confirm = (Read-Host "Type `"Y`" to continue as is, `"R`" to remove these characters and continue or `"N`" to quit)").trim('"') - While (($Confirm -ne "N") -and ($Confirm -ne "Y") -and ($Confirm -ne "R")) { - $Confirm = (Read-Host "Type `"Y`" to continue as is, `"R`" to remove these characters and continue or `"N`" to quit)").trim('"') - } - If ($Confirm -eq "N") { - Write-Warning "Aborted. No changes were made." - break - } - If ($Confirm -eq "R") { - Write-Host "Requested new Project name: $($NewProjectName)" - $NewProjectName = $NewProjectName -replace '[^ !@#$%^&();`~,.+=\-\w]','' - Write-Host ">Adjusted new Project name: $($NewProjectName)" - } - } -} - -# Try to GET the ProjectId for $ProjectName -Try { - $Project = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)/Projects/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectName} - If (!$Project) {throw} -} -Catch { - Write-Warning "Unable to find a ProjectId for the Project Name `"$($ProjectName)`" via `"$($OctopusURL)/api/$($SpaceId)/Projects/all`"" - Write-Warning "Check your parameters (Octopus API key, URL, SpaceId, etc.), ensure your API key has sufficient permissions, and the Octopus Server is accessible from this machine." - break -} - -# Confirm no Projects already exist in $SpaceId with $NewProjectName -$ProjectNameCheck = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)/Projects/all" -Headers $Header) | Where-Object {$_.Name -eq $NewProjectName} -If ($ProjectNameCheck) { - Write-Warning "A Project named `"$($NewProjectName)`" ($($ProjectNameCheck.Id)) already exists. Please try a different new Project name." - break -} - -# Set new Project name (and slug via $ChangeProjectSlug) -$Project.Name = $NewProjectName -If ($ChangeProjectSlug) { - $SlugConvert = $NewProjectName.ToLower().Replace('_','-') -replace '\s','-' - $SlugArray = $SlugConvert | Select-String '(\w+)' -AllMatches | ForEach-Object { $_.Matches.Value } - $Slug = $SlugArray -join '-' - $Project.Slug = $Slug -} - -# Save Project changes -$SaveProject = Invoke-RestMethod -Method PUT "$($OctopusURL)/api/$($SpaceId)/Projects/$($Project.Id)" -Headers $header -Body ($Project | ConvertTo-Json -Depth 10) - -$ProjectCheck = Invoke-RestMethod -Method GET -Uri "$($OctopusURL)/api/$($SpaceId)/Projects/$($Project.Id)" -Headers $Header -If ($ProjectCheck.Name -eq $NewProjectName) { - Write-Host "Project `"$ProjectName`" ($($Project.Id)) is now named `"$NewProjectName`" with the slug `"$($ProjectCheck.Slug)`"" -} +clear From f2f3b5d6f2449829d722befa4cbb9fd9d19eb00e Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Thu, 25 May 2023 09:58:04 +0100 Subject: [PATCH 15/20] Update RenameProject.ps1 --- REST/PowerShell/Projects/RenameProject.ps1 | 99 +++++++++++++++++++++- 1 file changed, 98 insertions(+), 1 deletion(-) diff --git a/REST/PowerShell/Projects/RenameProject.ps1 b/REST/PowerShell/Projects/RenameProject.ps1 index 5b4f8bb6..1e93cbfe 100644 --- a/REST/PowerShell/Projects/RenameProject.ps1 +++ b/REST/PowerShell/Projects/RenameProject.ps1 @@ -1 +1,98 @@ -clear +# ================================================================================================================== +# This script renames a Project (and optionally a Project's slug) on the specified Octopus Server. +# ================================================================================================================== + +$ErrorActionPreference = "Stop"; + +# ====== BYPASS PROMPTS? ====== +$BypassPrompts = $false # Set to $true if you wish to predefine your parameters + + +# ====== PARAMETERS ====== +If ($BypassPrompts) { + + # === Predefined Parameters (Optional) === + $OctopusURL = "http://YOUR_OCTOPUS_URL.bla" + $OctopusAPIKey = "API-XXXXXXXXXXXXXXXXXX" + $SpaceId = "Spaces-XX" + $ProjectName = "My Project" + $NewProjectName = "My New Project Name" + $ChangeProjectSlug = $true +} + +If (!$BypassPrompts) { + + # === Prompted Parameters === + $OctopusAPIKey = (Read-Host "Enter your Octopus API key (example: `"API-XXXXXXXXXXXXXXXXXX`")").trim('"') + $OctopusURL = (Read-Host "Enter your Octopus Instance URL with no trailing slash (i.e. `"http://YOUR_OCTOPUS_URL.bla`")").trim('"') + $SpaceId = (Read-Host "Enter the SpaceId where the Library Variable Set resides (example: `"Spaces-1`")").trim('"') + $ProjectName = (Read-Host "Enter the current Project name (example: `"My Project`")").trim('"') + $NewProjectName = (Read-Host "Enter the new Project name (example: `"My New Project Name`")").trim('"') + $ChangeProjectSlugPrompt = (Read-Host "Would you like to change the Project's Slug to match the new Project Name? (Type `"Y`" for yes or `"N`" for no)").trim('"') + While (($ChangeProjectSlugPrompt -ne "Y") -and ($ChangeProjectSlugPrompt -ne "N")) { + $ChangeProjectSlugPrompt = (Read-Host "Would you like to change the Project's Slug to match the new Project Name? (Type Y for yes or N for no)").trim('"') + } + If ($ChangeProjectSlugPrompt -eq "Y") {$ChangeProjectSlug = $true} + If ($ChangeProjectSlugPrompt -eq "N") {$ChangeProjectSlug = $false} +} + +$Header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey } + +# Special character check +$CheckSpecialChar = $NewProjectName | Select-String '[^ !@#$%^&();`~,.+=\-\w]' -AllMatches | ForEach-Object { $_.Matches.Value } +If ($CheckSpecialChar) { + $PrintSpecialChars = $CheckSpecialChar -join ' ' + Write-Warning "The following special characters were detected and may cause this action to fail:" + Write-Host "$($PrintSpecialChars)" + If (!$BypassPrompts) { + Write-Host "" + $Confirm = (Read-Host "Type `"Y`" to continue as is, `"R`" to remove these characters and continue or `"N`" to quit)").trim('"') + While (($Confirm -ne "N") -and ($Confirm -ne "Y") -and ($Confirm -ne "R")) { + $Confirm = (Read-Host "Type `"Y`" to continue as is, `"R`" to remove these characters and continue or `"N`" to quit)").trim('"') + } + If ($Confirm -eq "N") { + Write-Warning "Aborted. No changes were made." + break + } + If ($Confirm -eq "R") { + Write-Host "Requested new Project name: $($NewProjectName)" + $NewProjectName = $NewProjectName -replace '[^ !@#$%^&();`~,.+=\-\w]','' + Write-Host ">Adjusted new Project name: $($NewProjectName)" + } + } +} + +# Try to GET the ProjectId for $ProjectName +Try { + $Project = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)/Projects/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectName} + If (!$Project) {throw} +} +Catch { + Write-Warning "Unable to find a ProjectId for the Project Name `"$($ProjectName)`" via `"$($OctopusURL)/api/$($SpaceId)/Projects/all`"" + Write-Warning "Check your parameters (Octopus API key, URL, SpaceId, etc.), ensure your API key has sufficient permissions, and the Octopus Server is accessible from this machine." + break +} + +# Confirm no Projects already exist in $SpaceId with $NewProjectName +$ProjectNameCheck = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)/Projects/all" -Headers $Header) | Where-Object {$_.Name -eq $NewProjectName} +If ($ProjectNameCheck) { + Write-Warning "A Project named `"$($NewProjectName)`" ($($ProjectNameCheck.Id)) already exists. Please try a different new Project name." + break +} + +# Set new Project name (and slug via $ChangeProjectSlug) +$Project.Name = $NewProjectName +If ($ChangeProjectSlug) { + $SlugConvert = $NewProjectName.ToLower().Replace('_','-') -replace '\s','-' + $SlugArray = $SlugConvert | Select-String '(\w+)' -AllMatches | ForEach-Object { $_.Matches.Value } + $Slug = $SlugArray -join '-' + $Project.Slug = $Slug +} + +# Save Project changes +$SaveProject = Invoke-RestMethod -Method PUT "$($OctopusURL)/api/$($SpaceId)/Projects/$($Project.Id)" -Headers $header -Body ($Project | ConvertTo-Json -Depth 10) + +$ProjectCheck = Invoke-RestMethod -Method GET -Uri "$($OctopusURL)/api/$($SpaceId)/Projects/$($Project.Id)" -Headers $Header +If ($ProjectCheck.Name -eq $NewProjectName) { + Write-Host "Project `"$ProjectName`" ($($Project.Id)) is now named `"$NewProjectName`" with the slug `"$($ProjectCheck.Slug)`"" +} From 3ceb7601081b52a0aab77deaa9efd31113169b0f Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Fri, 26 May 2023 12:29:41 +0100 Subject: [PATCH 16/20] Update and rename DeleteProjectsWithoutDeploymentProcess.ps1 to ListorDeleteProjectsWithoutDeploymentProcess.ps1 Added various functions and precautions including: - Prompted parameters - Breaking out Projects without a Deployment Process into categories - Ability to output the list of affected Projects to a text file - Can look at "All" Spaces or a single SpaceId --- ...DeleteProjectsWithoutDeploymentProcess.ps1 | 26 -- ...DeleteProjectsWithoutDeploymentProcess.ps1 | 322 ++++++++++++++++++ 2 files changed, 322 insertions(+), 26 deletions(-) delete mode 100644 REST/PowerShell/Projects/DeleteProjectsWithoutDeploymentProcess.ps1 create mode 100644 REST/PowerShell/Projects/ListorDeleteProjectsWithoutDeploymentProcess.ps1 diff --git a/REST/PowerShell/Projects/DeleteProjectsWithoutDeploymentProcess.ps1 b/REST/PowerShell/Projects/DeleteProjectsWithoutDeploymentProcess.ps1 deleted file mode 100644 index 3e1f6d01..00000000 --- a/REST/PowerShell/Projects/DeleteProjectsWithoutDeploymentProcess.ps1 +++ /dev/null @@ -1,26 +0,0 @@ -$ErrorActionPreference = "Stop"; - -# Define working variables -$octopusURL = "https://youroctopusurl" -$octopusAPIKey = "API-KEY" -$header = @{ "X-Octopus-ApiKey" = $octopusAPIKey } - -# Get space -$space = (Invoke-RestMethod -Method Get -Uri "$octopusURL/api/spaces/all" -Headers $header) | Where-Object {$_.Name -eq $spaceName} - -# Get project -$projects = Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/projects/all" -Headers $header - -# Loop through projects -foreach ($project in $projects) -{ - # Get deployment process - $deploymentProcess = Invoke-RestMethod -Method Get -Uri "$octopusURL/api/$($space.Id)/deploymentprocesses/$($project.DeploymentProcessId)" -Headers $header - - # Check to see if there's a process - if (($null -eq $deploymentProcess.Steps) -or ($deploymentProcess.Steps.Count -eq 0)) - { - # Delete project - Invoke-RestMethod -Method Delete -Uri "$octopusURL/api/$($space.Id)/projects/$($project.Id)" -Headers $header - } -} \ No newline at end of file diff --git a/REST/PowerShell/Projects/ListorDeleteProjectsWithoutDeploymentProcess.ps1 b/REST/PowerShell/Projects/ListorDeleteProjectsWithoutDeploymentProcess.ps1 new file mode 100644 index 00000000..6ffd02f3 --- /dev/null +++ b/REST/PowerShell/Projects/ListorDeleteProjectsWithoutDeploymentProcess.ps1 @@ -0,0 +1,322 @@ +# ======================================================================= +# This script can list, output and/or delete Projects that are +# currently without a Deployment Process on the specified Octopus Server. +# ======================================================================= + +$ErrorActionPreference = "Stop"; + +# ====== BYPASS PROMPTS? ====== +$BypassPrompts = $false # Set to $true if you wish to predefine your parameters + + +# ====== PARAMETERS ====== +If ($BypassPrompts) { + + # === Predefined Parameters (Optional) === + $OctopusURL = "http://YOUR_OCTOPUS_URL.bla" + $OctopusAPIKey = "API-XXXXXXXXXXXXXXXXXX" + $SpaceId = "All" # SpaceId or use "All" to check all Spaces + $DirPath = "C:\New Folder" # Directory path for option (6) below (e.g. "C:\New Folder") + $FinalOption = 1 # Choose a number from the list below (1-6) + # (1) Do nothing (quit) + # (2) Delete all Projects with no Deployment Process (regardless of whether they have Runbooks or Releases with Deployments) + # (3) Delete all Projects with no Deployment Process, ignore any Projects with Runbooks + # (4) Delete all Projects with no Deployment Process, ignore any Projects that contain Releases with Deployments + # (5) Delete all Projects with no Deployment Process, ignore any Projects with Runbooks or that contain Releases with Deployments + # (6) Create a text file containing the list of Projects + +} + +If (!$BypassPrompts) { + + # === Prompted Parameters === + $OctopusAPIKey = (Read-Host "Enter your Octopus API key (example: `"API-XXXXXXXXXXXXXXXXXX`")").trim('"') + $OctopusURL = (Read-Host "Enter your Octopus Instance URL with no trailing slash (i.e. `"http://YOUR_OCTOPUS_URL.bla`")").trim('"') + $SpaceId = (Read-Host "Enter the SpaceId where you would like to check for Projects without a Deployment Process (example: `"Spaces-1`" or use `"All`" to check all Spaces)").trim('"') +} + +$Header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey } + +# Get SpaceIds (if SpaceId = "All") +$EmptyProjects = @() +$EmptyProjectsWithRunbooks = @() +$EmptyProjectsWithDeployments = @() +$EmptyProjectsWithNeither = @() +$EmptyProjectsWithBoth = @() + +If ($SpaceId -eq "All") { + $Spaces = Invoke-RestMethod -Method GET "$($OctopusURL)/api/Spaces/all" -Headers $Header + Foreach ($Space in $Spaces) { + $SpaceId = $Space.Id + Write-Host "Processing $($Space.name) ($($SpaceId))" + Try { + $SkipSpace = $false + $Projects = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)/Projects/all" -Headers $Header) + } + Catch { + $SkipSpace = $true + Write-Warning "This User does not have permissions in $($Space.name) ($($SpaceId)). Continuing..." + } + If ($SkipSpace -eq $false) { + Foreach ($Project in $Projects) { + $GitRefDPCounter = 0 + Write-Host "Processing $($Project.name) ($($Project.Id))" + If ($Project.PersistenceSettings.Type -eq "Database") { + $DeploymentProcess = Invoke-RestMethod -Method GET "$($OctopusURL)$($Project.Links.DeploymentProcess)" -Headers $Header + } + If (($Project.PersistenceSettings.Type -eq "VersionControlled") -and ($GitRefDPCounter -eq 0)) { + Try { + $GitRefList = Invoke-RestMethod -Method GET "$($OctopusURL)/api/Spaces-1/projects/$($Project.Id)/git/branches" -Headers $Header + } + Catch { + Write-Warning "$($Project.Name) ($($Project.Id)) does not have valid version control credentials. Continuing..." + } + $GitRefs = $GitRefList.Items + Foreach ($GitRef in $GitRefs) { + $GitRefDPLink = $GitRef.Links.DeploymentProcess + Try { + $DeploymentProcess = Invoke-RestMethod -Method GET "$($OctopusURL)$($GitRefDPLink)" -Headers $Header + } + Catch { + Write-Warning "$($GitRef.Name) is not initialized for $($Project.Name) ($($Project.Id)). Continuing..." + } + If (($DeploymentProcess.Steps) -or ($DeploymentProcess.Steps.Count -gt 0)) { + $GitRefDPCounter ++ + } + } + } + If ((!$DeploymentProcess.Steps) -or ($DeploymentProcess.Steps.Count -eq 0) -and ($GitRefDPCounter -eq 0)) { + $EmptyProjects += $Project + } + } + } + } +} +Else { + Try { + $SkipSpace = $false + $Space = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)" -Headers $Header) + $Projects = (Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($SpaceId)/Projects/all" -Headers $Header) + } + Catch { + $SkipSpace = $true + Write-Warning "This User does not have permissions in $($Space.name) ($($SpaceId)). Continuing..." + } + If ($SkipSpace -eq $false) { + Foreach ($Project in $Projects) { + $GitRefDPCounter = 0 + Write-Host "Processing $($Project.name) ($($Project.Id))" + If ($Project.PersistenceSettings.Type -eq "Database") { + $DeploymentProcess = Invoke-RestMethod -Method GET "$($OctopusURL)$($Project.Links.DeploymentProcess)" -Headers $Header + } + If (($Project.PersistenceSettings.Type -eq "VersionControlled") -and ($GitRefDPCounter -eq 0)) { + Try { + $GitRefList = Invoke-RestMethod -Method GET "$($OctopusURL)/api/Spaces-1/projects/$($Project.Id)/git/branches" -Headers $Header + } + Catch { + Write-Warning "$($Project.Name) ($($Project.Id)) does not have valid version control credentials. Continuing..." + } + $GitRefs = $GitRefList.Items + Foreach ($GitRef in $GitRefs) { + $GitRefDPLink = $GitRef.Links.DeploymentProcess + Try { + $DeploymentProcess = Invoke-RestMethod -Method GET "$($OctopusURL)$($GitRefDPLink)" -Headers $Header + } + Catch { + Write-Warning "$($GitRef.Name) is not initialized for $($Project.Name) ($($Project.Id)). Continuing..." + } + If (($DeploymentProcess.Steps) -or ($DeploymentProcess.Steps.Count -gt 0)) { + $GitRefDPCounter ++ + } + } + } + If ((!$DeploymentProcess.Steps) -or ($DeploymentProcess.Steps.Count -eq 0) -and ($GitRefDPCounter -eq 0)) { + $EmptyProjects += $Project + } + } + } +} +Foreach ($EmptyProject in $EmptyProjects) { + $Runbooks = Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($EmptyProject.SpaceId)/Projects/$($EmptyProject.Id)/Runbooks" -Headers $Header + If ($Runbooks.Items) { + $EmptyProjectsWithRunbooks += $EmptyProject + } + $Releases = Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($EmptyProject.SpaceId)/Projects/$($EmptyProject.Id)/Releases" -Headers $Header + If ($Releases.Items) { + $ProjectReleases = $Releases.Items + Foreach ($ProjectRelease in $ProjectReleases) { + Write-Host "Checking $($ProjectRelease.Id) for $($EmptyProject.Name) ($($EmptyProject.Id))" + $DeploymentCounter = 0 + If ($DeploymentCounter -lt 1) { + $Deployments = Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($EmptyProject.SpaceId)/Releases/$($ProjectRelease.Id)/Deployments" -Headers $Header + If (!$Deployments) { + $DeploymentCounter ++ + } + } + } + $EmptyProjectsWithDeployments += $EmptyProject + } + + If (($EmptyProjectsWithRunbooks.Id -contains $EmptyProject.Id) -and ($EmptyProjectsWithDeployments.Id -contains $EmptyProject.Id)) { + $EmptyProjectsWithBoth += $EmptyProject + $EmptyProjectsWithDeployments = $EmptyProjectsWithDeployments -ne $EmptyProject + $EmptyProjectsWithRunbooks = $EmptyProjectsWithRunbooks -ne $EmptyProject + } + If (($EmptyProjectsWithRunbooks.Id -notcontains $EmptyProject.Id) -and ($EmptyProjectsWithDeployments.Id -notcontains $EmptyProject.Id)) { + $EmptyProjectsWithNeither += $EmptyProject + } +} +Write-Host "" +Write-Host "===" +Write-Host "Projects with no Deployment Process, Runbooks, or Deployments:" +Write-Host "--------------------------------------------------------------" +Foreach ($_ in $EmptyProjectsWithNeither) { + Write-Host ">$($_.Name) ($($_.Id))" + Write-Host ">$($OctopusURL)$($_.Links.Web)" + Write-Host "" +} +Write-Host "" +Write-Host "===" +Write-Host "Projects with no Deployment Process, but contains Runbooks" +Write-Host "----------------------------------------------------------" +Foreach ($_ in $EmptyProjectsWithRunbooks) { + Write-Host ">$($_.Name) ($($_.Id))" + Write-Host ">$($OctopusURL)$($_.Links.Web)" + Write-Host "" +} +Write-Host "" +Write-Host "===" +Write-Host "Projects with no Deployment Process, but contains Releases with Deployments" +Write-Host "---------------------------------------------------------------------------" +Foreach ($_ in $EmptyProjectsWithDeployments) { + Write-Host ">$($_.Name) ($($_.Id))" + Write-Host ">$($OctopusURL)$($_.Links.Web)" + Write-Host "" +} +Write-Host "" +Write-Host "===" +Write-Host "Projects with no Deployment Process, but contains both Runbooks and Deployments:" +Write-Host "--------------------------------------------------------------------------------" +Foreach ($_ in $EmptyProjectsWithBoth) { + Write-Host ">$($_.Name) ($($_.Id))" + Write-Host ">$($OctopusURL)$($_.Links.Web)" + Write-Host "" +} + +If (!$BypassPrompts) { + Write-Host "Please select one of the following options:" + Write-Host "-------------------------------------------" + Write-Host "(1) Do nothing (quit)" + Write-Host "(2) Delete all Projects with no Deployment Process (regardless of whether they have Runbooks or Releases with Deployments)" + Write-Host "(3) Delete all Projects with no Deployment Process, ignore any Projects with Runbooks" + Write-Host "(4) Delete all Projects with no Deployment Process, ignore any Projects that contain Releases with Deployments" + Write-Host "(5) Delete all Projects with no Deployment Process, ignore any Projects with Runbooks or that contain Releases with Deployments" + Write-Host "(6) Create a text file containing the list of Projects" + $FinalOption = (Read-Host "Choose a number from the list above (1-6)") + While ($FinalOption -notin 1..6) { + $FinalOption = (Read-Host "Choose a number from the list above (1-6)") + } +} +$ProjectsToDelete = @() +If ($FinalOption -eq 1) { + Write-Host "No changes have been made. Quitting..." + break +} +If ($FinalOption -eq 2) { + Write-Host "Deleting all Projects with no Deployment Process (regardless of whether they have Runbooks or Releases with Deployments)" + Foreach ($EmptyProject in $EmptyProjects) { + Write-Host "Deleting Project "$($EmptyProject.Name)" ($($EmptyProject.Id)) in $($EmptyProject.SpaceId)" + Invoke-RestMethod -Method DEL "$($OctopusURL)/api/$($EmptyProject.SpaceId)/Projects/$($EmptyProject.Id)" -Headers $Header + } +} +If ($FinalOption -eq 3) { + Write-Host "Deleting all Projects with no Deployment Process, ignoring any Projects with Runbooks" + Foreach ($_ in $EmptyProjectsWithNeither) { $ProjectsToDelete += $_ } + Foreach ($_ in $EmptyProjectsWithDeployments) { $ProjectsToDelete += $_ } + Foreach ($_ in $ProjectsToDelete) { + Write-Host "Deleting Project "$($_.Name)" ($($_.Id)) in $($_.SpaceId)" + Invoke-RestMethod -Method DEL "$($OctopusURL)/api/$($_.SpaceId)/Projects/$($_.Id)" -Headers $Header + } +} +If ($FinalOption -eq 4) { + Write-Host "Deleting all Projects with no Deployment Process, ignoring any Projects that contain Releases with Deployments" + Foreach ($_ in $EmptyProjectsWithNeither) { $ProjectsToDelete += $_ } + Foreach ($_ in $EmptyProjectsWithRunbooks) { $ProjectsToDelete += $_ } + Foreach ($_ in $ProjectsToDelete) { + Write-Host "Deleting Project "$($_.Name)" ($($_.Id)) in $($_.SpaceId)" + Invoke-RestMethod -Method DEL "$($OctopusURL)/api/$($_.SpaceId)/Projects/$($_.Id)" -Headers $Header + } +} +If ($FinalOption -eq 5) { + Write-Host "Deleting all Projects with no Deployment Process, ignoring any Projects with Runbooks or that contain Releases with Deployments" + Foreach ($_ in $EmptyProjectsWithNeither) { $ProjectsToDelete += $_ } + Foreach ($_ in $ProjectsToDelete) { + Write-Host "Deleting Project "$($_.Name)" ($($_.Id)) in $($_.SpaceId)" + Invoke-RestMethod -Method DEL "$($OctopusURL)/api/$($_.SpaceId)/Projects/$($_.Id)" -Headers $Header + } +} +If ($FinalOption -eq 6) { + If (!$BypassPrompts) { + $DirPath = (Read-Host "Please specify a location to output the list of Projects (e.g. `"C:\New Folder`")").trim('"') + } + Write-Host "No changes have been made to your Octopus instance. Creating text file..." + $TimeStamp = $(((get-date).ToUniversalTime()).ToString("yyyyMMddTHHmmssZ")) + $FileName = "ProjectsWithoutDeploymentProcesses.$($TimeStamp).txt" + Try { + New-Item -ItemType "directory" -Path "$($DirPath)" + } + Catch { + $TestPath = Test-Path "$($DirPath)" + If ($TestPath) { + Write-Host "Directory already exists. Continuing..." + } + If (!$TestPath) { + Write-Warning "Unable to create or access directory. Please check your local machine permissions to this folder and make sure you typed the location correctly." + break + } + } + New-Item -Path "$($DirPath)" -Name "$($FileName)" -ItemType "file" + Function AddToOutput { + Param ($Text) + Add-Content -Path "$($DirPath)\$($FileName)" -Value $Text + } + AddToOutput -Text "===" + AddToOutput -Text "Projects with no Deployment Process, Runbooks, or Deployments:" + AddToOutput -Text "--------------------------------------------------------------" + Foreach ($_ in $EmptyProjectsWithNeither) { + AddToOutput -Text ">$($_.Name) ($($_.Id))" + AddToOutput -Text ">$($OctopusURL)$($_.Links.Web)" + AddToOutput -Text "" + } + AddToOutput -Text "" + AddToOutput -Text "===" + AddToOutput -Text "Projects with no Deployment Process, but contains Runbooks" + AddToOutput -Text "----------------------------------------------------------" + Foreach ($_ in $EmptyProjectsWithRunbooks) { + AddToOutput -Text ">$($_.Name) ($($_.Id))" + AddToOutput -Text ">$($OctopusURL)$($_.Links.Web)" + AddToOutput -Text "" + } + AddToOutput -Text "" + AddToOutput -Text "===" + AddToOutput -Text "Projects with no Deployment Process, but contains Releases with Deployments" + AddToOutput -Text "---------------------------------------------------------------------------" + Foreach ($_ in $EmptyProjectsWithDeployments) { + AddToOutput -Text ">$($_.Name) ($($_.Id))" + AddToOutput -Text ">$($OctopusURL)$($_.Links.Web)" + AddToOutput -Text "" + } + AddToOutput -Text "" + AddToOutput -Text "===" + AddToOutput -Text "Projects with no Deployment Process, but contains both Runbooks and Deployments:" + AddToOutput -Text "--------------------------------------------------------------------------------" + Foreach ($_ in $EmptyProjectsWithBoth) { + AddToOutput -Text ">$($_.Name) ($($_.Id))" + AddToOutput -Text ">$($OctopusURL)$($_.Links.Web)" + AddToOutput -Text "" + } + Write-Host "" + Write-Host ">>>>>>>>>>>>>>>>>" + Write-Host "Text file created: $($DirPath)\$($FileName)" + Write-Host ">>>>>>>>>>>>>>>>>" +} From 1e1f65075381f2750acb89b958f86897348555d1 Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Fri, 26 May 2023 12:41:32 +0100 Subject: [PATCH 17/20] Update ListorDeleteProjectsWithoutDeploymentProcess.ps1 Added comments --- .../ListorDeleteProjectsWithoutDeploymentProcess.ps1 | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/REST/PowerShell/Projects/ListorDeleteProjectsWithoutDeploymentProcess.ps1 b/REST/PowerShell/Projects/ListorDeleteProjectsWithoutDeploymentProcess.ps1 index 6ffd02f3..59dea99b 100644 --- a/REST/PowerShell/Projects/ListorDeleteProjectsWithoutDeploymentProcess.ps1 +++ b/REST/PowerShell/Projects/ListorDeleteProjectsWithoutDeploymentProcess.ps1 @@ -24,7 +24,6 @@ If ($BypassPrompts) { # (4) Delete all Projects with no Deployment Process, ignore any Projects that contain Releases with Deployments # (5) Delete all Projects with no Deployment Process, ignore any Projects with Runbooks or that contain Releases with Deployments # (6) Create a text file containing the list of Projects - } If (!$BypassPrompts) { @@ -37,13 +36,15 @@ If (!$BypassPrompts) { $Header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey } -# Get SpaceIds (if SpaceId = "All") +# ====== SCRIPT BODY ====== +# Set arrays for later $EmptyProjects = @() $EmptyProjectsWithRunbooks = @() $EmptyProjectsWithDeployments = @() $EmptyProjectsWithNeither = @() $EmptyProjectsWithBoth = @() +# Find Projects without a Deployment Process in All Spaces If ($SpaceId -eq "All") { $Spaces = Invoke-RestMethod -Method GET "$($OctopusURL)/api/Spaces/all" -Headers $Header Foreach ($Space in $Spaces) { @@ -92,6 +93,7 @@ If ($SpaceId -eq "All") { } } } +# Find Projects without a Deployment Process in $SpaceId Else { Try { $SkipSpace = $false @@ -106,9 +108,11 @@ Else { Foreach ($Project in $Projects) { $GitRefDPCounter = 0 Write-Host "Processing $($Project.name) ($($Project.Id))" + # Check for a Deployment Process in a normal Project If ($Project.PersistenceSettings.Type -eq "Database") { $DeploymentProcess = Invoke-RestMethod -Method GET "$($OctopusURL)$($Project.Links.DeploymentProcess)" -Headers $Header } + # Check for a Deployment Process in a Git-enabled Project If (($Project.PersistenceSettings.Type -eq "VersionControlled") -and ($GitRefDPCounter -eq 0)) { Try { $GitRefList = Invoke-RestMethod -Method GET "$($OctopusURL)/api/Spaces-1/projects/$($Project.Id)/git/branches" -Headers $Header @@ -136,6 +140,8 @@ Else { } } } + +# Check $EmptyProjects for Runbooks and Releases with Deployments Foreach ($EmptyProject in $EmptyProjects) { $Runbooks = Invoke-RestMethod -Method GET "$($OctopusURL)/api/$($EmptyProject.SpaceId)/Projects/$($EmptyProject.Id)/Runbooks" -Headers $Header If ($Runbooks.Items) { From 6ad22517148f5e46c92af498e76e77478269c5fb Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Fri, 26 May 2023 12:44:13 +0100 Subject: [PATCH 18/20] Update RenameProject.ps1 Added comments --- REST/PowerShell/Projects/RenameProject.ps1 | 2 ++ 1 file changed, 2 insertions(+) diff --git a/REST/PowerShell/Projects/RenameProject.ps1 b/REST/PowerShell/Projects/RenameProject.ps1 index 1e93cbfe..7e472ee9 100644 --- a/REST/PowerShell/Projects/RenameProject.ps1 +++ b/REST/PowerShell/Projects/RenameProject.ps1 @@ -36,8 +36,10 @@ If (!$BypassPrompts) { If ($ChangeProjectSlugPrompt -eq "N") {$ChangeProjectSlug = $false} } + $Header = @{ "X-Octopus-ApiKey" = $OctopusAPIKey } +# ====== SCRIPT BODY ====== # Special character check $CheckSpecialChar = $NewProjectName | Select-String '[^ !@#$%^&();`~,.+=\-\w]' -AllMatches | ForEach-Object { $_.Matches.Value } If ($CheckSpecialChar) { From dcb0d483e80444ec5778cbdb254367c5a0011348 Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Fri, 26 May 2023 16:00:27 +0100 Subject: [PATCH 19/20] Update CreateProject.ps1 Updated prompting to be the default option --- REST/PowerShell/Projects/CreateProject.ps1 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/REST/PowerShell/Projects/CreateProject.ps1 b/REST/PowerShell/Projects/CreateProject.ps1 index 7ee5f00a..036dd941 100644 --- a/REST/PowerShell/Projects/CreateProject.ps1 +++ b/REST/PowerShell/Projects/CreateProject.ps1 @@ -6,7 +6,7 @@ $ErrorActionPreference = "Stop"; # ====== BYPASS PROMPTS? ====== -$BypassPrompts = $true # Set to $true if you wish to predefine your parameters +$BypassPrompts = $false # Set to $true if you wish to predefine your parameters # ====== PARAMETERS ====== From a3b2e284048de37eac191ef882b6212b0abda460 Mon Sep 17 00:00:00 2001 From: Donny Bell <63249187+donnybell@users.noreply.github.com> Date: Tue, 30 May 2023 10:53:06 +0100 Subject: [PATCH 20/20] Update CreateProject.ps1 Changed logic for committing the JSON --- REST/PowerShell/Projects/CreateProject.ps1 | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/REST/PowerShell/Projects/CreateProject.ps1 b/REST/PowerShell/Projects/CreateProject.ps1 index 036dd941..4584b5ed 100644 --- a/REST/PowerShell/Projects/CreateProject.ps1 +++ b/REST/PowerShell/Projects/CreateProject.ps1 @@ -69,14 +69,11 @@ $JsonPayload = @{ } # Create Project using $JsonPayload -Try { - $CheckProjName = (Invoke-RestMethod -Method GET -Uri "$($OctopusURL)/api/$($SpaceId)/projects/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectName} - If ($CheckProjName) {throw} - Else { - $NewProject = Invoke-RestMethod -Method POST -Uri "$($OctopusURL)/api/$($SpaceId)/projects" -Body ($JsonPayload | ConvertTo-Json -Depth 10) -Headers $Header - Write-Host "You may view your new Project at: $($OctopusURL)$($NewProject.Links.Self)" - } -} -Catch { +$CheckProjName = (Invoke-RestMethod -Method GET -Uri "$($OctopusURL)/api/$($SpaceId)/projects/all" -Headers $Header) | Where-Object {$_.Name -eq $ProjectName} +If ($CheckProjName) { Write-Warning "A Project with the name `"$($ProjectName)`" already exists in `"$($SpaceId)`". Please choose a Project Name that does not exist in this Space." } +Else { + $NewProject = Invoke-RestMethod -Method POST -Uri "$($OctopusURL)/api/$($SpaceId)/projects" -Body ($JsonPayload | ConvertTo-Json -Depth 10) -Headers $Header + Write-Host "You may view your new Project at: $($OctopusURL)$($NewProject.Links.Self)" +}