创建循环以在 Powershell 中创建新资源组
Creating a loop to create a new resource group in Powershell
我需要创建一个循环来检查资源组名称是否被使用,如果没有使用该名称创建一个新资源组。
这是我用来尝试完成此操作的代码
do
{
$rg = Read-Host -Prompt "What would you like to name the new Resource Group"
if (!(Get-AzureRmResourceGroup -ResourceGroupName $rg -ErrorAction Ignore))
{
New-AzureRmResourceGroup -ResourceGroupName $rg -Location "West Europe"
}
else {
$rg = Read-Host -Prompt "Resouce Group name not available, please select another"
New-AzureRmResourceGroup -ResourceGroupName $rg -Location "West Europe"
}
}
while (!(Get-AzureRmResourceGroup -ResourceGroupName $rg -ErrorAction Ignore))
你说的:"I want a user to enter a number, and keep entering if until they enter something > 10".
您编码的内容:"Enter a number, test if it's < 10 and prompt again. Don't test this one, go with it without testing it. Now loop all of that."
do
{
# coming round from a previous loop, $num exists, indicating this is a retry.
if ($null -ne $num) { Write-Host "Sorry, try again" }
[int]$num = Read-Host "Enter a number"
} until ($num -gt 10)
我需要创建一个循环来检查资源组名称是否被使用,如果没有使用该名称创建一个新资源组。
这是我用来尝试完成此操作的代码
do
{
$rg = Read-Host -Prompt "What would you like to name the new Resource Group"
if (!(Get-AzureRmResourceGroup -ResourceGroupName $rg -ErrorAction Ignore))
{
New-AzureRmResourceGroup -ResourceGroupName $rg -Location "West Europe"
}
else {
$rg = Read-Host -Prompt "Resouce Group name not available, please select another"
New-AzureRmResourceGroup -ResourceGroupName $rg -Location "West Europe"
}
}
while (!(Get-AzureRmResourceGroup -ResourceGroupName $rg -ErrorAction Ignore))
你说的:"I want a user to enter a number, and keep entering if until they enter something > 10".
您编码的内容:"Enter a number, test if it's < 10 and prompt again. Don't test this one, go with it without testing it. Now loop all of that."
do
{
# coming round from a previous loop, $num exists, indicating this is a retry.
if ($null -ne $num) { Write-Host "Sorry, try again" }
[int]$num = Read-Host "Enter a number"
} until ($num -gt 10)