检查 Azure 资源组是否存在 - Azure Powershell
Check If Azure Resource Group Exist - Azure Powershell
我正在尝试验证 ResourceGroup 是否存在,所以我认为以下代码应该 return 正确或错误,但它不会输出任何内容。
$RSGtest = Find-AzureRmResource | Format-List ResourceGroupName | get-unique
$RSGtest -Match "$myResourceGroupName"
为什么我没有得到任何输出?
更新:
你现在应该使用Get-AzResourceGroup cmdlet from the new cross-plattform AZ PowerShell Module。 :
Get-AzResourceGroup -Name $myResourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinue
if ($notPresent)
{
# ResourceGroup doesn't exist
}
else
{
# ResourceGroup exist
}
原答案:
有一个 Get-AzureRmResourceGroup cmdlet:
Get-AzureRmResourceGroup -Name $myResourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinue
if ($notPresent)
{
# ResourceGroup doesn't exist
}
else
{
# ResourceGroup exist
}
试试这个
$ResourceGroupName = Read-Host "Resource group name"
Find-AzureRmResourceGroup | where {$_.name -EQ $ResourceGroupName}
我也在寻找同样的东西,但在我的场景中还有一个额外的条件。
所以我是这样想出来的。获取场景详情
$rg="myrg"
$Subscriptions = Get-AzSubscription
$Rglist=@()
foreach ($Subscription in $Subscriptions){
$Rglist +=(Get-AzResourceGroup).ResourceGroupName
}
$rgfinal=$rg
$i=1
while($rgfinal -in $Rglist){
$rgfinal=$rg +"0" + $i++
}
Write-Output $rgfinal
Set-AzContext -Subscription "Subscription Name"
$createrg= New-AzResourceGroup -Name $rgfinal -Location "location"
我是一个 PS 新手,我正在寻找这个问题的解决方案。
我没有直接在 SO 上搜索,而是尝试使用 PS 帮助自行调查(以获得更多关于 PS 的经验),我想出了一个可行的解决方案。
然后我搜索了 SO 以查看我与专家答案的比较情况。
我想我的解决方案不太优雅但更紧凑。我在这里报告,以便其他人发表意见:
if (!(Get-AzResourceGroup $rgname -ErrorAction SilentlyContinue))
{ "not found"}
else
{"found"}
我的逻辑解释:我分析了 Get-AzResourceGroup 输出,发现它是一个包含找到的资源组元素的数组,如果未找到组,则为 null。我选择了 not (!) 形式,它有点长但允许跳过 else 条件。最常见的情况是,如果资源组不存在,我们只需要创建它,如果已经存在,则什么也不做。
我正在尝试验证 ResourceGroup 是否存在,所以我认为以下代码应该 return 正确或错误,但它不会输出任何内容。
$RSGtest = Find-AzureRmResource | Format-List ResourceGroupName | get-unique
$RSGtest -Match "$myResourceGroupName"
为什么我没有得到任何输出?
更新:
你现在应该使用Get-AzResourceGroup cmdlet from the new cross-plattform AZ PowerShell Module。 :
Get-AzResourceGroup -Name $myResourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinue
if ($notPresent)
{
# ResourceGroup doesn't exist
}
else
{
# ResourceGroup exist
}
原答案:
有一个 Get-AzureRmResourceGroup cmdlet:
Get-AzureRmResourceGroup -Name $myResourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinue
if ($notPresent)
{
# ResourceGroup doesn't exist
}
else
{
# ResourceGroup exist
}
试试这个
$ResourceGroupName = Read-Host "Resource group name"
Find-AzureRmResourceGroup | where {$_.name -EQ $ResourceGroupName}
我也在寻找同样的东西,但在我的场景中还有一个额外的条件。
所以我是这样想出来的。获取场景详情
$rg="myrg"
$Subscriptions = Get-AzSubscription
$Rglist=@()
foreach ($Subscription in $Subscriptions){
$Rglist +=(Get-AzResourceGroup).ResourceGroupName
}
$rgfinal=$rg
$i=1
while($rgfinal -in $Rglist){
$rgfinal=$rg +"0" + $i++
}
Write-Output $rgfinal
Set-AzContext -Subscription "Subscription Name"
$createrg= New-AzResourceGroup -Name $rgfinal -Location "location"
我是一个 PS 新手,我正在寻找这个问题的解决方案。
我没有直接在 SO 上搜索,而是尝试使用 PS 帮助自行调查(以获得更多关于 PS 的经验),我想出了一个可行的解决方案。 然后我搜索了 SO 以查看我与专家答案的比较情况。 我想我的解决方案不太优雅但更紧凑。我在这里报告,以便其他人发表意见:
if (!(Get-AzResourceGroup $rgname -ErrorAction SilentlyContinue))
{ "not found"}
else
{"found"}
我的逻辑解释:我分析了 Get-AzResourceGroup 输出,发现它是一个包含找到的资源组元素的数组,如果未找到组,则为 null。我选择了 not (!) 形式,它有点长但允许跳过 else 条件。最常见的情况是,如果资源组不存在,我们只需要创建它,如果已经存在,则什么也不做。