Check If Azure Resource Group Exist - Azure Powershell

霸气de小男生 提交于 2019-12-06 17:29:02

问题


I'm trying to verify if ResourceGroup exist or not so i thought that following code should return true or false, but it doesn't output anything.

$RSGtest = Find-AzureRmResource | Format-List ResourceGroupName | get-unique
$RSGtest -Match "$myResourceGroupName"

Why am I not getting any output?


回答1:


There is a Get-AzureRmResourceGroup cmdlet:

Get-AzureRmResourceGroup -Name $myResourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinue

if ($notPresent)
{
    # ResourceGroup doesn't exist
}
else
{
    # ResourceGroup exist
}

Note: Consider using the new Get-AzResourceGroup cmdlet:

Get-AzResourceGroup -Name $myResourceGroupName -ErrorVariable notPresent -ErrorAction SilentlyContinue

if ($notPresent)
{
    # ResourceGroup doesn't exist
}
else
{
    # ResourceGroup exist
}



回答2:


try this

$ResourceGroupName = Read-Host "Resource group name"
Find-AzureRmResourceGroup | where {$_.name -EQ $ResourceGroupName}


来源:https://stackoverflow.com/questions/37598086/check-if-azure-resource-group-exist-azure-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!