【问题标题】:Azure Powershell to check resource namesAzure Powershell 检查资源名称
【发布时间】:2026-01-23 09:55:02
【问题描述】:

Azure 可以拒绝随机生成的资源名称。是否有任何 Powershell cmdlet 可以检查这些名称?

我知道有一个 Test-AzureName。但它只适用于有限类型的资源。对于我的用例来说还不够。 (存储、SQL、DNS、公共 IP)

我知道有这个 REST-API。但是当我通过 Invoke-RestMethod 调用它时,它返回一个错误:{"error":{"code":"AuthenticationFailed","message":"Authentication failed. The 'Authorization' header is missing."}}

我不太擅长 Powershell,有人可以指出 Azure Powershell cmdlet 来完成这样的任务或帮助我完成 REST-API 工作吗?

谢谢!

【问题讨论】:

    标签: powershell azure


    【解决方案1】:

    带有“Check resource name”的Invoke-RestMethod REST API 足以满足您的需求。但是,你需要做一些准备。

    首先,您需要创建一个 Active Directory 应用程序。

    1. Classic Portal 中登录您的 Azure 帐户。
    2. 从左侧窗格中选择Active Directory,可以点击您的默认目录。
    3. 点击Application,然后点击底部窗格中的Add
    4. 您应该创建一个WEB 应用程序和/或WEB API。对于 NAMESIGN-ON URLAPP ID URI,输入任何合适的内容,因为在这种情况下无关紧要。我在测试时为 SIGN-ON URLAPP ID URI 输入“https://localhost”。
    5. 点击确定进行创建。
    6. 创建后,单击应用程序的CONFIGURE。向下滚动到“密钥”部分,然后选择您希望密码的有效期。
    7. 保存并获取客户端的密钥。在此页面中,您可以获取您的客户端 ID 和密钥。将它们复制并保存到其他地方,因为您以后会用到它们。
    8. 配置页面的其他应用程序的权限下,点击添加应用程序
    9. 选择Windows Azure Service Management API,点击OK添加。
    10. 以组织用户身份访问 Azure 服务管理(预览版)将委派权限添加到服务管理 API。
    11. 保存更改。

    有关此的更多信息,请参阅Create Active Directory application and service principal using portal

    以下脚本将为您提供正确的 REST API 标头。

    try{
        $subscription = Get-AzureRmSubscription
    }
    catch{
        Login-AzureRmAccount
        $subscription = Get-AzureRmSubscription
    }
    
    $tenantId = $subscription.TenantId
    
    #these are the client id and key you get from the above steps.
    $clientId = "<your client id>"
    $key = "<your key>"
    
    $authUrl = "https://login.windows.net/${tenantId}"
    $AuthContext = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]$authUrl
    
    $cred = New-Object Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential $clientId,$key
    $result = $AuthContext.AcquireToken("https://management.core.windows.net/",$cred)
    $authHeader = @{
    'Content-Type'='application/json'
    'Authorization'=$result.CreateAuthorizationHeader()
    }
    
    $URI = "https://management.azure.com/providers/microsoft.resources/checkresourcename?api-version=2014-01-01"
    Invoke-RestMethod -Uri $URI -Method POST -Headers $authHeader -Body "{'Name':'<the name you want to test>','Type':'<the resource type you want to test>'}"
    

    【讨论】:

      【解决方案2】:

      问题的核心是如何为Invoke-Rest构建身份验证http header来满足Azure要求。你可以这样做

      1. 在 Azure 默认 Active Directory 中创建应用程序
      2. 保存应用程序客户端 ID 和密钥
      3. 将应用程序适当的权限授予资源组
      4. 在您的 Powershell 上下文中添加“Microsoft.IdentityModel.Clients.ActiveDirectory.dll”
      5. 为令牌调用 Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext 方法AcquireTokenAsync
      6. 通过步骤 5 中的令牌构造 auth 标头
      7. 将 header 和 rest uri 传递给 Invoke-Rest cmdlet

      更多细节您可以参考How to authenticate Azure Rest API with Azure Service Principal by Powershell获取完整的示例代码。

      【讨论】:

        【解决方案3】:

        如上所述,大多数提供商都有一个名为 checkNameAvailability (https://docs.microsoft.com/en-us/search/?search=checkNameAvailability&scope=REST) 的 API,您可以使用它来查看名称是否已被使用或全球唯一。我已经在 PowerShell 函数中封装了其中的一些。见https://secureinfra.blog/2019/11/07/test-azure-resource-name-availability/

        【讨论】: