我想使用 REST API 调用创建 Azure 资源组,但我似乎无法获得正确的语法。这是我所拥有的:
$validateResourceGroupUri = 'https://management.azure.com/subscriptions/SUBSCRIPTION_ID/resourceGroups/' + $resourceGroupName + '/?api-version=2015-01-01'
try { $trapValidateResponse = Invoke-RestMethod -Method PUT -Headers $armHeaders -Uri $validateResourceGroupUri -Body $deploymentTemplate }
catch { throw $_ }
地点:
$deploymentTemplate = JSON deployment template (obviously)
$resourceGroupName = user-inputted RG name to be created
$armHeaders = @{ 'Authorization' = "Bearer $token"; 'Content-Type' = "Application/json" }
我感觉问题出在 -Body 参数中,但我似乎无法在网上找到任何详细说明调用到底应包含哪些内容的内容。我发现THIS如果您向下滚动到“创建资源组”部分,它会详细说明一些信息,但不幸的是,这就是我所能找到的全部信息。有什么想法吗?
最佳答案
您可以尝试使用以下命令来创建新的资源组,它对我有用。
##get token
$TENANTID="<your tenantid>"
$APPID="<>"
$PASSWORD="<>"
$result=Invoke-RestMethod -Uri https://login.microsoftonline.com/$TENANTID/oauth2/token?api-version=1.0 -Method Post -Body @{"grant_type" = "client_credentials"; "resource" = "https://management.core.windows.net/"; "client_id" = "$APPID"; "client_secret" = "$PASSWORD" }
$token=$result.access_token
##set subscriptionId and resource group name
$subscriptionId="<your subscriptionId >"
$resourcegroupname="<resource group name>"
$Headers=@{
'authorization'="Bearer $token"
'host'="management.azure.com"
}
$body='{
"location": "northeurope",
"tags": {
"tagname1": "test-tag"
}
}'
Invoke-RestMethod -Uri "https://management.azure.com/subscriptions/$subscriptionId/resourcegroups/${resourcegroupname}?api-version=2015-01-01" -Headers $Headers -ContentType "application/json" -Method PUT -Body $body
关于rest - 如何使用 PowerShell 中的 RESTful API 在 Azure 中创建资源组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44854649/