Azure: delete all tags - azure

I have script to delete all Tags.
I get Tags yet:
Get-AzTag
Name Count
displayName 2
App 1
Department 1
Env 1
Owner 1
I can't find which resource is attached.
Get-AzResource
Any idea?
thanks

Thanks #Gill-Bates, According to SO-thread it says,
A tag or value that is already associated with a resource or resource group cannot be removed. Before using Remove-AzTag, use the Tag parameter of the Set-AzResourceGroup cmdlet to delete the tag or values from the resource or resource group.
Update-AzTag -ResourceId xyz-Tag $tags -Operation Delete
xyz is resource id
Output:
References taken from:
https://github.com/Azure/azure-cli/issues/11761
https://debbiesmspowerbiazureblog.home.blog/2020/04/27/azure-tagging-with-powershell-example/
https://stackoverflow.com/a/54162388

Related

Azure: How To Bulk Tag with PowerShell Using CSV

My PowerShell is VERY rusty, so please bear with me. I've been tasked to bulk tag Azure Resources based on CSV data, specifically Azure VM's. In this CSV are 3 headers (VMName, TagName, TagValue). I've tried to automate this task with PowerShell and no matter how I format the code, I keep falling short. Can someone help me clear this up or perhaps point me in the direction of a known working PS script that will help me accomplish this?
`
Connect-AzAccount -TenantId ''
Set-AzContext -Subscription ''
$Import = Import-Csv -Path '...\Tags.csv' |
ForEach-Object {
$_.psobject.properties |
ForEach-Object {Set-Variable -Name $_.Name -Value $_.Tags}
foreach ($Name in $Import) {
$Tag = $_.Name.Tags
$Tag.Add($_.Tags)
Set-AzResource -ResourceId $Name.ResourceId -Tag -Force
}
}
`
I've tried a hash table and a fully customized script. It either only applies the Tag Name and not the Tag Value, and it needs both, or it shoots off error after error. Microsoft seems to want bulk tagging at the subscription and resource group level, so it's a bit difficult to get this right specific to resources. In the end, I want the script to read the server name in Row 1 Column 1, find that resource in Azure, and create the Tag using the Tag Name (Row 1 Column 2) and Tag Value (Row 1 Column 3).
After reproducing from my end, I could able to get this work using New-AzTag. Below is the complete script that worked for me.
$Import = Import-Csv -Path 'Tags.csv'
foreach($I in $Import) {
$Resource = Get-AzResource -Name $I.VMName
$TagName=$I.TagName
$TagValue=$I.TagValue
New-AzTag -ResourceId $Resource.ResourceId -Tag #{"$TagName"="$TagValue"}
}
RESULTS:
Tags.csv
In portal

Route Table Details

Is there a way to fetch all details of routing from each subscription with Route-Table Name, subscriptions, next hop type address prefix.
I have tried 'Get-AzRouteTable -ResourceGroupName "" -Name "prod" | Get-AzRouteConfig | Export-Csv azureroutetable2.csv' but getting only from specific environment, is there a better way to do the same?
Regards
Devbrat
According to the help for Get-AzRouteTable it doesn't require any value to be specified for ResourceGroupName so we should just be able to loop through your available contexts. If you did have a large Azure Network infrastructure and they were logically organised by Resource Group, you may want to consider looping through Resource Groups too.
That being said, you can do something like this. It will create a csv for each subscription in your current session. Change the . at the beginning of the path value to set a full path if you would like.
$Contexts = Get-AzContext -ListAvailable
foreach ($Context in $Contexts) {
[void](Set-AzContext -SubscriptionId $Context.Subscription.Id)
$RouteConfig = Get-AzRouteTable -Name "prod" | Get-AzRouteConfig
# If you are using PowerShell 5.1, add '-NoTypeInformation' to the end of this command.
$RouteConfig | Export-Csv -Path ".\$($Context.Subscription.Name)_Routes.csv" -Encoding utf8 -NoClobber
}

Azure YAML pipeline: find variable in group dynamically

I have a Powershell script that is executed in my YAML pipeline during the release stage.
I can reference group variables using $(myVar). However now I have a need to reference a variable dynamically given its name.
For instance, if I have the string 'myVar', I want to search the variable group for a variable named 'myVar' and get its value.
Is this possible to achieve ?
In the end I did it like this. First I installed the Devops module for powershell so I can get group variables through devops api :
Add-AzureDevOpsAccount -OrganisationName "myor" -ProjectName "myproj" -Token "mytoken"
Write-Host -ForegroundColor DarkGreen "Getting variables group based on name"
$group = Get-AzureDevOpsVariableGroup -SearchString "dev"
Then to get a variable based on its name :
$tokenValue = $group.variables | Select-Object -ExpandProperty $someName | Select-Object -ExpandProperty Value

Extract ResourceName or resourceIdentifier from resourceId function in ARM template

How do i get the name / resource identifier by the resource display name in ARM template?
I have the user group called "Developer" and unique identifier as "xxxxxxxxxxx" under the resource type "Microsoft.ApiManagement/service/groups"
By the resource display name "Developer", how can i get the identifier (xxxxxxxxxxx) ?
you can use resourceId() function for that:
resourceId('Microsoft.ApiManagement/service/groups', 'ApiManagementName', 'Developer')
Reading: https://learn.microsoft.com/en-us/azure/azure-resource-manager/resource-group-template-functions-resource#resourceid

How to export LocalNetworkGateway info from multiple Resource Groups

I'm new to powershell and azure and need to export all the LocalNetworkGateway information from multiple Subscriptions and Resource Groups.
I have a script to export from Resource Groups but I have to manually enter the ResourceGroupName for each one.
Is there a way to have a variable that contains all the ResourceGroupNames so that I don't have to run the script 40 times and manually enter a different ResourceGroupName for each?
Any help would be gratefully received.
I have code for one Resource Group at a time.
Get-AzLocalNetworkGateway -ResourceGroupName “RGName” | Export-Csv -Path "c:\Azure\LocalNetworkGateway.csv"
you can just iterate over resource groups:
$resourceGroups = Get-AzResourceGroup
$resourceGroups.foreach{
Get-AzLocalNetworkGateway -ResourceGroupName $_.ResourceGroupName |
Export-Csv -Path "c:\Azure\LocalNetworkGateway.csv" -Append
}

Resources