Skip to content

Commit 73b4a8a

Browse files
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
1 parent e1ac652 commit 73b4a8a

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

helpers/GenerateResourcesAndImage.ps1

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -266,16 +266,23 @@ Function GenerateResourcesAndImage {
266266
$ResourceGroupExists = $false
267267
}
268268
else {
269-
# Resource group already exists, ask the user what to do
270-
$title = "Resource group '$ResourceGroupName' already exists"
271-
$message = "Do you want to delete the resource group and all resources in it?"
269+
# are we running in a non-interactive session?
270+
# https://stackoverflow.com/questions/9738535/powershell-test-for-noninteractive-mode
271+
if ([System.Console]::IsOutputRedirected -or ![Environment]::UserInteractive -or !!([Environment]::GetCommandLineArgs() | Where-Object { $_ -ilike '-noni*' })) {
272+
throw "Non-interactive mode, resource group '$ResourceGroupName' already exists, either specify -Force to delete it, or -ReuseResourceGroup to reuse."
273+
}
274+
else {
275+
# Resource group already exists, ask the user what to do
276+
$title = "Resource group '$ResourceGroupName' already exists"
277+
$message = "Do you want to delete the resource group and all resources in it?"
272278

273-
$options = @(
274-
[System.Management.Automation.Host.ChoiceDescription]::new("&Yes", "Delete the resource group and all resources in it."),
275-
[System.Management.Automation.Host.ChoiceDescription]::new("&No", "Keep the resource group and continue."),
276-
[System.Management.Automation.Host.ChoiceDescription]::new("&Abort", "Abort execution.")
277-
)
278-
$result = $Host.UI.PromptForChoice($title, $message, $options, 0)
279+
$options = @(
280+
[System.Management.Automation.Host.ChoiceDescription]::new("&Yes", "Delete the resource group and all resources in it."),
281+
[System.Management.Automation.Host.ChoiceDescription]::new("&No", "Keep the resource group and continue."),
282+
[System.Management.Automation.Host.ChoiceDescription]::new("&Abort", "Abort execution.")
283+
)
284+
$result = $Host.UI.PromptForChoice($title, $message, $options, 0)
285+
}
279286

280287
switch ($result) {
281288
0 {

0 commit comments

Comments
 (0)