Azure可以拒绝随机生成的资源名称.是否有任何Powershell cmdlet来检查这些名称?
我知道有一个Test-AzureName.但它只适用于有限类型的资源.对我的用例来说还不够.(存储,SQL,DNS,公共IP)
我知道有这个REST-API.但是当我通过Invoke-RestMethod调用它时,它会返回一个错误:{"error":{"code":"AuthenticationFailed","message":"身份验证失败.'Authorization'标头丢失了."}}
我不是很擅长Powershell,是否可以有人指出我使用Azure Powershell cmdlet执行此类任务或帮助我使REST-API工作?
谢谢!
在Invoke-RestMethod与" 检查资源名称 " REST API是你的情况不够好.但是,你需要做一些准备.
首先,您需要创建一个Active Directory应用程序.
有关此内容的详细信息,请参阅使用门户创建Active Directory应用程序和服务主体
以下脚本将为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>'}"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5105 次 |
| 最近记录: |