如何通过PowerShell删除Azure AppInsights和App Service之间的链接

时间:2019-03-08 14:49:01

标签: azure powershell appinsights

我有一个AppInsights资源,可提供一系列应用程序服务(每个应用程序服务代表一个隔离的测试环境)。我们通过PowerShell启动并销毁这些应用程序服务。

App服务通过隐藏标签链接到AppInsights。如果删除App Service,则不会自动删除隐藏的标签。资源的上限为15个标签,这意味着在破坏环境时,必须从AppInsights中删除这些标签。

我可以这样查看标签:

PS Azure:\> $AppInsights = Get-AzureRmApplicationInsights -ResourceGroupName "foo" -Name "bar";

PS Azure:\> $AppInsights.Tags;

Key                                                                                    Value
---                                                                                                                                                                  -----
hidden-link:/subscriptions/xxx/resourceGroups/foo/providers/Microsoft.Web/sites/xxx    Resource
...

因此我可以编辑该哈希表,但是似乎无法通过PowerShell将更改持久化回Azure。

还有另一种方法吗?

1 个答案:

答案 0 :(得分:1)

没有直接的方法。但是您可以使用Set-AzureRmResource来实现。

首先,您可以使用Get-AzureRmApplicationInsights -ResourceGroupName "xxx" -Name "xxx"来获取应用数据洞察的属性,请注意稍后将使用的Type:

enter image description here

我还可以看到我有5个标签:

enter image description here

我想删除一个以“ appInsightsdemo11”结尾的

下面的示例代码(您可以进行修改以满足实际需要):

$a = Get-AzureRmApplicationInsights  -ResourceGroupName "xxx" -Name "xxx"

$b = $a.tags

# use $new_tags to store the tags which are not deleted
$new_tags=@{}

# if the key does not contain "appInsightsdemo11", it will not be deleted.
$b.Keys | %{ if(!$_.contains("appInsightsdemo11")){$new_tags.Add($_, $b[$_])}}

# then update the tags which does not have the one "appInsightsdemo11"
Set-AzureRmResource -ResourceGroupName "xxx" -ResourceName "xxx" -Tag $new_tags -force -ResourceType microsoft.insights/components

完成上述命令后,再次运行cmdlet Get-AzureRmApplicationInsights。您会看到包含“ appInsightsdemo11”的标签已被删除。

enter image description here

相关问题