我不太擅长 Powershell,有人可以指出我使用 Azure Powershell cmdlet 来完成这样的任务或帮助我完成 REST-API 工作吗?谢谢! 解决方案 Invoke-RestMethod with "检查资源名称" REST API 足以满足您的需求.但是,您需要做一些准备.首先,您需要创建一个 Active Directory 应用程序.在经典门户中登录到您的 Azure 帐户.从左侧窗格中选择 Active Directory,可以单击您的默认目录.点击应用,然后点击底部窗格中的添加.您应该创建一个WEB 应用程序和/或WEB API.对于NAME、SIGN-ON URL 和APP ID URI,输入任何合适的内容,因为在这种情况下无关紧要.我为 SIGN-ON URL 和 APP ID URIhttps://localhost"/strong> 测试时.点击确定进行创建.创建后,单击应用程序的CONFIGURE.向下滚动到密钥"部分,然后选择您希望密码的有效期.保存并为您的客户获取密钥.在此页面中,您可以获取客户端 ID 和密钥.将它们复制并保存在其他地方,因为您以后会需要它们.在配置页面的对其他应用程序的权限下,点击添加应用程序.选择Windows Azure Service Management API,然后点击确定进行添加.添加以组织用户身份访问 Azure 服务管理(预览版) 委派的服务管理 API 权限.保存更改.有关这方面的更多信息,请参阅 使用门户创建 Active Directory 应用程序和服务主体以下脚本将为您提供适当的 REST API 标头.尝试{$subscription = Get-AzureRmSubscription}抓住{登录-AzureRmAccount$subscription = Get-AzureRmSubscription}$tenantId = $subscription.TenantId#这些是您从上述步骤中获得的客户端 ID 和密钥.$clientId = ""$key = ""$authUrl = "https://login.windows.net/${tenantId}"$AuthContext = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]$authUrl$cred = 新对象 Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential $clientId,$key$result = $AuthContext.AcquireToken("https://management.core.windows.net/",$cred)$authHeader = @{'内容类型'='应用程序/json''授权'=$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':'','Type':''}"Random generated resource names can be rejected by Azure. Is there any Powershell cmdlet to check those names?I know there is a Test-AzureName. But it only works with a limited type of resources. Not enough for my use case. (Storage, SQL, DNS, Public IP)And I know there is this REST-API. But when I call it through Invoke-RestMethod, it returns an error: {"error":{"code":"AuthenticationFailed","message":"Authentication failed. The 'Authorization' header is missing."}}I'm not very good at Powershell, can someone point me out Azure Powershell cmdlet to do such a task or help me to get the REST-API work?Thanks! 解决方案 The Invoke-RestMethod with "Check resource name" REST API is good enough for your case. But, you need to do some preparation.First, You need to create an Active Directory application.Login to your Azure Accout in the Classic Portal.Select Active Directory from the left pane, can click your default Directory.Click Application, and click Add in the bottom pane.You should create a WEB APPLICATION AND/OR WEB API. For NAME, SIGN-ON URL, and APP ID URI, enter anything suitable because it doesn't matter in this case. I enter "https://localhost" for SIGN-ON URL and APP ID URI when testing.Click OK to create.After the Creation, click CONFIGURE of your application. Scroll down to the Keys section and select how long you would like your password to be valid.Save and get the key for your client. In this page, you can get your client id and key. Copy and save them somewhere else, because you will need them later.In the Configure Page, under the permissions to other applications, click Add application.Select Windows Azure Service Management API, and click OK to Add.Add the Access Azure Service Management as organization users (preview) delegated permission to the service management API.Save the change.For more information about this, see Create Active Directory application and service principal using portalthe following script will give you proper headers for the 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>'}" 这篇关于用于检查资源名称的 Azure Powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!