【问题标题】:Cannot see Resource group in Azure portal在 Azure 门户中看不到资源组
【发布时间】:2018-11-28 21:52:48
【问题描述】:

剧透:我是 Azure 和 Azure Powershell 的新手。

我开始学习 Azure 和 Azure Powershell,我目前的自学练习是编写一个脚本,用于检查 Azure 中是否存在特定资源组。如果此特定资源组不存在,则创建一个。于是我开始写这个脚本:

# Exit on error
$ErrorActionPreference = "Stop"

# Import module for Azure Rm
Import-Module AzureRM

# Connect with Azure
Connect-AzureRmAccount

# Define name of Resource group we want to create
$ResourceGroupTest = "ResourceGroupForStorageAccount"

# Check if ResourceGroup exists
Get-AzureRmResourceGroup -Name $ResourceGroupTest -ErrorVariable $NotPresent -ErrorAction SilentlyContinue

Write-Host "Start to check if Resource group '$($ResourceGroupTest)' exists..."
if ($NotPresent) {
    Write-Host "Resource group with name '$($ResourceGroupTest)' does not exist."

    # Create resource group
    New-AzureRmResourceGroup -Name $ResourceGroupTest -Location "West Europe" -Verbose
} else {
    Write-Host "Found Resource group with name '$($ResourceGroupTest)'."
}

现在,当我运行这个脚本时,我得到了这样的输出:

Start to check if Resource group 'ResourceGroupForStorageAccount' exists...
Found Resource group with name 'ResourceGroupForStorageAccount'.
Account                      SubscriptionName               Tenant ...
-------                      ----------------               -------- ...
my.email@host.com            Some subscription              ...             

但我在 Azure RM 门户的资源组列表中找不到这个名为 ResourceGroupForStorageAccount 的新创建资源组。

我的问题在哪里?

【问题讨论】:

  • 嗨,克里斯蒂安,你的问题有什么更新吗?对你有用吗?
  • @IvanYang 第一次阅读您的答案后,我在脚本中更改了它并重新运行它,但没有看到任何更改。现在,几天后,我再次尝试,它成功了,现在我可以在我的 Azure 门户中看到资源组。所以,谢谢你的回答。
  • 资源组创建后过段时间看不到就太奇怪了,不过能用就太高兴了:)。

标签: azure powershell azure-powershell azure-resource-group


【解决方案1】:

-ErrorVariable 的值不正确,请使用NotPresent 而不是$NotPresent 作为参数-ErrorVariable。如果使用-ErrorVariable $NotPresent,则$NotPresent 始终为null/false,因此永远不会执行创建资源命令。

示例代码如下:

#your other code here.

# Check if ResourceGroup exists
Get-AzureRmResourceGroup -Name $ResourceGroupTest -ErrorVariable NotPresent -ErrorAction SilentlyContinue

Write-Host "Start to check if Resource group '$($ResourceGroupTest)' exists..."
if ($NotPresent) {
    Write-Host "Resource group with name '$($ResourceGroupTest)' does not exist."

    # Create resource group
    New-AzureRmResourceGroup -Name $ResourceGroupTest -Location "West Europe" -Verbose
} else {
    Write-Host "Found Resource group with name '$($ResourceGroupTest)'."
}

【讨论】:

    【解决方案2】:

    只是为了添加到现有答案中,这是因为 powershell 在您的表达式中扩展了变量-ErrorVariable $NotPresent。因为你的变量不存在它变成:-ErrorVariable。所以它不会创建一个名为 not present 的变量,并且您的 if() 语句不会像您期望的那样工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多