From 73b4a8a4f2c8a36fa7a0e1d994743848e815b442 Mon Sep 17 00:00:00 2001 From: ilia-shipitsin <125650415+ilia-shipitsin@users.noreply.github.com> Date: Fri, 22 Sep 2023 11:34:04 +0200 Subject: [PATCH] helpers/GenerateResourcesAndImage.ps1: check for interactive mode (#8326) * helpers/GenerateResourcesAndImage.ps1: check for interactive mode when RG already exists, we asked user whether to delete it, however it is only possible in interactive mode * mention -ReuseResourceGroup as an option, add back new line --- helpers/GenerateResourcesAndImage.ps1 | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/helpers/GenerateResourcesAndImage.ps1 b/helpers/GenerateResourcesAndImage.ps1 index 2ca3503de..120e27760 100644 --- a/helpers/GenerateResourcesAndImage.ps1 +++ b/helpers/GenerateResourcesAndImage.ps1 @@ -266,16 +266,23 @@ Function GenerateResourcesAndImage { $ResourceGroupExists = $false } else { - # Resource group already exists, ask the user what to do - $title = "Resource group '$ResourceGroupName' already exists" - $message = "Do you want to delete the resource group and all resources in it?" + # are we running in a non-interactive session? + # https://stackoverflow.com/questions/9738535/powershell-test-for-noninteractive-mode + if ([System.Console]::IsOutputRedirected -or ![Environment]::UserInteractive -or !!([Environment]::GetCommandLineArgs() | Where-Object { $_ -ilike '-noni*' })) { + throw "Non-interactive mode, resource group '$ResourceGroupName' already exists, either specify -Force to delete it, or -ReuseResourceGroup to reuse." + } + else { + # Resource group already exists, ask the user what to do + $title = "Resource group '$ResourceGroupName' already exists" + $message = "Do you want to delete the resource group and all resources in it?" - $options = @( - [System.Management.Automation.Host.ChoiceDescription]::new("&Yes", "Delete the resource group and all resources in it."), - [System.Management.Automation.Host.ChoiceDescription]::new("&No", "Keep the resource group and continue."), - [System.Management.Automation.Host.ChoiceDescription]::new("&Abort", "Abort execution.") - ) - $result = $Host.UI.PromptForChoice($title, $message, $options, 0) + $options = @( + [System.Management.Automation.Host.ChoiceDescription]::new("&Yes", "Delete the resource group and all resources in it."), + [System.Management.Automation.Host.ChoiceDescription]::new("&No", "Keep the resource group and continue."), + [System.Management.Automation.Host.ChoiceDescription]::new("&Abort", "Abort execution.") + ) + $result = $Host.UI.PromptForChoice($title, $message, $options, 0) + } switch ($result) { 0 {