rest - 如何使用 PowerShell 中的 RESTful API 在 Azure 中创建资源组?

标签 rest powershell azure

我想使用 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

enter image description here

关于rest - 如何使用 PowerShell 中的 RESTful API 在 Azure 中创建资源组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44854649/

相关文章:

azure - 将 FIWARE Orion 与 Azure Cosmos DB 连接

json - 406 Not Acceptable : Spring MVC + JSON

windows - 如何从提升的 PowerShell 控制台以非管理员身份运行进程?

powershell - 在通过 GUI [powershell] 访问之前无法访问卷影复制

powershell - 在Powershell中获取USB设备实例路径

javascript - 由于循环 JSON 错误,Azure Durable Functions 无法返回对象

angular - 如何在从 package.json 读取配置时拥有 npm 包的自动版本控制机制

java - 有没有办法使用 PostProcessInterceptor 仅过滤特定类型的 ServerResponse?

angularjs - 使用 $resource? 自动提供默认参数?

用于验证资源的 REST 实践