创建循环以在Powershell中创建新资源组

时间:2017-03-16 22:27:16

标签: powershell resources azure-resource-group

我需要创建一个循环来检查是否采用了资源组名称,如果没有,则创建一个具有该名称的新资源组。

这是我用来尝试完成此代码的代码

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))

1 个答案:

答案 0 :(得分:0)

你说的是什么:"我希望用户输入一个号码,并在他们输入某些内容时继续输入> 10"

您编码的内容:"输入一个数字,测试它是否<< s< 10再次提示。不要测试这个,不经测试就可以使用它。现在循环所有这些。"

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)
相关问题